import {RouteProp} from '@react-navigation/core';
import {useNavigation} from '@react-navigation/native';
import React, {useEffect, useRef, useState} from 'react';
import {
  Image,
  Keyboard,
  KeyboardAvoidingView,
  Platform,
  StyleSheet,
  TouchableWithoutFeedback,
  View,
} from 'react-native';
import {Button} from 'react-native-elements';
import {MainStackParams} from 'src/routes';
import {
  CaptionScreenHeader,
  MomentTags,
  SearchBackground,
} from '../../components';
import {TagFriendsFooter} from '../../components/moments';
import {TAGG_LIGHT_BLUE_2} from '../../constants';
import {MomentTagType} from '../../types';
import {SCREEN_WIDTH, StatusBarHeight} from '../../utils';

type TagFriendsScreenRouteProps = RouteProp<
  MainStackParams,
  'TagFriendsScreen'
>;
interface TagFriendsScreenProps {
  route: TagFriendsScreenRouteProps;
}
const TagFriendsScreen: React.FC<TagFriendsScreenProps> = ({route}) => {
  const {imagePath, selectedTags} = route.params;
  const navigation = useNavigation();
  const imageRef = useRef(null);
  const [tags, setTags] = useState<MomentTagType[]>([]);

  /*
   * Update list of tagged users from route params
   */
  useEffect(() => {
    setTags(selectedTags ? selectedTags : []);
  }, [selectedTags]);

  /*
   * Navigate back to Tag Users Screen, send selected users
   */
  const handleDone = () => {
    navigation.navigate('CaptionScreen', {
      ...route.params,
      selectedTags: tags,
    });
  };

  return (
    <SearchBackground>
      <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
        <KeyboardAvoidingView
          behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
          style={styles.flex}>
          <View style={styles.contentContainer}>
            <View style={styles.buttonsContainer}>
              <Button
                title="Cancel"
                buttonStyle={styles.button}
                onPress={() => navigation.goBack()}
              />
              <Button
                title="Done"
                titleStyle={styles.shareButtonTitle}
                buttonStyle={styles.button}
                onPress={handleDone}
              />
            </View>
            <CaptionScreenHeader
              style={styles.header}
              title={'Tap on photo to tag friends!'}
            />
            <TouchableWithoutFeedback
              onPress={() =>
                navigation.navigate('TagSelectionScreen', {
                  selectedTags: tags,
                })
              }>
              <Image
                ref={imageRef}
                style={styles.image}
                source={{uri: imagePath}}
                resizeMode={'contain'}
              />
            </TouchableWithoutFeedback>
            {tags.length !== 0 && (
              <MomentTags
                tags={tags}
                setTags={setTags}
                editing={true}
                imageRef={imageRef}
                deleteFromList={(user) =>
                  setTags(tags.filter((tag) => tag.user.id !== user.id))
                }
              />
            )}
            <View style={styles.footerContainer}>
              <TagFriendsFooter tags={tags} setTags={setTags} />
            </View>
          </View>
        </KeyboardAvoidingView>
      </TouchableWithoutFeedback>
    </SearchBackground>
  );
};

const styles = StyleSheet.create({
  contentContainer: {
    paddingTop: StatusBarHeight,
    justifyContent: 'flex-end',
  },
  buttonsContainer: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    marginLeft: '5%',
    marginRight: '5%',
  },
  button: {
    backgroundColor: 'transparent',
  },
  shareButtonTitle: {
    fontWeight: 'bold',
    color: TAGG_LIGHT_BLUE_2,
  },
  header: {
    marginVertical: 20,
  },
  image: {
    position: 'relative',
    width: SCREEN_WIDTH,
    aspectRatio: 1,
    marginBottom: '3%',
  },
  text: {
    position: 'relative',
    backgroundColor: 'white',
    width: '100%',
    paddingHorizontal: '2%',
    paddingVertical: '1%',
    height: 60,
  },
  flex: {
    flex: 1,
  },
  footerContainer: {marginHorizontal: '5%', marginTop: '3%'},
});

export default TagFriendsScreen;
