import { FC, SyntheticEvent } from 'react';

import Popover from '@material-hu/mui/Popover';
import Stack from '@material-hu/mui/Stack';
import Typography from '@material-hu/mui/Typography';

import HuPills from '@material-hu/components/design-system/Pills';

import useHuGoTheme from 'src/hooks/useHuGoTheme';
import { Holiday, TimeOffRequest } from 'src/types/timeTracking';
import { useLokaliseTranslation } from 'src/utils/i18n';

type Props = {
  anchor: HTMLElement | null;
  closePopover: () => void;
  isOpen: boolean;
  dayOffEvent: TimeOffRequest | Holiday;
};

const DayOffPopover: FC<Props> = props => {
  const { anchor, closePopover, isOpen, dayOffEvent } = props;
  const { t } = useLokaliseTranslation('time_tracker');
  const HuGoThemeProvider = useHuGoTheme();

  if (!dayOffEvent) {
    return null;
  }

  const isTimeOffRequest = 'timeOffPolicyTypeName' in dayOffEvent;
  return (
    <HuGoThemeProvider>
      <Popover
        anchorEl={anchor}
        open={isOpen}
        onClose={(e: SyntheticEvent) => {
          e.stopPropagation();
          closePopover();
        }}
        anchorOrigin={{
          vertical: 'bottom',
          horizontal: 'left',
        }}
        onClick={e => {
          e.stopPropagation();
        }}
        slotProps={{
          paper: {
            sx: {
              bgcolor: ({ palette }) => palette.new.background.elements.default,
              border: ({ palette }) =>
                `1px solid ${palette.new.border.neutral.default}`,
            },
          },
        }}
      >
        <Stack sx={{ width: '338px' }}>
          <Stack sx={{ p: 2, gap: 0.5 }}>
            <Typography
              variant="globalS"
              sx={{
                color: ({ palette }) => palette.new.text.neutral.default,
                fontWeight: 'semiBold',
              }}
            >
              {isTimeOffRequest
                ? dayOffEvent.timeOffPolicyTypeName
                : dayOffEvent.holidayName}
            </Typography>
          </Stack>
          <Stack
            sx={{
              borderTop: ({ palette }) =>
                `1px solid ${palette.new.border.neutral.default}`,
              flexDirection: 'row',
              justifyContent: 'space-between',
              p: 2,
            }}
          >
            <HuPills
              type="highlight"
              label={isTimeOffRequest ? t('permit_day') : t('holiday')}
              hasIcon={false}
            />
          </Stack>
        </Stack>
      </Popover>
    </HuGoThemeProvider>
  );
};

export default DayOffPopover;
