import React from 'react';
import {
  StyleSheet,
  View,
  TouchableOpacity,
  Text,
  StatusBar,
  KeyboardAvoidingView,
  Platform,
  Alert,
} from 'react-native';
import {
  Background,
  RegistrationWizard,
  LinkSocialMedia,
} from '../../components';
import {LinkerType} from 'src/types';
import {SOCIAL_LIST} from '../../constants/';
import {OnboardingStackParams, AuthContext} from '../../routes';
import {RouteProp} from '@react-navigation/native';
import {StackNavigationProp} from '@react-navigation/stack';

/**
 * Social Media Screen for displaying social media linkers
 */

type SocialMediaRouteProps = RouteProp<OnboardingStackParams, 'SocialMedia'>;

type SocialMediaNavigationProp = StackNavigationProp<
  OnboardingStackParams,
  'SocialMedia'
>;

interface SocialMediaProps {
  route: SocialMediaRouteProps;
  navigation: SocialMediaNavigationProp;
}

const SocialMedia: React.FC<SocialMediaProps> = ({route, navigation}) => {
  const {userId, username} = route.params;
  const linkers: Array<LinkerType> = [];
  const [state, setState] = React.useState({
    showMore: false,
  });

  /**
   * login: determines if user successully created an account to
   * navigate to home and display main tab navigation bar
   */
  const {login} = React.useContext(AuthContext);

  //   let numSocials: Number = state.showMore ? 9 : 3;

  for (let i = 0; i < SOCIAL_LIST.length; i++) {
    let linker: LinkerType = {
      label: SOCIAL_LIST[i],
    };
    linkers.push(linker);
  }

  /**
   * Just commenting this out, in case we need it in the future
   */
  //   const handleShowPress = () => {
  //     setState({
  //       ...state,
  //       showMore: !state.showMore,
  //     });
  //   };

  const handleLogin = () => {
    try {
      login(userId, username);
    } catch (error) {
      console.log(error);
      Alert.alert('There was a problem logging you in');
    }
  };

  return (
    <Background style={styles.container}>
      <StatusBar barStyle="light-content" />
      <RegistrationWizard style={styles.wizard} step="seven" />
      <KeyboardAvoidingView
        behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
        style={styles.container}>
        <View style={{marginBottom: '30%'}}>
          <Text style={styles.header}>SOCIAL MEDIA</Text>
          <Text style={styles.subtext}>
            Select the social media you want to add
          </Text>
        </View>
        <View style={styles.linkerContainer}>
          {linkers.map((linker, index) => (
            <LinkSocialMedia key={index} social={linker} />
          ))}
        </View>
      </KeyboardAvoidingView>
      <TouchableOpacity onPress={handleLogin} style={styles.loginButton}>
        <Text style={styles.loginButtonLabel}>Login</Text>
      </TouchableOpacity>
    </Background>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  linkerContainer: {
    position: 'relative',
    bottom: '15%',
    flexDirection: 'row',
    height: '25%',
    flexWrap: 'wrap',
    justifyContent: 'center',
    alignContent: 'center',
    marginBottom: '10%',
  },
  header: {
    color: '#fff',
    fontSize: 22,
    fontWeight: '600',
    textAlign: 'center',
    marginBottom: '4%',
    marginHorizontal: '10%',
  },
  subtext: {
    color: '#fff',
    fontSize: 14,
    fontWeight: '600',
    textAlign: 'center',
    marginBottom: '35%',
    marginHorizontal: '10%',
  },
  // show: {
  //   borderColor: '#fff',
  //   borderWidth: 1,
  //   borderRadius: 3,
  //   paddingHorizontal: '2%',
  //   paddingVertical: '1%',
  //   marginVertical: '3%',
  //   marginLeft: '65%',
  // },
  wizard: {
    ...Platform.select({
      ios: {
        top: 50,
      },
      android: {
        bottom: 40,
      },
    }),
  },
  loginButton: {
    backgroundColor: '#8F01FF',
    justifyContent: 'center',
    alignItems: 'center',
    width: 150,
    height: 40,
    borderRadius: 5,
    borderWidth: 1,
    borderColor: '#8F01FF',
    marginBottom: '15%',
  },
  loginButtonLabel: {
    fontSize: 16,
    fontWeight: '500',
    color: '#ddd',
  },
  form: {
    alignItems: 'center',
    justifyContent: 'flex-start',
    flex: 3,
  },
});

export default SocialMedia;
