import React from 'react';
import { TouchableOpacity, View } from 'react-native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { DrawerNavigationProp } from '@react-navigation/drawer';
import Icon from 'react-native-vector-icons/Ionicons';
import { route, RouteParams } from '@utils';
import {
  DashboardScreen,
  AddLeadScreen,
  CustomersScreen,
  ProfileScreen,
} from '@features';
import { LeadsStack } from './leadsStack';
import { useTheme } from '@theme';
import { getStyles } from './tabStack.styles';

export type TabStackParamList = Pick<
  RouteParams,
  'dashboard' | 'leads' | 'addLead' | 'customers' | 'profile'
>;

const Tab = createBottomTabNavigator<TabStackParamList>();

export const TabStack = () => {
  const { theme: colors } = useTheme();
  const styles = getStyles(colors);

  return (
    <Tab.Navigator
      screenOptions={({ route: tabRoute }) => ({
        tabBarIcon: ({ focused, color }) => {
          if (tabRoute.name === route.addLead) {
            return (
              <View style={styles.addLeadButtonWrapper}>
                <Icon name="add" size={24} color="#FFFFFF" />
              </View>
            );
          }

          let iconName = 'square';
          if (tabRoute.name === route.dashboard) {
            iconName = focused ? 'grid' : 'grid-outline';
          } else if (tabRoute.name === route.leads) {
            iconName = focused ? 'funnel' : 'funnel-outline';
          } else if (tabRoute.name === route.customers) {
            iconName = focused ? 'people' : 'people-outline';
          } else if (tabRoute.name === route.profile) {
            iconName = focused ? 'person' : 'person-outline';
          }

          return (
            <View style={[styles.iconContainer, focused && styles.iconContainerFocused]}>
              <Icon name={iconName} size={20} color={color} />
            </View>
          );
        },
        tabBarActiveTintColor: colors.icon,
        tabBarInactiveTintColor: colors.textMuted,
        tabBarStyle: styles.tabBar,
        tabBarLabelStyle: styles.tabBarLabel,
        headerStyle: styles.header,
        headerTitleStyle: styles.headerTitle,
        headerTitleAlign: 'center',
      })}>
      <Tab.Screen
        name={route.dashboard}
        component={DashboardScreen}
        options={({ navigation }) => ({
          tabBarLabel: 'Dashboard',
          headerTitle: 'Convex CRM',
          headerLeft: () => (
            <TouchableOpacity
              style={styles.drawerButton}
              activeOpacity={0.7}
              onPress={() => {
                const parentNav = navigation.getParent<DrawerNavigationProp<any>>();
                if (parentNav) {
                  parentNav.openDrawer();
                } else {
                  (navigation as any).openDrawer?.();
                }
              }}>
              <Icon name="menu-outline" size={20} color={colors.text} />
            </TouchableOpacity>
          ),
        })}
      />
      <Tab.Screen
        name={route.leads}
        component={LeadsStack}
        options={{
          tabBarLabel: 'Leads',
          headerShown: false,
        }}
      />
      <Tab.Screen
        name={route.addLead}
        component={AddLeadScreen}
        options={{
          tabBarLabel: 'Add Lead',
          headerTitle: 'Add Lead',
        }}
      />
      <Tab.Screen
        name={route.customers}
        component={CustomersScreen}
        options={{
          tabBarLabel: 'Customers',
          headerTitle: 'Customers',
        }}
      />
      <Tab.Screen
        name={route.profile}
        component={ProfileScreen}
        options={{
          tabBarLabel: 'Profile',
          headerTitle: 'Profile',
        }}
      />
    </Tab.Navigator>
  );
};
