import React, {memo} from 'react';
import {Keyboard} from 'react-native';
import {IconChartBar} from '@tabler/icons-react-native';
import {useNavigation} from '@react-navigation/native';
import {StackNavigationProp} from '@react-navigation/stack';
import {useNewPost} from '@components/_custom/NewPost/hooks/useNewPost';
import {
  NewPostScreens,
  NewPostStackParamList,
} from '@components/_custom/NewPost/navigation';

import {BaseButton} from '../BaseButton';

function AddPoll() {
  const {hasPoll, gif, editor, post} = useNewPost();
  const requestEdit = !!post?._links?.requestEdit;
  const navigation =
    useNavigation<StackNavigationProp<NewPostStackParamList>>();

  const toggleHasPoll = () => {
    editor.blur();
    Keyboard.dismiss();
    setTimeout(() => {
      navigation.navigate(NewPostScreens.CreatePoll);
    }, 500);
  };

  return (
    <BaseButton
      Icon={IconChartBar}
      onPress={toggleHasPoll}
      disabled={hasPoll || !!gif || requestEdit}
    />
  );
}

export default memo(AddPoll);
