import { motion } from 'framer-motion';
import { PropsWithChildren, useRef, useState } from 'react';
import { AnchorLink } from '../../ui/AnchorLink';
import { Link } from 'react-router-dom';
import { useOnClickOutside } from 'usehooks-ts';
import { useModalStore } from '../../store/modalStore';
import { ModalWithForm } from '../Main/ModalWithForm';
import { Button } from '../../ui/Button';
import { BurgerIcon } from '../icons/BurgerIcon';
import { CloseIcon } from '../icons/CloseIcon';
import { CubeIcon } from '../icons/CubeIcon';

export function Header() {
  const [menuOpen, setMenuOpen] = useState(false);
  const setModal = useModalStore(state => state.setModal);

  const menuRef = useRef<HTMLDivElement>(null);
  const menuBtnRef = useRef<HTMLButtonElement>(null);
  useOnClickOutside<HTMLDivElement | HTMLButtonElement>(
    [menuRef, menuBtnRef],
    () => setMenuOpen(false),
  );

  return (
    <header className="sticky top-0 z-[90] w-full bg-[#14161F]">
      <nav className="flex justify-between border-b border-[#3D425C] lg:pl-10 pl-4 xl:max-h-[72px] max-h-16">
        <Link to={'/'} className="flex items-center outline-none">
          <img
            src="/images/GRAFFinteractive.svg"
            alt="GRAFF interactive"
            className="w-[104px]"
          />
        </Link>
        <div className="flex mx-auto">
          <AnchorLink route="#products">Типы тренажеров</AnchorLink>
          <AnchorLink route="#projects">Проекты</AnchorLink>
          <AnchorLink route="#trainings">Варианты комплектации</AnchorLink>
        </div>
        <Button
          onClick={() => setModal(<ModalWithForm />)}
          className="btn-text max-sm:hidden px-10 font-semibold rounded-none"
        >
          Оставить заявку
        </Button>
        {/* <LangToggler lang={lang} /> */}
        <button
          ref={menuBtnRef}
          onClick={() => setMenuOpen(prev => !prev)}
          className="p-5 xl:hidden border-[#3D425C] max-sm:border-l outline-none"
        >
          <div className="size-6">
            {!menuOpen ? <BurgerIcon /> : <CloseIcon />}
          </div>
        </button>
      </nav>
      <motion.div
        initial={{ opacity: 0, visibility: 'hidden' }}
        animate={{
          opacity: +menuOpen,
          visibility: menuOpen ? 'visible' : 'hidden',
        }}
        transition={{ duration: 0.2 }}
        ref={menuRef}
        onClick={() => setMenuOpen(false)}
        className={
          'absolute z-50 w-full min-[1350px]:hidden sm:max-[1350px]:max-w-[340px] right-0' +
          (menuOpen ? ' shadow-[0_0_0_9999px_rgba(0,0,0,.4)]' : '')
        }
      >
        <div>
          <BurgerAnchor route="#products">Типы тренажеров</BurgerAnchor>
          <BurgerAnchor route="#projects">Проекты</BurgerAnchor>
          <BurgerAnchor route="#trainings">Варианты комплектации</BurgerAnchor>
          <Button
            onClick={() => {
              setMenuOpen(false);
              setModal(<ModalWithForm />);
            }}
            width="full"
            className="sm:hidden btn-text py-6 font-semibold rounded-none outline-none"
          >
            Оставить заявку
          </Button>
        </div>
        <div className="gridgrid-cols-[2fr_1fr_1fr] sm:grid-cols-2">
          {/* <ChooseLang currentLang="RU" />
          <ChooseLang currentLang="EN" /> */}
        </div>
      </motion.div>
    </header>
  );
}

function BurgerAnchor({
  children,
  route,
}: PropsWithChildren<{ route: string }>) {
  return (
    <Link
      to={route}
      className="flex gap-x-1 items-center px-10 py-6 gap-1 btn-text bg-[#14161F] w-full hover:bg-[#3D425C] last:border-b-0 [&:not(:last-child)]:border-b sm:border-l font-semibold border-[#3D425C] lg:hover:bg-[#3D425C] active:bg-[#14161F]"
    >
      <CubeIcon />
      {children}
    </Link>
  );
}

// function ChooseLang({ currentLang }: { currentLang: 'RU' | 'EN' }) {
//   const { setLang, lang } = useLanguageStore();

//   return (
//     <button
//       onClick={() => setLang(currentLang)}
//       className={
//         'min-h-[72px] w-full h-full btn-text font-semibold bg-[#14161F] sm:hover:bg-[#3D425C] border active:bg-[#14161F] outline-none ' +
//         (lang === currentLang
//           ? '[border-image:linear-gradient(to_right,#798FFF,#D375FF)_3]'
//           : 'border-[#3D425C]')
//       }
//     >
//       {currentLang}
//     </button>
//   );
// }

// function LangToggler({ lang }: { lang: Lang }) {
//   const [open, setOpen] = useState(false);
//   const langTogglerRef = useRef<HTMLButtonElement>(null);
//   useOnClickOutside(langTogglerRef, () => setOpen(false));
//   const hovered = useHover(langTogglerRef);

//   return (
//     <div
//       className="max-w-[101px]s max-xl:hidden relative"
//       style={{ backgroundColor: hovered ? '#3D425C' : 'transparent' }}
//     >
//       <button
//         ref={langTogglerRef}
//         onClick={() => setOpen(prev => !prev)}
//         className="btn-text flex gap-x-1 items-center mx-6 h-full font-semibold outline-none"
//       >
//         {lang}
//         <ChevronDownIcon />
//       </button>
//       <AnimatePresence>
//         <motion.div
//           className="absolute z-20 grid grid-cols-2 min-w-[101px]s w-full"
//           onClick={() => setOpen(false)}
//           initial={{ visibility: 'hidden', opacity: 0 }}
//           animate={{
//             visibility: open ? 'visible' : 'hidden',
//             opacity: +open,
//           }}
//           exit={{ visibility: 'hidden', opacity: 0 }}
//         >
//           <ChooseLang currentLang={'RU'} />
//           <ChooseLang currentLang={'EN'} />
//         </motion.div>
//       </AnimatePresence>
//     </div>
//   );
// }
