import {RouteProp} from '@react-navigation/core';
import {useNavigation} from '@react-navigation/native';
import React, {useEffect, useState} from 'react';
import {Alert, SafeAreaView, StatusBar, StyleSheet, View} from 'react-native';
import {Text} from 'react-native-animatable';
import {TouchableOpacity} from 'react-native-gesture-handler';
import LinearGradient from 'react-native-linear-gradient';
import {useDispatch, useSelector} from 'react-redux';
import {BACKGROUND_GRADIENT_MAP} from '../../constants';
import {BADGE_DATA} from '../../constants/badges';
import {ERROR_BADGES_EXCEED_LIMIT} from '../../constants/strings';
import {MainStackParams} from '../../routes';
import {
  addBadgesService,
  getSuggestedPeopleProfile,
  updateBadgesService,
} from '../../services';
import {suggestedPeopleBadgesFinished} from '../../store/actions';
import {RootState} from '../../store/rootReducer';
import {BackgroundGradientType} from '../../types';
import {SCREEN_HEIGHT, StatusBarHeight} from '../../utils';
import BadgeList from './BadgeList';
import BadgeScreenHeader from './BadgeScreenHeader';

/**
 * Home Screen for displaying Tagg Badge Selections
 **/

type BadgeSelectionRouteProp = RouteProp<MainStackParams, 'BadgeSelection'>;

type BadgeSelectionProps = {
  route: BadgeSelectionRouteProp;
};

const BadgeSelection: React.FC<BadgeSelectionProps> = ({route}) => {
  const {editing} = route.params;
  const {
    user: {userId: loggedInUserId},
    profile: {university},
  } = useSelector((state: RootState) => state.user);
  const [selectedBadges, setSelectedBadges] = useState<string[]>([]);
  const dispatch = useDispatch();
  const navigation = useNavigation();

  // Loading badges data and extracting into a string []
  useEffect(() => {
    const loadData = async () => {
      const response = await getSuggestedPeopleProfile(loggedInUserId);
      if (response) {
        const data = response.badges;
        let extractedBadgeNames: string[] = [];
        data.forEach((badge) => {
          extractedBadgeNames.push(badge.name);
        });
        setSelectedBadges(extractedBadgeNames);
      }
    };
    if (editing) {
      loadData();
    }
  }, []);

  navigation.setOptions({
    headerRight: () => (
      <TouchableOpacity
        style={styles.rightButtonContainer}
        onPress={async () => {
          if (editing) {
            updateBadgesService(selectedBadges, university);
            navigation.navigate('UpdateSPPicture', {
              editing: true,
            });
          } else {
            if (selectedBadges.length !== 0) {
              const success = await addBadgesService(
                selectedBadges,
                university,
              );
              if (success) {
                dispatch(suggestedPeopleBadgesFinished());
                navigation.navigate('SuggestedPeople');
              }
            } else {
              dispatch(suggestedPeopleBadgesFinished());
              navigation.navigate('SuggestedPeople');
            }
          }
        }}>
        <Text style={styles.rightButton}>
          {selectedBadges.length !== 0 || editing ? 'Done' : 'Skip'}
        </Text>
      </TouchableOpacity>
    ),
  });

  const selectKey = (key: string) => {
    if (selectedBadges.includes(key)) {
      const selectedBadgesArray = [...selectedBadges];
      const itemIndex = selectedBadgesArray.indexOf(key);
      if (itemIndex > -1) {
        selectedBadgesArray.splice(itemIndex, 1);
      }
      setSelectedBadges(selectedBadgesArray);
    } else {
      if (selectedBadges.length < 5) {
        const selectedBadgesArray = [...selectedBadges, key];
        setSelectedBadges(selectedBadgesArray);
      } else {
        Alert.alert(ERROR_BADGES_EXCEED_LIMIT);
      }
    }
  };

  return (
    <LinearGradient
      colors={BACKGROUND_GRADIENT_MAP[BackgroundGradientType.Dark]}
      style={styles.container}>
      <StatusBar barStyle={'light-content'} />
      <SafeAreaView>
        <View style={styles.listContainer}>
          <BadgeScreenHeader {...{university}} />
          {/* filter not working, comment out for now */}
          {/* <SearchBar
            style={styles.searchBarStyle}
            onCancel={() => {}}
            top={Animated.useValue(0)}
          /> */}
          {university && (
            <BadgeList
              data={BADGE_DATA[university]}
              selectedBadges={selectedBadges}
              selectKey={selectKey}
            />
          )}
        </View>
      </SafeAreaView>
    </LinearGradient>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  searchBarStyle: {
    width: '95%',
    alignSelf: 'center',
    marginTop: SCREEN_HEIGHT * 0.05,
  },
  viewContainer: {marginTop: StatusBarHeight},
  listContainer: {marginTop: SCREEN_HEIGHT * 0.05},
  rightButtonContainer: {marginRight: 24},
  rightButton: {
    color: '#FFFFFF',
    fontWeight: 'bold',
    fontSize: 15,
    lineHeight: 18,
  },
  leftButtonContainer: {marginLeft: 24},
  leftButton: {
    color: '#FFFFFF',
    fontWeight: '500',
    fontSize: 15,
    lineHeight: 18,
  },
});

export default BadgeSelection;
