import {
  AppBar as AppBarMui,
  Box,
  Button,
  Menu,
  MenuItem,
  Toolbar,
  Typography
} from '@mui/material'

import { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { setAuthState } from '../../store/actions'
import { State } from '../../store/rootReducer'
import { Dispatch } from '../../store/store'
import Username from '../username'

import { Link, useNavigate } from 'react-router-dom'
import { NostrController } from '../../controllers'
import { appPublicRoutes, getProfileRoute } from '../../routes'
import {
  clearAuthToken,
  getRoboHashPicture,
  saveNsecBunkerDelegatedKey,
  shorten
} from '../../utils'
import styles from './style.module.scss'

export const AppBar = () => {
  const navigate = useNavigate()

  const dispatch: Dispatch = useDispatch()

  const [username, setUsername] = useState('')
  const [userAvatar, setUserAvatar] = useState('')
  const [anchorElUser, setAnchorElUser] = useState<null | HTMLElement>(null)

  const authState = useSelector((state: State) => state.auth)
  const metadataState = useSelector((state: State) => state.metadata)

  useEffect(() => {
    if (metadataState && metadataState.content) {
      const { picture, display_name, name } = JSON.parse(metadataState.content)
      const pubkey = authState?.usersPubkey || ''

      if (picture) {
        setUserAvatar(picture)
      } else {
        setUserAvatar(getRoboHashPicture(pubkey))
      }

      setUsername(shorten(display_name || name || '', 7))
    }
  }, [metadataState])

  const handleOpenUserMenu = (event: React.MouseEvent<HTMLElement>) => {
    setAnchorElUser(event.currentTarget)
  }

  const handleCloseUserMenu = () => {
    setAnchorElUser(null)
  }

  const handleProfile = () => {
    const hexKey = authState?.usersPubkey
    if (hexKey) navigate(getProfileRoute(hexKey))

    setAnchorElUser(null)
  }

  const handleLogout = () => {
    handleCloseUserMenu()
    dispatch(
      setAuthState({
        loggedIn: false,
        usersPubkey: undefined,
        loginMethod: undefined,
        nsecBunkerPubkey: undefined
      })
    )

    // clear authToken saved in local storage
    clearAuthToken()

    // update nsecBunker delegated key after logout
    const nostrController = NostrController.getInstance()
    const newDelegatedKey = nostrController.generateDelegatedKey()
    saveNsecBunkerDelegatedKey(newDelegatedKey)

    navigate('/')
  }
  const isAuthenticated = authState?.loggedIn === true

  return (
    <AppBarMui position="fixed" className={styles.AppBar}>
      <Toolbar className={styles.toolbar}>
        <Box className={styles.logoWrapper}>
          <img src="/logo.png" alt="Logo" onClick={() => navigate('/')} />
        </Box>

        <Box className={styles.rightSideBox}>
          {!isAuthenticated && (
            <Button
              onClick={() => {
                navigate(appPublicRoutes.login)
              }}
              variant="contained"
            >
              Sign in
            </Button>
          )}

          {isAuthenticated && (
            <>
              <Username
                username={username}
                avatarContent={userAvatar}
                handleClick={handleOpenUserMenu}
              />
              <Menu
                id="menu-appbar"
                anchorEl={anchorElUser}
                anchorOrigin={{
                  vertical: 'bottom',
                  horizontal: 'center'
                }}
                keepMounted
                transformOrigin={{
                  vertical: 'top',
                  horizontal: 'center'
                }}
                open={!!anchorElUser}
                onClose={handleCloseUserMenu}
              >
                <MenuItem
                  sx={{
                    justifyContent: 'center',
                    display: { md: 'none' }
                  }}
                >
                  <Typography variant="h6">{username}</Typography>
                </MenuItem>
                <MenuItem
                  onClick={handleProfile}
                  sx={{
                    justifyContent: 'center'
                  }}
                >
                  Profile
                </MenuItem>
                <Link
                  to={appPublicRoutes.help}
                  target="_blank"
                  style={{ color: 'inherit', textDecoration: 'inherit' }}
                >
                  <MenuItem
                    sx={{
                      justifyContent: 'center'
                    }}
                  >
                    Help
                  </MenuItem>
                </Link>
                <MenuItem
                  onClick={handleLogout}
                  sx={{
                    justifyContent: 'center'
                  }}
                >
                  Logout
                </MenuItem>
              </Menu>
            </>
          )}
        </Box>
      </Toolbar>
    </AppBarMui>
  )
}
