'use client';

import { Disclosure } from '@headlessui/react';
import { Bars3Icon, XMarkIcon } from '@heroicons/react/24/outline';
import { ConnectButton } from '@/components/ConnectButton';

const navigation = [
  { name: 'DApp', href: '/dapp', current: false },
  { name: 'Settings', href: '/dapp?settings=1', current: false },
  { name: 'README', href: 'https://git.manko.yoga/manawenuz/mortgagefi-helper', current: false, external: true },
];

function classNames(...classes: string[]) {
  return classes.filter(Boolean).join(' ');
}

export default function Navbar() {
  return (
    <Disclosure
      as="nav"
      className="sticky top-0 z-40 border-b border-line/70 bg-cloud/70 backdrop-blur-md"
    >
      {({ open }) => (
        <>
          <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div className="flex justify-between h-16">
              <div className="flex">
                <div className="flex-shrink-0 flex items-center gap-2">
                  <span className="text-2xl" aria-hidden>🌱</span>
                  <span className="font-display text-xl font-semibold text-forest-deep">
                    MortgageFi
                  </span>
                </div>
                <div className="hidden sm:ml-8 sm:flex sm:space-x-2">
                  {navigation.map((item) => (
                    <a
                      key={item.name}
                      href={item.href}
                      target={item.external ? "_blank" : "_self"}
                      rel={item.external ? "noopener noreferrer" : ""}
                      className="inline-flex items-center rounded-full px-3 py-1.5 my-3 text-sm font-medium text-ink-soft transition hover:bg-sun/30 hover:text-forest-deep"
                    >
                      {item.name}
                    </a>
                  ))}
                </div>
              </div>
              <div className="hidden sm:ml-6 sm:flex sm:items-center">
                <ConnectButton />
              </div>
              <div className="-mr-2 flex items-center sm:hidden">
                <Disclosure.Button className="inline-flex items-center justify-center p-2 rounded-full text-ink-soft hover:text-forest-deep hover:bg-sun/30 focus:outline-none focus:ring-2 focus:ring-sky-deep">
                  <span className="sr-only">Open main menu</span>
                  {open ? (
                    <XMarkIcon className="block h-6 w-6" aria-hidden="true" />
                  ) : (
                    <Bars3Icon className="block h-6 w-6" aria-hidden="true" />
                  )}
                </Disclosure.Button>
              </div>
            </div>
          </div>

          <Disclosure.Panel className="sm:hidden">
            <div className="pt-2 pb-3 space-y-1 px-2">
              {navigation.map((item) => (
                <Disclosure.Button
                  key={item.name}
                  as="a"
                  href={item.href}
                  target={item.external ? "_blank" : "_self"}
                  rel={item.external ? "noopener noreferrer" : ""}
                  className="block rounded-xl px-3 py-2 text-base font-medium text-ink-soft hover:bg-sun/30 hover:text-forest-deep"
                >
                  {item.name}
                </Disclosure.Button>
              ))}
              <div className="pt-4 pb-2 border-t border-line/60">
                <div className="px-2">
                  <ConnectButton />
                </div>
              </div>
            </div>
          </Disclosure.Panel>
        </>
      )}
    </Disclosure>
  );
}
