import { type FC } from 'react';
import { useNavigate } from 'react-router';

import {
  IconCalendarPlus,
  IconEdit,
  IconShare2 as IconShare,
  IconTrash,
  IconUserDown,
} from '@material-hu/icons/tabler';

import HuMenuList from '@material-hu/components/composed-components/MenuList';
import useHuSnackbar from '@material-hu/components/design-system/Snackbar';

import { logEvent } from 'src/config/amplitude';
import useCopyUrl from 'src/hooks/useCopyUrl';
import useFeatureFlag from 'src/hooks/useFeatureFlag';
import { EventName } from 'src/types/amplitude';
import { type Event, EventActions } from 'src/types/events';
import { FeatureFlags } from 'src/types/featureFlags';
import { canPerformAction } from 'src/utils/actionsLinks';
import { insertIf } from 'src/utils/arrays';
import { useLokaliseTranslation } from 'src/utils/i18n';

import useDownloadCalendar from '../hooks/useDownloadCalendar';
import { useDownloadGuestList } from '../hooks/useDownloadGuestList';
import { eventsRoutes } from '../routes';

type Props = {
  event: Event;
  canManageEvent: boolean;
  showDeleteModal: () => void;
};

export const EventMenuItems: FC<Props> = props => {
  const { event, canManageEvent, showDeleteModal } = props;
  const { copyUrlToClipboard } = useCopyUrl();
  const { enqueueSnackbar } = useHuSnackbar();

  const isMultipleOrganizersEnabled = useFeatureFlag(
    FeatureFlags.EVENTS_MULTIPLE_ORGANIZERS_ENABLED,
  );

  const canEditEvent = isMultipleOrganizersEnabled
    ? canPerformAction(event._links, EventActions.EDIT)
    : canManageEvent;
  const canDeleteEvent = isMultipleOrganizersEnabled
    ? canPerformAction(event._links, EventActions.DELETE)
    : canManageEvent;

  const { isLoadingCalendar, downloadCalendarEvent } = useDownloadCalendar(
    event?.id,
  );

  const { isLoadingGuests, downloadGuestList } = useDownloadGuestList(event);

  const handleDownloadGuestList = () => {
    downloadGuestList();
    logEvent(EventName.EVENT_REPORT_DOWNLOAD, {
      eventId: event?.id,
      isForGroup: true, //for now is always true because by default the event is for entire community,
    });
  };

  const handleCopyUrl = () => {
    copyUrlToClipboard(eventsRoutes.event(event?.id));
    enqueueSnackbar({
      title: t('LINK_COPIED'),
      variant: 'info',
    });
  };

  const { t } = useLokaliseTranslation('events');
  const navigate = useNavigate();

  const options = [
    {
      onClick: handleCopyUrl,
      Icon: IconShare,
      title: t('COPY_LINK'),
      description: t('COPY_LINK_DESCRIPTION'),
    },
    {
      onClick: () => downloadCalendarEvent(),
      disabled: isLoadingCalendar,
      Icon: IconCalendarPlus,
      title: t('ADD_TO_CALENDAR'),
      description: t('ADD_TO_CALENDAR_DESCRIPTION'),
    },
    ...insertIf(canManageEvent, {
      onClick: handleDownloadGuestList,
      disabled: isLoadingGuests,
      Icon: IconUserDown,
      title: t('EXPORT_GUEST_LIST'),
      description: t('EXPORT_GUEST_LIST_DESCRIPTION'),
    }),
    ...insertIf(canEditEvent, {
      onClick: () => navigate(eventsRoutes.eventEdit(event?.id)),
      Icon: IconEdit,
      title: t('general:edit'),
      description: t('EDIT_DESCRIPTION'),
    }),
    ...insertIf(canDeleteEvent, {
      onClick: showDeleteModal,
      Icon: IconTrash,
      title: t('ELIMINATE'),
      description: t('DELETE_DESCRIPTION'),
    }),
  ];

  return (
    <HuMenuList
      options={options}
      fixedDimensions={false}
    />
  );
};

export default EventMenuItems;
