import { FC, memo } from 'react';

import { useDrawerV2 } from '@material-hu/hooks/useDrawerV2';
import { IconCalendarShare } from '@material-hu/icons/tabler';

import Button from '@material-hu/components/design-system/Buttons/Button';

import getScheduleDrawerConfig from 'src/pages/dashboard/feed/components/schedule/components/ScheduleDrawerContent';
import { useScheduleDrawerState } from 'src/pages/dashboard/feed/components/schedule/components/useScheduleDrawerState';
import { useLokaliseTranslation } from 'src/utils/i18n';

type SchedulePostDraftProps = {
  disabled: boolean;
  onSchedulePost: (date: string, sendNotification: boolean) => void;
  isReschedule?: boolean;
  initialValues?: {
    date: string;
    hours: number;
    minutes: number;
    sendPush?: boolean;
  };
  isLoading?: boolean;
  canSendPostNotification: boolean;
};

const SchedulePostDraft: FC<SchedulePostDraftProps> = props => {
  const {
    disabled,
    onSchedulePost,
    isReschedule = false,
    initialValues,
    isLoading = false,
    canSendPostNotification,
  } = props;

  const { t } = useLokaliseTranslation(['communication', 'post']);

  const scheduleDrawerState = useScheduleDrawerState({
    onSchedulePost,
    canSendPush: canSendPostNotification,
    allowsOnlyChangingPush: true,
    initialValues,
    isLoading,
  });
  const { drawer: scheduleDrawer, showDrawer: showScheduleDrawer } =
    useDrawerV2(({ closeDrawer }) =>
      getScheduleDrawerConfig(scheduleDrawerState, closeDrawer),
    );

  return (
    <>
      <Button
        variant="secondary"
        disabled={disabled}
        startIcon={
          <IconCalendarShare
            width={16}
            height={16}
          />
        }
        onClick={showScheduleDrawer}
      >
        {t(
          `${isReschedule ? 'post:reschedule_post' : 'post:schedule_post_web'}`,
        )}
      </Button>
      {scheduleDrawer}
    </>
  );
};

export default memo(SchedulePostDraft);
