import { useNavigation, useRoute } from '@react-navigation/native';
import type { NativeStackNavigationProp } from '@react-navigation/native-stack';
import React, { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { Dimensions, StyleSheet, TouchableOpacity, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';

import HomeIcon from 'svg/HomeIcon';
import Passport from 'svg/Passport';
import StatusIcon from 'svg/StatusIcon';
import User from 'svg/User';
import Wallet from 'svg/Wallet';
import AppText from './AppText';

const { width } = Dimensions.get('window');
const isSmallScreen = width < 360;

type RouteName = 'Home' | 'Status' | 'Passports' | 'Wallet' | 'Profile';

const Navigation = () => {
  const navigation = useNavigation<NativeStackNavigationProp<any>>();
  const { bottom } = useSafeAreaInsets();
  const route = useRoute();
  const { t } = useTranslation();
  const links = useMemo(
    () => [
      { label: 'Asosiy', icon: HomeIcon, route: 'Home' as RouteName },
      { label: 'Status', icon: StatusIcon, route: 'Status' as RouteName },
      { label: 'Passportlar', icon: Passport, route: 'Passports' as RouteName },
      { label: "To'lov", icon: Wallet, route: 'Wallet' as RouteName },
      { label: 'Profil', icon: User, route: 'Profile' as RouteName },
    ],
    [],
  );

  const handleNavigation = useCallback(
    (routeName: RouteName) => {
      if (route.name === routeName) return;

      navigation.replace(routeName);
    },
    [navigation, route.name],
  );

  const containerStyle = useMemo(() => [styles.container], []);

  const renderNavItem = useCallback(
    ({ label, icon: Icon, route: routeName }: any) => {
      const isActive = route.name === routeName;
      const color = isActive ? '#28A7E8' : '#6C6C6C';

      const iconSize = isSmallScreen ? 24 : 35;
      const fontSize = isSmallScreen ? 10 : 10;

      return (
        <TouchableOpacity
          key={routeName}
          style={styles.links}
          onPress={() => handleNavigation(routeName)}
          activeOpacity={0.7}
        >
          <Icon color={color} width={iconSize} height={iconSize} />
          <AppText
            style={{
              color,
              fontWeight: '500',
              fontSize,
            }}
          >
            {t(label)}
          </AppText>
        </TouchableOpacity>
      );
    },
    [route.name, handleNavigation, t],
  );

  const navItems = useMemo(
    () => links.map(renderNavItem),
    [links, renderNavItem],
  );

  return (
    <View style={[styles.wrapper, { marginBottom: bottom }]}>
      <View style={containerStyle}>{navItems}</View>
    </View>
  );
};

const styles = StyleSheet.create({
  wrapper: {
    bottom: 0,
    width: '100%',
    height: 70,
    backgroundColor: '#fff',
  },
  container: {
    width: '100%',
    height: '100%',
    backgroundColor: '#fff',
    borderColor: '#eee',
    borderWidth: 1,
    borderTopLeftRadius: 20,
    borderTopRightRadius: 20,
    flexDirection: 'row',
    justifyContent: 'space-around',
  },
  links: {
    justifyContent: 'center',
    alignItems: 'center',
  },
});

export default Navigation;
