import {
  Portal,
  Popover,
  PopoverTrigger,
  PopoverBody,
  PopoverContent,
  Box,
  Text,
  Flex,
  Spacer,
  Image,
  HStack,
} from '@chakra-ui/react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Option, ContextMenuProps } from '../../../interfaces/context';
import { fetchNui } from '../../../utils/fetchNui';

const openMenu = (id: string | undefined) => {
  fetchNui<ContextMenuProps>('openContext', { id: id, back: false });
};

const clickContext = (id: string) => {
  fetchNui('clickContext', id);
};

const Item: React.FC<{
  option: [string, Option];
}> = ({ option }) => {
  const button = option[1];
  const buttonKey = option[0];

  return (
    <>
      <Popover placement="right-start" trigger="hover" eventListeners={{ scroll: true }} isLazy>
        <PopoverTrigger>
          <Box
            bg="gray.800"
            borderRadius="md"
            h="fit-content"
            w="100%"
            p={2}
            mb={1}
            fontFamily="Poppins"
            fontSize="md"
            transition="300ms"
            _hover={{ bg: 'gray.700' }}
          >
            <Flex
              w="100%"
              alignItems="center"
              onClick={() => (button.menu ? openMenu(button.menu) : clickContext(buttonKey))}
            >
              {button?.icon && (
                <FontAwesomeIcon
                  fixedWidth
                  icon={button.icon}
                  fontSize={20}
                  style={{
                    marginRight: 10,
                    justifySelf: 'center',
                    color: button.iconColor,
                  }}
                />
              )}
              <Box>
                <Box paddingBottom={button.description ? 1 : 0}>
                  <Text w="100%" fontWeight="medium">
                    {button.title ? button.title : buttonKey}
                  </Text>
                </Box>
                {button.description && (
                  <Box paddingBottom={1}>
                    <Text>{button.description}</Text>
                  </Box>
                )}
              </Box>
              {(button.menu || button.arrow) && button.arrow !== false && (
                <>
                  <Spacer />
                  <Box alignSelf="center" justifySelf="center" mr={4} fontSize="xl">
                    <FontAwesomeIcon icon="chevron-right" />
                  </Box>
                </>
              )}
            </Flex>
            <Portal>
              {(button.metadata || button.image) && (
                <PopoverContent
                  fontFamily="Poppins"
                  bg="gray.800"
                  outline="none"
                  border="none"
                  w="fit-content"
                  maxW="2xs"
                >
                  <PopoverBody>
                    <>
                      {button.image && <Image src={button.image} />}
                      {Array.isArray(button.metadata) ? (
                        button.metadata.map((metadata: string | { label: string; value: any }, index: number) => (
                          <Text key={`context-metadata-${index}`}>
                            {typeof metadata === 'string' ? `${metadata}` : `${metadata.label}: ${metadata.value}`}
                          </Text>
                        ))
                      ) : (
                        <>
                          {typeof button.metadata === 'object' &&
                            Object.entries(button.metadata).map((metadata: { [key: string]: any }, index) => (
                              <Text key={`context-metadata-${index}`}>
                                {metadata[0]}: {metadata[1]}
                              </Text>
                            ))}
                        </>
                      )}
                    </>
                  </PopoverBody>
                </PopoverContent>
              )}
            </Portal>
          </Box>
        </PopoverTrigger>
      </Popover>
    </>
  );
};

export default Item;
