import {
  AddIcon,
  ArrowBackIcon,
  ArrowLeftIcon,
  ArrowRightIcon,
  SearchIcon,
} from "@chakra-ui/icons";
import {
  Avatar,
  Box,
  Button,
  HStack,
  Image,
  Input,
  InputGroup,
  InputLeftElement,
  Popover,
  PopoverArrow,
  PopoverBody,
  PopoverContent,
  PopoverFooter,
  PopoverTrigger,
  Portal,
  Text,
  useColorMode,
  VStack,
} from "@chakra-ui/react";
import React, { useContext, useState } from "react";
import { Link, useNavigate } from "react-router-dom";
import * as XLSX from "xlsx";
import GlobalStateContext from "../Contexts/GlobalStateContext";
import mainLogo from "../assets/optifii_logo.svg";
import { MdNotificationsNone } from "react-icons/md";
import womenpfp from "../assets/womenpfp1.png";
import { MdLogout } from "react-icons/md";

const users = [
  { id: 1, user: "Jenny Wilson", role: "Admin" },
  { id: 2, user: "Jenny Wilson", role: "Employee" },
];

const HeaderMain = ({
  logOutHandler,
  slideDirecttion,
  isDrawerOpen,
  toggleDrawer,
  blur,
}) => {
  const navigate = useNavigate();
  const { colorMode, toggleColorMode } = useContext(GlobalStateContext);
  const [searchTerm, setSearchTerm] = useState("");

  const openDrawerOnClick = () => {};

  return (
    <Box
      w={"100%"}
      h={{ base: "8%", lg: "8%" }}
      position={"relative"}
      className={` pt-2 pb-2  fw-400 border-bottom d-flex ${
        slideDirecttion ? " ps-2" : ""
      }  justify-content-between align-items-center`}
      zIndex={999}
    >
      <Box display={"flex"} alignItems={"center"}>
        <Box
          w={"250px"}
          display={"flex"}
          alignItems={"center"}
          justifyContent={"space-between"}
        >
          <Box
            className={`d-flex  ${
              true ? "justify-content-start" : "justify-content-center"
            }  p-4 pt-0 pb-0 position-relative `}
          >
            <Image
              style={{
                width: 95,
              }}
              src={mainLogo}
              alt="Logo"
              onClick={() => !blur && navigate("/")}
              cursor={"pointer"}
            />
          </Box>
          <Button
            colorScheme={"forestGreen"}
            rounded={"lg"}
            onClick={toggleDrawer}
            style={{
              width: "28px",
              height: "28px",
              minWidth: "28px",
              zIndex: 99,
              backgroundColor: "#6311CB29",
            }}
          >
            {isDrawerOpen ? (
              <ArrowLeftIcon className="web-text-small" color={"#6311CB"} />
            ) : (
              <ArrowRightIcon className="web-text-small " color={"#6311CB"} />
            )}
          </Button>
        </Box>

        {!blur && (
          <InputGroup width={350} size="sm" ml={5}>
            <InputLeftElement pointerEvents="none">
              <SearchIcon color="gray.300" />
            </InputLeftElement>
            <Input
              type="search"
              placeholder="Type to search..."
              rounded="md"
              focusBorderColor="#3725EA"
              value={searchTerm}
              onChange={(e) => setSearchTerm(e.target.value)}
            />
          </InputGroup>
        )}
      </Box>
      <Box
        filter={blur && "blur(5px)"}
        display={"flex"}
        justifyContent={"space-between"}
      >
        <Box display={"flex"} gap={2}>
          <Box display={"flex"} gap={2} alignItems={"center"} me={2}>
            <Button size={"sm"} bg={"none"} p={0}>
              <MdNotificationsNone fontSize={"20px"} />
            </Button>
          </Box>
          <Box me={4} gap={2} className="d-flex justify-content-center ">
            <Popover placement="bottom">
              <Portal>
                <PopoverContent maxW="200px">
                  <PopoverArrow />
                  <Link to="/profile">
                    <PopoverBody
                      textAlign="center"
                      color="#6311CB"
                      className="web-text-medium pointer link"
                    >
                      View Profile
                    </PopoverBody>
                  </Link>

                  {/* <Link to={"/help-and-support"}>
                    <PopoverBody className="web-text-medium pointer ">
                      Help & Support
                    </PopoverBody>
                  </Link> */}
                  <PopoverBody className="web-text-medium pointer ">
                    <VStack>
                      <Text color="#A0ABBB" fontSize={"small"} fontWeight={600}>
                        Switch Accounts
                      </Text>
                      {users.map((val) => {
                        return (
                          <VStack
                            className="web-text-medium pointer link"
                            h="50px"
                            pt={2}
                            key={val.id}
                            w="100%"
                          >
                            <HStack
                              alignItems="flex-start"
                              justifyContent="space-evenly"
                              w="100%"
                            >
                              <Image
                                src={womenpfp}
                                h="31.79px"
                                alt="Profile Picture"
                              />
                              <Text
                                as={"span"}
                                fontSize="x-small"
                                fontWeight={600}
                                color="#191D23"
                              >
                                {val.user}
                                <Text
                                  // as={"span"}
                                  fontSize="x-small"
                                  fontWeight={600}
                                  color="#A0ABBB"
                                >
                                  {val.role}
                                </Text>
                              </Text>
                            </HStack>
                          </VStack>
                        );
                      })}
                    </VStack>
                  </PopoverBody>

                  <PopoverFooter
                    onClick={logOutHandler}
                    className="web-text-medium pointer link"
                    display="flex"
                    flexDirection="row"
                    alignItems="flex-start"
                    gap={3}
                  >
                    <MdLogout />
                    <Text
                      as={"span"}
                      color="#868788"
                      fontWeight={600}
                      fontSize="x-small"
                    >
                      Sign out of all accounts
                    </Text>
                  </PopoverFooter>
                </PopoverContent>
              </Portal>
              {blur ? (
                <Box>
                  <Box className="d-flex pointer  align-items-center">
                    <Avatar size="sm" boxSize={37} bg={"#210a33"} />
                    <Box
                      style={{
                        display: "flex",
                      }}
                      className=" overflow-hidden ms-3  flex-column "
                    >
                      <Text as={"span"} className="web-text-small">
                        Hello, Developers
                      </Text>
                      <Text as={"span"} className="web-text-xsmall">
                        mailto:wdi@tanami.com
                      </Text>
                    </Box>
                  </Box>
                </Box>
              ) : (
                <PopoverTrigger>
                  <Box className="d-flex pointer  align-items-center">
                    <Avatar size="sm" boxSize={37} bg={"#210a33"} />
                    <Box
                      style={{
                        display: "flex",
                      }}
                      className=" overflow-hidden ms-3  flex-column "
                    >
                      <Text as={"span"} className="web-text-small">
                        Hello, Developers
                      </Text>
                      <Text as={"span"} className="web-text-xsmall">
                        mailto:wdi@tanami.com
                      </Text>
                    </Box>
                  </Box>
                </PopoverTrigger>
              )}
            </Popover>
          </Box>
        </Box>
      </Box>
    </Box>
  );
};

export default HeaderMain;
