import { useState } from 'react';
import { NavLink } from '../../ui/NavLink';
import { Link } from 'react-router-dom';
import { Lang, useLang } from '../../store/language';

export function Navbar() {
  const [menuOpen, setMenuOpen] = useState(false);
  const { value: lang } = useLang();

  return (
    <>
      <nav className="flex items-stretch justify-between border-b border-[#3D425C] desktop:pl-10 mobile:pl-4 desktop:min-h-[72px] mobile:min-h-16">
        <Link to={'/'} className="flex w-[104px] justify-between items-center">
          <img src="src/assets/logo.svg" alt="" />
          <img
            src="src/assets/text_logo.svg"
            alt=""
            className="desktop:block mobile:hidden"
          />
        </Link>
        <div className="flex">
          <NavLink text="Типы тренажеров" route="/" />
          <NavLink text="Варианты комплектации" route="/" />
          <NavLink text="Проекты" route="/" />
          <NavLink text="События" route="/" />
          <button className="desktop:text-lg tablet:text-base font-semibold text-[#ffffff] bg-gradient-to-r from-[#798FFF] to-[#D375FF] border-x border-[#3D425C] tablet:block mobile:hidden px-10">
            Оставить заявку
          </button>
          {!menuOpen && <LangToggler lang={lang} />}
          <button
            onClick={() => setMenuOpen(prev => !prev)}
            className="px-6 py-5 min-[1350px]:hidden mobile:block border-[#3D425C] border-l"
          >
            <img
              src={`src/assets/${menuOpen ? 'cross' : 'burger'}.svg`}
              alt=""
            />
          </button>
        </div>
      </nav>
      {menuOpen && (
        <div
          onClick={() => setMenuOpen(false)}
          className={
            'absolute z-50 w-full min-[1350px]:hidden tablet:max-[1350px]:max-w-[340px] right-0 border-x border-b border-[#3D425C]' +
            (menuOpen ? ' shadow-[0_0_0_9999px_rgba(0,0,0,.4)]' : '')
          }
        >
          <BurgerLink route="/" text="Типы тренажеров" />
          <BurgerLink route="/" text="Варианты комплектации" />
          <BurgerLink route="/" text="Проекты" />
          <BurgerLink route="/" text="События" />
          <div className="grid mobile:max-tablet:grid-cols-[216px_1fr_1fr] tablet:grid-cols-2">
            <button className="text-[#ffffff] desktop:text-lg tablet:text-base tablet:hidden font-semibold bg-gradient-to-r from-[#798FFF] to-[#D375FF] border-x border-[#3D425C] py-[30px] px-10">
              Оставить заявку
            </button>
            <ChooseLang lang="RU" />
            <ChooseLang lang="EN" />
          </div>
        </div>
      )}
    </>
  );
}

function BurgerLink({ text, route }: { text: string; route: string }) {
  return (
    <Link
      to={route}
      className="flex px-10 py-6 gap-1 text-[#ffffff] mobile:text-base desktop:text-lg bg-[#14161F] w-full font-semibold border-[#3D425C] border-b hover:bg-[#3D425C] active:bg-[#14161F]"
    >
      <img src="src/assets/cube.svg" alt="" />
      {text}
    </Link>
  );
}

function ChooseLang({ lang }: { lang: 'RU' | 'EN' }) {
  const { updateLang, value } = useLang();
  return (
    <div
      className={
        'p-px min-h-[72px] ' +
        (value !== lang
          ? 'bg-[#3D425C]'
          : 'bg-gradient-to-r from-[#798FFF] to-[#D375FF] box-border')
      }
    >
      <button
        onClick={() => updateLang(lang)}
        className={
          'text-[#ffffff] w-full h-full desktop:text-lg tablet:text-base font-semibold bg-[#14161F] hover:bg-[#3D425C] active:bg-[#14161F]'
        }
      >
        {lang}
      </button>
    </div>
  );
}

function LangToggler({ lang }: { lang: Lang }) {
  const [open, setOpen] = useState(false);
  return (
    <div className="min-w-[101px] mobile:max-[1350px]:hidden box-border border-r border-[#3D425C]">
      <button
        onClick={() => setOpen(prev => !prev)}
        className="mx-6 h-full gap-x-1 items-center flex text-[#ffffff] font-semibold box-border"
      >
        {lang}
        <img src="src/assets/arrow_down.svg" alt="" />
      </button>
      {open && (
        <div
          className="absolute grid grid-cols-2 min-w-[101px] box-border"
          onClick={() => setOpen(false)}
        >
          <ChooseLang lang={'RU'} />
          <ChooseLang lang={'EN'} />
        </div>
      )}
    </div>
  );
}
