import React, {useState, ReactNode} from 'react';
import {useTranslation} from 'react-i18next';
import {ImageOrVideo} from 'react-native-image-crop-picker';
import {Attachment, GifAttachment} from '@interfaces/attachments';
import {BodyAttributes} from '@interfaces/bodyAttributes';
import {LinkPreviewsBodyParam} from '@interfaces/linkPreview';
import {PollConfiguration, Post, PostState} from '@modules/post/interfaces';
import {transformLinksToSpans} from '@modules/post/screens/PublishPost/utils';
import {EMPTY_STRING} from '@shared/constants';

import {Multimedia} from './components/Multimedia';
import {getPostAttachments} from './utils';
import {PublishIcons} from './components/PublishIcons';
import {Author} from './components/Author';
import {Size} from './components/Size';
import {SharedPost} from './components/SharedPost';
import {NewPostContext} from './context/NewPostContext';
import {TopContainer} from './components/TopContainer';
import RichTextInput from './components/RichTextInput';
import {Suggestions} from './components/Suggestions';
import {ScrollContainer} from './components/ScrollContainer';
import {useGetEditor} from './hooks/useGetEditor';
import SegmentationPill from './components/SegmentationPill';
import CommonHeader from './components/CommonHeader';
import BadgeScheduleButton from './components/BadgeScheduleButton';
import NewPostNavigator from './navigation';
import {PushNotificationDialog} from './components/PushNotificationDialog';

interface Props {
  children: ReactNode;
  post?: Partial<Post>;
}

export function NewPost({post, children}: Props) {
  const [currentSize, setCurrentSize] = useState(0);
  const initialContent = post?.bodyHtml
    ? transformLinksToSpans(post.bodyHtml)
    : post?.body || '';
  const [messageBody, setMessageBody] = useState(initialContent);
  const [isLoading, setIsLoading] = useState(false);
  const [inputChanged, setInputChanged] = useState(false);
  const [linkPreviews, setLinkPreviews] = useState<LinkPreviewsBodyParam>(
    post?.linkPreviews ?? null,
  );
  const initialAttachments = getPostAttachments(post);
  const [messageBodyWithoutLastTag, setMessageBodyWithoutLastTag] = useState(
    post?.body || '',
  );
  const [images, setImages] = useState<Attachment[]>(initialAttachments.images);
  const [videos, setVideos] = useState<Attachment[]>(initialAttachments.videos);
  const [files, setFiles] = useState<Attachment[]>(initialAttachments.files);
  const [hasPoll, setHasPoll] = useState(!!post?.poll);
  const [pollOptions, setPollOptions] = useState(
    post?.poll?.pollOptions?.map(({option}) => option) || [
      EMPTY_STRING,
      EMPTY_STRING,
    ],
  );
  const [pollConfiguration, setPollConfiguration] = useState<PollConfiguration>(
    post?.poll?.pollConfiguration || {
      endsAt: null,
      isAnonymous: true,
    },
  );
  const [scheduleDateTime, setScheduleDateTime] = useState<Date | undefined>(
    post?.state === PostState.Scheduled && post.publicationDatetime != null
      ? new Date(post.publicationDatetime)
      : undefined,
  );
  const [sendPushNotification, setSendPushNotification] =
    useState<boolean>(false);
  const [gif, setGif] = useState<Nullable<GifAttachment>>(
    initialAttachments.gif,
  );
  const [taggedUsers, setTaggedUsers] = useState<BodyAttributes[]>(
    post?.bodyAttributes || [],
  );
  const [segmentationItemIds, setSegmentationItemIds] = useState<number[]>(
    post?.segmentationItemIdsArray || [],
  );
  const [pickedAssets, setPickedAssets] = useState<ImageOrVideo[]>([]);

  const {t} = useTranslation();

  const editor = useGetEditor({
    initialContent,
    setMessageBody,
    placeholder: t(hasPoll ? 'post.make_question' : 'general.write_something'),
  });

  return (
    <NewPostContext.Provider
      value={{
        currentSize,
        setCurrentSize,
        messageBody,
        messageBodyWithoutLastTag,
        setMessageBody,
        setMessageBodyWithoutLastTag,
        hasPoll,
        setHasPoll,
        pollConfiguration,
        setPollConfiguration,
        editor,
        isLoading,
        setIsLoading,
        post,
        images,
        videos,
        files,
        gif,
        taggedUsers,
        linkPreviews,
        setImages,
        setVideos,
        setFiles,
        setGif,
        initialAttachments,
        pollOptions,
        setPollOptions,
        setTaggedUsers,
        setLinkPreviews,
        inputChanged,
        setInputChanged,
        segmentationItemIds,
        setSegmentationItemIds,
        scheduleDateTime,
        setScheduleDateTime,
        setSendPushNotification,
        sendPushNotification,
        pickedAssets,
        setPickedAssets,
      }}>
      {children}
    </NewPostContext.Provider>
  );
}

export * from './hooks/useNewPost';
export * from './hooks/useSaveDraftOnUnmount';
export * from './constants';
export * from './components/Suggestions/constants';
export * from './navigation';
export * from './services';
export * from './interfaces';
export * from './utils';
export {default as TagUsersTextInput} from './components/TagUsersTextInput';
export {default as NewPostDocuments} from './components/Documents';
export {default as NewPostImagesAndVideos} from './components/Multimedia/components/ImagesAndVideos';
export {default as NewPostAddImage} from './components/Footer/components/AddImage';
export {default as NewPostAddVideo} from './components/Footer/components/AddVideo';
export {default as NewPostImageOrVideo} from './components/Footer/components/ImageOrVideo';
export {default as NewPostSelectFile} from './components/Footer/components/SelectFile';
export {default as PublishPostDialogs} from './components/PublishPostDialogs';

NewPost.ScrollContainer = ScrollContainer;
NewPost.TopContainer = TopContainer;
NewPost.Author = Author;
NewPost.SegmentationPill = SegmentationPill;
NewPost.Size = Size;
NewPost.RichTextInput = RichTextInput;
NewPost.Suggestions = Suggestions;
NewPost.PublishIcons = PublishIcons;
NewPost.Multimedia = Multimedia;
NewPost.CommonHeader = CommonHeader;
NewPost.BadgeScheduleButton = BadgeScheduleButton;
NewPost.Navigator = NewPostNavigator;
NewPost.PushNotificationDialog = PushNotificationDialog;
NewPost.SharedPost = SharedPost;
