import React from 'react';
import { View, Text, FlatList, TouchableOpacity, StyleSheet, Dimensions } from 'react-native';
import { useNavigation } from '@react-navigation/native';

import { Colors } from '../../../theme';
import { NAVIGATION_PAGES } from 'src/types';
import { StoreType, storage } from 'src/storage';

import UserGroupIcon from '../../../../assets/icons/user-group.svg';
import CrownIcon from '../../../../assets/icons/crown.svg';
import IDCardIcon from '../../../../assets/icons/id-card.svg';
import StreetPeopleIcon from '../../../../assets/icons/street-view.svg';
import ChartPieIcon from '../../../../assets/icons/chart-pie.svg';
import MemoriamIcon from '../../../../assets/icons/monument.svg';
import ClockIcon from '../../../../assets/icons/clock.svg';
import TrophyIcon from '../../../../assets/icons/trophy.svg';
import InfoIcon from 'assets/icons/info-solid.svg';
import FriendsIcon from 'assets/icons/friends.svg';
import { getFontSize } from 'src/utils';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useFriendsNotificationsStore } from 'src/stores/friendsNotificationsStore';
import MessagesDot from 'src/components/MessagesDot';

const TravellersScreen = () => {
  const navigation = useNavigation();
  const token = storage.get('token', StoreType.STRING);
  const insets = useSafeAreaInsets();
  const isNotificationActive = useFriendsNotificationsStore((state) => state.isNotificationActive);

  const buttons = [
    { label: 'Master Ranking', icon: UserGroupIcon, page: NAVIGATION_PAGES.MASTER_RANKING },
    { label: 'UN Masters', icon: CrownIcon, page: NAVIGATION_PAGES.UN_MASTERS },
    { label: 'LPI Ranking', icon: IDCardIcon, page: NAVIGATION_PAGES.LPI_RANKING },
    { label: 'Series Ranking', icon: StreetPeopleIcon, page: NAVIGATION_PAGES.SERIES_RANKING },
    { label: 'Statistics', icon: ChartPieIcon, page: NAVIGATION_PAGES.STATISTICS },
    { label: 'In Memoriam', icon: MemoriamIcon, page: NAVIGATION_PAGES.IN_MEMORIAM },
    { label: 'In History', icon: ClockIcon, page: NAVIGATION_PAGES.IN_HISTORY },
    { label: 'Triumphs', icon: TrophyIcon, page: NAVIGATION_PAGES.TRIUMPHS }
  ];

  if (token) {
    buttons.push({ label: 'Friends', icon: FriendsIcon, page: NAVIGATION_PAGES.MY_FRIENDS });
  }

  const renderItem = ({ item }: { item: { label: string; icon: any; page: string } }) => (
    <TouchableOpacity
      style={{ alignItems: 'center' }}
      onPress={() => navigation.navigate(item.page as never)}
    >
      <View style={styles.button}>
        <item.icon
          fill={Colors.ORANGE}
          width={Dimensions.get('window').width < 380 ? 105 : 110}
          height={32}
        />
        <Text style={styles.label}>{item.label}</Text>
      </View>
      {item.label === 'Friends' && isNotificationActive > 0 && (
        <MessagesDot messagesCount={isNotificationActive} right={'15%'} top={'10%'} />
      )}
    </TouchableOpacity>
  );

  return (
    <View style={{ paddingTop: insets.top, marginLeft: '5%', marginRight: '5%', height: '100%' }}>
      <View style={styles.header}>
        <View style={{ width: 30 }} />
        <Text style={styles.title}>Nomads</Text>
        <TouchableOpacity
          onPress={() => navigation.navigate(NAVIGATION_PAGES.JOIN_INFO as never)}
          style={{ width: 30 }}
        >
          <InfoIcon />
        </TouchableOpacity>
      </View>

      <FlatList
        data={buttons}
        renderItem={renderItem}
        keyExtractor={(item, index) => 'key' + index}
        numColumns={2}
        contentContainerStyle={styles.container}
        style={{ flex: 1 }}
        columnWrapperStyle={{ justifyContent: 'space-between' }}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    justifyContent: 'space-between',
    paddingHorizontal: Dimensions.get('window').width < 380 ? '5%' : 24,
    paddingVertical: 8,
    gap: 32,
    width: '100%'
  },
  button: {
    alignItems: 'center',
    justifyContent: 'center',
    paddingVertical: 16,
    paddingHorizontal: 8,
    backgroundColor: Colors.FILL_LIGHT,
    borderRadius: 16,
    gap: 12
  },
  label: {
    color: Colors.DARK_BLUE,
    fontSize: 13,
    fontWeight: 'bold'
  },
  title: { color: Colors.DARK_BLUE, fontFamily: 'redhat-700', fontSize: getFontSize(14) },
  header: {
    alignItems: 'center',
    paddingVertical: 16,
    flexDirection: 'row',
    justifyContent: 'space-between'
  }
});

export interface Ranking {
  user_id: number;
  score_dare: number;
  score_nm: number;
  score_un: number;
  score_unp: number;
  score_tcc: number;
  score_deep: number;
  score_whs: number | null;
  score_kye: number;
  score_tbt: number;
  score_yes: number;
  score_slow: number;
  rank_tbt: number;
  avatar: string | null;
  first_name: string;
  last_name: string;
  age: number;
  flag1: string;
  flag2: string | null;
  badge_1281: number;
  badge_un: number;
  badge_un_25: number;
  badge_un_50: number;
  badge_un_75: number;
  badge_un_100: number;
  badge_un_150: number;
  badge_un_193: number;
  badge_premium: number;
  badge_supreme: number;
  badge_tbt: number;
  badge_offline: number;
  patreon: number;
  country: string;
  auth: number;
  rank: number;
  country_rank: number;
  dod: number;
  ukr: number;
  badges: number;
  arrow_nm: number;
  arrow_un: number;
  arrow_unp: number;
  arrow_dare: number;
  arrow_yes: number;
  arrow_whs: number;
  arrow_tcc: number;
  arrow_tbt: number;
  arrow_slow: number;
  arrow_kye: number;
  displayIndex: number;
  homebase: number;
}

export default TravellersScreen;
