"use client";

import React, { useState, useEffect } from 'react';
import classnames from 'classnames';
import Link from 'next/link';
import Button from './Button';
import Image from 'next/image';
import Dropdown from "./Dropdown";

export interface MenuItem {
  title: string;
  route?: string;
  children?: MenuItem[];
}

const menuItems: MenuItem[] = [
  {
    title: "Employers",
    children: [
      {
        title: "Employer Services",
        route: "",
      },
      {
        title: "Self-Funded Plans",
        route: "",
      },
      {
        title: "Control Health Care Costs",
        route: "",
      },
    ],
  },
  {
    title: "Members",
    children: [
      {
        title: "Find a Medical Provider",
        route: "",
      },
      {
        title: "Obtain Rx Information",
        route: "",
      },
    ],
  },
  {
    title: "Providers",
    children: [
      {
        title: "Eligibility Lookup",
        route: "",
      },
      {
        title: "Contact BBS",
        route: "",
      },
    ],
  },
  {
    title: "Brokers",
    route: "",
    },
];

const Navbar = () => {

    const [isScrolled, setIsScrolled] = useState(false);

    useEffect(() => {
        const handleScroll = () => {
            // Find pixels from top to bottom of "tag line" div
            if (window.scrollY > 280) {
                setIsScrolled(true);
            } else {
                setIsScrolled(false);
            }
        };

        window.addEventListener('scroll', handleScroll);

        return () => {
            window.removeEventListener('scroll', handleScroll);
        };
    }, []);

  return (
    <nav className="sticky top-0 left-0 z-50 hidden lg:flex justify-center">
        <div className={classnames(
                    "w-11/12 bg-britton-neutral px-1 text-britton-dark rounded-b-3xl", {
                        "min-h-6 pb-1 border-b-2 border-x-2 border-britton-dark" : isScrolled,
                        "min-h-20 pb-4" : !isScrolled
                    }
                )}
        >
            <div className="container mx-auto justify-between items-center flex">
                <div className={classnames('rounded-b-3xl bg-britton-dark w-72', {"hidden" : isScrolled})}>
                    <Image
                        src="/images/bbstpa-forsite.png"
                        alt="Britton Logo"
                        width={273}
                        height={80}
                    />
                </div>
                <div className={classnames('rounded-b-3xl py-2 bg-britton-dark font-semibold text-britton-neutral text-center text-lg uppercase w-72', {"hidden" : !isScrolled})}>
                    <Link href="/">
                        Britton Benefit Services
                    </Link>
                </div>
                {/* <ul className={classnames("flex space-x-4 font-semibold text-lg uppercase", {"pl-2" : isScrolled})}>
                    <li>
                        <Link href="/services" className="hover:text-britton-light">
                            Employers
                        </Link>
                    </li>
                    <li>
                        <Link href="/contact" className="hover:text-britton-light">
                            Members
                        </Link>
                    </li>
                    <li>
                        <Link href="/contact" className="hover:text-britton-light">
                            Providers
                        </Link>
                    </li>
                    <li>
                        <Link href="/contact" className="hover:text-britton-light">
                            Brokers
                        </Link>
                    </li>
                    <li>
                        <Link href="/contact" className="hover:text-britton-light">
                            Contact Us
                        </Link>
                    </li>
                </ul> */}
                <div className={classnames("flex space-x-4 font-semibold text-lg uppercase", {"pl-2" : isScrolled})}>
                    {menuItems.map((item) => {
                    return item.hasOwnProperty("children") ? 
                    (
                        <Dropdown item={item} />
                    ) : (
                        <Link className="hover:text-britton-light" href={item?.route || ""}>
                            {item.title}
                        </Link>
                    );
                    })}
                </div>
                <div className={classnames("flex space-x-2 font-semibold", {"pt-1" : isScrolled})}>
                    <Button className="bg-britton-neutral text-britton-accent2 hover:text-britton-accent py-1 px-4 rounded border-1 border-britton-accent2 hover:border-britton-accent">
                        Sign In
                    </Button>
                    <Button className="bg-britton-dark hover:bg-britton-medium text-britton-light py-1 px-4 rounded">
                        Get a Quote
                    </Button>
                </div>
            </div>
        </div>
    </nav>
  );
};

export default Navbar;