import { Button, IconButton, Modal as ModalMui } from '@mui/material'
import {
  Link,
  matchPath,
  Outlet,
  useLocation,
  useNavigate
} from 'react-router-dom'
import styles from './style.module.scss'
import { appPublicRoutes } from '../../routes'
import { faClose } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'

function useRouteMatch(patterns: readonly string[]) {
  const { pathname } = useLocation()

  for (let i = 0; i < patterns.length; i += 1) {
    const pattern = patterns[i]
    const possibleMatch = matchPath(pattern, pathname)
    if (possibleMatch !== null) {
      return possibleMatch
    }
  }

  return null
}

export const Modal = () => {
  const navigate = useNavigate()
  const tabs = [
    { to: appPublicRoutes.login, title: 'Login', label: 'Login' },
    { to: appPublicRoutes.register, title: 'Register', label: 'Register' },
    {
      to: appPublicRoutes.nostr,
      title: 'Login',
      sx: { padding: '10px' },
      label: (
        <img
          src="https://image.nostr.build/fb557f1b6d58c7bbcdf4d1edb1b48090c76ff1d1384b9d1aae13d652e7a3cfe4.gif"
          width="25"
          alt="nostr logo"
          height="25"
        />
      )
    }
  ]
  const routeMatch = useRouteMatch(tabs.map((t) => t.to))
  const activeTab = routeMatch?.pattern?.path
  const handleClose = () => {
    navigate(appPublicRoutes.landingPage)
  }

  return (
    <ModalMui open={true} onClose={handleClose} aria-labelledby="modal-title">
      <div className={styles.modal}>
        <header className={styles.header}>
          <h2 className={styles.title} id="modal-title">
            {tabs.find((t) => activeTab === t.to)?.title}
          </h2>
          <IconButton
            onClick={handleClose}
            sx={{
              fontSize: '18px',
              color: 'rgba(0, 0, 0, 0.5)',
              padding: '8px 15px',
              borderRadius: '4px',
              ':hover': {
                background: 'var(--primary-light)',
                color: 'white'
              }
            }}
          >
            <FontAwesomeIcon icon={faClose} />
          </IconButton>
        </header>

        <main className={styles.body}>
          <ul>
            {tabs.map((t) => {
              return (
                <li key={t.to}>
                  <Button
                    component={Link}
                    to={t.to}
                    sx={{
                      width: '100%',
                      height: '100%',
                      ...(t?.sx ? t.sx : {})
                    }}
                    variant={
                      activeTab === t.to || t.to === appPublicRoutes.nostr
                        ? 'contained'
                        : 'text'
                    }
                  >
                    {t.label}
                  </Button>
                </li>
              )
            })}
          </ul>
          <div className={styles.tabContent}>
            <Outlet />
          </div>
        </main>

        <footer className={styles.footer}>Welcome to SIGit!</footer>
      </div>
    </ModalMui>
  )
}
