'use client';

import { useTheme } from 'next-themes';
import { useState, useEffect } from 'react';
import { Dropdown } from '@/components/ui/Dropdown';
import { FiAlignJustify, FiSearch } from 'react-icons/fi';
import { RiMoonFill, RiSunFill } from 'react-icons/ri';
import { IoMdNotificationsOutline } from 'react-icons/io';

export function Header({
  brandText,
  onOpenSidenav,
}: {
  brandText: string;
  onOpenSidenav: () => void;
}) {
  const { theme, setTheme } = useTheme();
  const [mounted, setMounted] = useState(false);
  useEffect(() => setMounted(true), []);

  return (
    <nav className="sticky top-4 z-40 mx-4 flex flex-row flex-wrap items-center justify-between rounded-xl bg-white/10 p-2 backdrop-blur-xl dark:bg-[#0b14374d]">
      <div className="ml-[6px]">
        <p className="shrink text-[28px] capitalize text-navy-700 dark:text-white font-bold">
          {brandText}
        </p>
      </div>

      <div className="relative mt-[3px] flex h-[61px] flex-grow items-center justify-end gap-3 rounded-full bg-white px-4 py-2 shadow-xl shadow-shadow-500 dark:!bg-navy-800 dark:shadow-none md:flex-grow-0">
        <div className="flex h-full items-center rounded-full bg-lightPrimary text-navy-700 dark:bg-navy-900 dark:text-white xl:w-[225px]">
          <p className="pl-3 pr-2 text-xl">
            <FiSearch className="h-4 w-4 text-gray-400 dark:text-white" />
          </p>
          <input
            type="text"
            placeholder="Поиск..."
            className="block h-full w-full rounded-full bg-lightPrimary text-sm font-medium text-navy-700 outline-none placeholder:text-gray-400 dark:bg-navy-900 dark:text-white sm:w-fit"
          />
        </div>

        <span
          className="flex cursor-pointer text-xl text-gray-600 dark:text-white xl:hidden"
          onClick={onOpenSidenav}
        >
          <FiAlignJustify className="h-5 w-5" />
        </span>

        <Dropdown
          button={
            <p className="cursor-pointer">
              <IoMdNotificationsOutline className="h-4 w-4 text-gray-600 dark:text-white" />
            </p>
          }
          classNames="top-8 right-0 w-max"
        >
          <div className="flex w-[300px] flex-col gap-3 rounded-[20px] bg-white p-4 shadow-xl shadow-shadow-500 dark:!bg-navy-700 dark:text-white">
            <p className="text-sm text-gray-600 dark:text-white">Нет новых уведомлений</p>
          </div>
        </Dropdown>

        {mounted && (
          <div
            className="cursor-pointer text-gray-600"
            onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}
          >
            {theme === 'dark' ? (
              <RiSunFill className="h-4 w-4 text-gray-600 dark:text-white" />
            ) : (
              <RiMoonFill className="h-4 w-4 text-gray-600 dark:text-white" />
            )}
          </div>
        )}

        <Dropdown
          button={
            <div className="h-10 w-10 rounded-full bg-brand-500 flex items-center justify-center text-white font-bold">
              U
            </div>
          }
          classNames="top-12 right-0 w-56"
        >
          <div className="flex w-56 flex-col justify-start rounded-[20px] bg-white shadow-xl shadow-shadow-500 dark:!bg-navy-700 dark:text-white">
            <div className="flex flex-col p-4 gap-2">
              <a href="/profile" className="text-sm text-gray-800 dark:text-white">
                Профиль
              </a>
              <a href="/settings" className="text-sm text-gray-800 dark:text-white">
                Настройки
              </a>
              <a href="#" className="text-sm font-medium text-red-500">
                Выйти
              </a>
            </div>
          </div>
        </Dropdown>
      </div>
    </nav>
  );
}
