import {useBottomTabBarHeight} from '@react-navigation/bottom-tabs';
import {StackNavigationProp} from '@react-navigation/stack';
import React, {useContext, useEffect} from 'react';
import {Image, StyleSheet, View} from 'react-native';
import {SafeAreaView, useSafeAreaInsets} from 'react-native-safe-area-context';
import {
  Channel,
  Chat,
  MessageInput,
  MessageList,
  useAttachmentPickerContext,
} from 'stream-chat-react-native';
import {ChatContext} from '../../App';
import {ChatHeader, ChatInput, TabsGradient} from '../../components';
import {MainStackParams} from '../../routes';
import {ScreenType} from '../../types';
import {HeaderHeight, 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 {setTopInset} = useAttachmentPickerContext();
  const insets = useSafeAreaInsets();

  const chatTheme = {
    messageList: {
      container: {
        backgroundColor: 'white',
      },
    },
    messageInput: {
      container: {
        backgroundColor: '#f8f8f8',
        height: 70,
      },
    },
    avatar: {
      container: {
        borderRadius: 20,
        width: 40,
        height: 40,
      },
    },
    messageSimple: {
      avatarWrapper: {
        container: {
          height: '100%',
          top: -8,
          right: -25,
          zIndex: 1,
        },
      },
      container: {
        paddingTop: 8,
      },
      card: {
        container: {
          backgroundColor: 'lightgreen',
          borderWidth: 2,
          borderColor: 'black',
        },
      },
      content: {
        container: {},
        containerInner: {
          backgroundColor: '#DEE6F4',
          borderColor: 'transparent',
          borderBottomLeftRadius: 10,
          borderTopLeftRadius: 10,
          borderBottomRightRadius: 10,
          borderTopRightRadius: 10,
        },
      },
    },
  };

  const loggedInUsersMessageTheme = {
    messageSimple: {
      content: {
        containerInner: {
          backgroundColor: '#DEE6F4',
        },
        container: {
          left: 0,
        },
      },
    },
  };

  useEffect(() => {
    setTopInset(insets.top + HeaderHeight);
  });

  const CustomTypingIndicator = () => {
    return (
      <View style={styles.typingIndicatorContainer}>
        <Image
          source={require('../../assets/gifs/loading-animation.gif')}
          style={{width: 88, height: 49}}
        />
      </View>
    );
  };

  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}>
        <Channel
          channel={channel}
          keyboardVerticalOffset={0}
          OverlayReactionList={() => null}
          messageActions={({copyMessage, deleteMessage}) => [
            copyMessage,
            deleteMessage,
          ]}
          TypingIndicator={CustomTypingIndicator}
          myMessageTheme={loggedInUsersMessageTheme}>
          <MessageList onThreadSelect={() => {}} />
          {/* <MessageInput /> */}
          <MessageInput Input={ChatInput} />
        </Channel>
      </Chat>
      <TabsGradient />
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    backgroundColor: 'white',
    flex: 1,
  },
  typingIndicatorContainer: {
    backgroundColor: '#E4F0F2',
    width: 88,
    height: 32,
    borderRadius: 10,
    marginBottom: 10,
    marginLeft: 10,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
  },
});

export default ChatScreen;
