import React, { useContext, useEffect } from "react";
import ProfileDropdown from "./_molecules/ProfileDropdown";
import CompanyDropdown from "./_molecules/CompanyDropdown";
import { AppStateContext } from "../../../contexts/AppContext";
import { UserContext } from "../../../contexts/UserContext";
import { Company } from "src/types";

export default function Tabbar(
  props: React.PropsWithChildren<{
    openSideBar: boolean;
  }>,
) {
  const appState = useContext(AppStateContext);
  const userState = useContext(UserContext);
  const [company, setCompany] = React.useState<Company | undefined>(undefined);

  useEffect(() => {
    const user = userState.getUser();

    if (user) {
      const c = (user.companies || [])[0];
      setCompany(c);
    }
  });

  return (
    <div>
      <div className="px-5 py-3 flex bg-white shadow justify-between sticky top-0 z-50">
        <div className="flex flex-row gap-4 items-center justify-center">
          <button
            className="rounded-full p-2 hover:bg-black hover:bg-opacity-40 duration-300 hover:text-white"
            onClick={() => appState.updateOpenSidebar(!props.openSideBar)}
          >
            <svg
              xmlns="http://www.w3.org/2000/svg"
              fill="none"
              viewBox="0 0 24 24"
              strokeWidth={1.5}
              stroke="currentColor"
              className="w-6 h-6"
            >
              <path
                strokeLinecap="round"
                strokeLinejoin="round"
                d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5"
              />
            </svg>
          </button>
        </div>
        <div className="flex flex-row gap-4 justify-center items-center">
          {company && <CompanyDropdown></CompanyDropdown>}
          <ProfileDropdown></ProfileDropdown>
        </div>
      </div>
    </div>
  );
}
