import {useBottomTabBarHeight} from '@react-navigation/bottom-tabs';
import {StackNavigationProp} from '@react-navigation/stack';
import React, {Fragment, useContext} from 'react';
import {StyleSheet, View} from 'react-native';
import {SafeAreaView} from 'react-native-safe-area-context';
import {
  Channel,
  Chat,
  MessageInput,
  MessageList,
  OverlayProvider,
} from 'stream-chat-react-native';
import {ChatContext} from '../../App';
import UpArrowIcon from '../../assets/icons/up_arrow.svg';
import ChatHeader from '../../components/messages/ChatHeader';
import {TAGG_LIGHT_BLUE} from '../../constants';
import {MainStackParams} from '../../routes';
import {ScreenType} from '../../types';
import {isIPhoneX} from '../../utils';

type ChatScreenNavigationProp = StackNavigationProp<MainStackParams, 'Chat'>;
interface ChatScreenProps {
  navigation: ChatScreenNavigationProp;
}
/*
 * Screen that displays all of the user's active conversations.
 */
const ChatScreen: React.FC<ChatScreenProps> = () => {
  const {channel, chatClient} = useContext(ChatContext);
  const tabbarHeight = useBottomTabBarHeight();

  const chatTheme = {
    messageList: {
      container: {
        backgroundColor: 'white',
      },
    },
  };

  const SendButton = () => (
    <View style={styles.submitButton}>
      <UpArrowIcon color={'white'} />
    </View>
  );
  // const insets = useSafeAreaInsets();

  return (
    <SafeAreaView
      style={[
        styles.container,
        // unable to figure out the padding issue, a hacky solution
        {paddingBottom: isIPhoneX() ? tabbarHeight + 20 : tabbarHeight + 50},
      ]}>
      <ChatHeader screenType={ScreenType.Chat} />
      <Chat client={chatClient} style={chatTheme}>
        <OverlayProvider
          FileSelectorIcon={Fragment}
          // topInset={insets.top}
          topInset={0}
          bottomInset={0}>
          <Channel
            channel={channel}
            keyboardVerticalOffset={0}
            OverlayReactionList={Fragment}
            messageActions={({copyMessage, deleteMessage}) => [
              copyMessage,
              deleteMessage,
            ]}
            Attachment={Fragment}>
            <MessageList onThreadSelect={() => {}} />
            {/* <MessageInput Input={ChatInput} /> */}
            <MessageInput SendButton={SendButton} />
          </Channel>
        </OverlayProvider>
      </Chat>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    backgroundColor: 'white',
    flex: 1,
  },

  submitButton: {
    height: 35,
    width: 35,
    backgroundColor: TAGG_LIGHT_BLUE,
    borderRadius: 999,
    justifyContent: 'center',
    alignItems: 'center',
    bottom: -5,
    alignSelf: 'flex-end',
  },
});

export default ChatScreen;
