import { FC } from 'react';
import { useFormContext } from 'react-hook-form';

import { useSearchParams } from 'react-router-dom';

import Card from '@material-hu/mui/Card';
import CardContent from '@material-hu/mui/CardContent';
import Stack from '@material-hu/mui/Stack';

import useHuGoTheme from 'src/hooks/useHuGoTheme';
import { UsePostingReturn } from 'src/hooks/usePosting';
import { useLokaliseTranslation as useTranslation } from 'src/utils/i18n';

import PostDraftAddForm from '../PostDraftAddForm';

type PostDraftProps = {
  usePostingProps: Omit<UsePostingReturn, 'form' | 'handleSubmit' | 'submit'>;
  isEdit?: boolean;
  isScheduled?: boolean;
};

const PostDraft: FC<PostDraftProps> = props => {
  const { usePostingProps, isEdit, isScheduled } = props;
  const { t } = useTranslation(['drafts', 'post']);
  const HugoThemeProvider = useHuGoTheme();
  const { getValues } = useFormContext();
  const [searchParams] = useSearchParams();
  const canSendPostNotification = searchParams.get('canSendPostNotification');
  const canCreatePoll = searchParams.get('canCreatePoll');

  const { authorFullName, groupTitle, authorProfilePicture } = getValues();

  return (
    <HugoThemeProvider>
      <Card variant="outlined">
        <CardContent
          sx={{
            display: 'flex',
            pt: 0,
            paddingBottom: '8px !important',
          }}
        >
          <Stack sx={{ width: 1 }}>
            <PostDraftAddForm
              secondaryFullName={authorFullName}
              secondaryProfilePicture={authorProfilePicture}
              secondaryMiddleText={t('drafts:steps.post.in_the_name_of')}
              segmentationGroupName={groupTitle || t('post:feed')}
              usePostingProps={usePostingProps}
              isEdit={isEdit}
              isScheduled={isScheduled}
              canSendPostNotification={canSendPostNotification === 'true'}
              canCreatePoll={canCreatePoll === 'true'}
            />
          </Stack>
        </CardContent>
      </Card>
    </HugoThemeProvider>
  );
};

export default PostDraft;
