"use client";
import { useState } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import Image from "next/image";

import "./BarNavigation.css";

export default function BarNavigationServicio() {
  const [openMenu, setOpenMenu] = useState(false);
  const [openSubMenu, setOpenSubMenu] = useState<number | null>(null);

  const pathname = usePathname();

  const isActive = (paths: string[]) => {
    return paths.some((path) => pathname.startsWith(path));
  };

  const toggleMenu = () => setOpenMenu(!openMenu);
  const toggleSubMenu = (index: number) => {
    if (typeof window !== "undefined" && window.innerWidth <= 1000) {
      setOpenSubMenu(openSubMenu === index ? null : index);
    }
  };

  const closeAllMenus = () => {
    setOpenMenu(false);
    setOpenSubMenu(null);
  };

  return (
    <nav className="barNavigation">
      <div className={`menuToggle ${openMenu ? "" : ""}`} onClick={toggleMenu}>
        <div></div>
        <div></div>
        <div></div>
      </div>

      <ul className={openMenu ? "active" : ""}>
        <li
          className={`subMenu 
            ${openSubMenu === 0 ? "open" : ""}
            ${isActive(["/Inscripcion", "/AgregarTiempo"]) ? "active" : ""}
          `}>
          <span onClick={() => toggleSubMenu(0)}>
            <Image
              src="/user-plus.svg"
              alt="Logo FES"
              width={16}
              height={16}
              color="#545454"
            />
            Inscripciones</span>
          <ul onClick={toggleMenu}>
            <Link
              href="/Inscripcion"
              className="links"
              onClick={(e) => {
                e.stopPropagation();
                closeAllMenus();
              }}
            >
              <li>Inscripción Usuario</li>
            </Link>
            <Link
              href="/AgregarTiempo"
              className="links"
              onClick={(e) => {
                e.stopPropagation();
                closeAllMenus();
              }}
            >
              <li>Agregar Tiempo</li>
            </Link>
          </ul>
        </li>

        <li
          className={`subMenu 
          ${openSubMenu === 1 ? "open" : ""}             
          ${isActive(["/Impresiones", "/AsignacionMesas", "/AsignacionEquipo", "/Monitor"]) ? "active" : ""}
          `}>
          <span onClick={() => toggleSubMenu(1)}>
            <Image
              src="/briefcase.svg"
              alt="Logo FES"
              width={16}
              height={16}
              color="#545454"
            />
            Servicios</span>
          <ul onClick={toggleMenu}>
            <Link
              href="/Impresiones"
              className="links"
              onClick={(e) => {
                e.stopPropagation();
                closeAllMenus();
              }}
            >
              <li>Impresiones y Ploteo</li>
            </Link>
            <Link
              href="/AsignacionMesas"
              className="links"
              onClick={(e) => {
                e.stopPropagation();
                closeAllMenus();
              }}
            >
              <li>Asignación de Mesas</li>
            </Link>
            <Link
              href="/AsignacionEquipo"
              className="links"
              onClick={(e) => {
                e.stopPropagation();
                closeAllMenus();
              }}
            >
              <li>Asignación de Equipos</li>
            </Link>
            <Link
              href="/Monitor"
              className="links"
              onClick={(e) => {
                e.stopPropagation();
                closeAllMenus();
              }}
            >
              <li>Monitor</li>
            </Link>
          </ul>
        </li>

        {/* <li className="subMenu" onClick={toggleMenu}>
          <Link
            href="/Mensajes"
            className="links"
            onClick={(e) => {
              e.stopPropagation();
              closeAllMenus();
            }}
          >
            <span>Mensajes</span>
          </Link>
        </li> */}

        <li
          className={`subMenu
          ${isActive(["/ActivosMantenimiento"]) ? "active" : ""}
          `} onClick={toggleMenu}>
          <Link
            href="/ActivosMantenimiento"
            className="links"
            onClick={(e) => {
              e.stopPropagation();
              closeAllMenus();
            }}
          >
            <span>
              <Image
                src="/computer.svg"
                alt="Logo FES"
                width={16}
                height={16}
                color="#545454"
              />
              Activos y en Mantenimiento</span>
          </Link>
        </li>

      </ul>
    </nav>
  );
}
//IO
