import React, {useState} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import {useSelector} from 'react-redux';
import {UniversityIcon} from '.';
import {PROFILE_CUTOUT_TOP_Y} from '../../constants';
import {RootState} from '../../store/rootreducer';
import {ScreenType} from '../../types';
import {normalize} from '../../utils';
import Avatar from './Avatar';
import FriendsCount from './FriendsCount';
import ProfileMoreInfoDrawer from './ProfileMoreInfoDrawer';

type ProfileHeaderProps = {
  userXId: string | undefined;
  screenType: ScreenType;
  isBlocked: boolean;
  handleBlockUnblock: () => void;
};

const ProfileHeader: React.FC<ProfileHeaderProps> = ({
  userXId,
  screenType,
  isBlocked,
  handleBlockUnblock,
}) => {
  const {
    profile: {name = '', university_class = 2021} = {},
    user: {username: userXName = ''},
  } = userXId
    ? useSelector((state: RootState) => state.userX[screenType][userXId])
    : useSelector((state: RootState) => state.user);
  const [drawerVisible, setDrawerVisible] = useState(false);
  const [firstName, lastName] = [...name.split(' ')];
  return (
    <View style={styles.container}>
      <ProfileMoreInfoDrawer
        isOpen={drawerVisible}
        isBlocked={isBlocked}
        handleBlockUnblock={handleBlockUnblock}
        userXId={userXId}
        userXName={userXName}
        setIsOpen={setDrawerVisible}
      />

      <View style={styles.row}>
        <Avatar
          style={styles.avatar}
          userXId={userXId}
          screenType={screenType}
        />
        <View style={styles.header}>
          {name.length <= 16 ? (
            <Text style={styles.name}>{name}</Text>
          ) : (
            <View>
              <Text style={styles.name}>{firstName}</Text>
              <Text style={styles.name}>{lastName}</Text>
            </View>
          )}
          <View style={styles.friendsAndUniversity}>
            <FriendsCount screenType={screenType} userXId={userXId} />
            <UniversityIcon
              university="brown"
              university_class={university_class}
            />
          </View>
        </View>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    top: PROFILE_CUTOUT_TOP_Y * 1.02,
    width: '100%',
    position: 'absolute',
  },
  row: {
    flexDirection: 'row',
  },
  header: {
    flexDirection: 'column',
    justifyContent: 'space-evenly',
    alignItems: 'center',
    marginRight: '15%',
    marginLeft: '5%',
    flex: 1,
  },
  avatar: {
    marginLeft: '3%',
    top: '-8%',
  },
  name: {
    fontSize: normalize(17),
    fontWeight: '500',
    alignSelf: 'center',
  },
  friendsAndUniversity: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-evenly',
    width: '100%',
    height: 50,
  },
});

export default ProfileHeader;
