import { Tabs } from 'expo-router'
import React from 'react'
import { StyleSheet, View, Text, Platform } from 'react-native'
import { LinearGradient } from 'expo-linear-gradient'
import { useTranslation } from 'react-i18next'
import { useSafeAreaInsets } from 'react-native-safe-area-context'

import { HomeIcon, MessageIcon, MyIcon, VideoIcon } from '@/components/icon'

const TAB_BAR_HEIGHT = 60

export default function TabLayout() {
  const { t } = useTranslation()
  const insets = useSafeAreaInsets()
  // Android 不需要额外的底部安全区域，iOS 和 Web 需要
  const bottomInset = Platform.OS === 'android' ? 0 : insets.bottom

  return (
    <Tabs
      screenOptions={{
        tabBarActiveTintColor: '#FFFFFF',
        tabBarInactiveTintColor: '#FFFFFF',
        headerShown: false,
        tabBarHideOnKeyboard: true,
        tabBarShowLabel: true,
        tabBarStyle: {
          position: 'absolute',
          backgroundColor: '#1C1E22',
          height: TAB_BAR_HEIGHT + bottomInset,
          paddingTop: 4,
          paddingBottom: bottomInset,
          paddingLeft: 4,
          paddingRight: 4,
          borderTopWidth: 0,
          elevation: 0,
          shadowOpacity: 0,
          borderTopColor: 'transparent',
        },
        tabBarLabelStyle: {
          color: '#FFFFFF',
          fontSize: 10,
          fontWeight: '400',
        },
        tabBarBackground: () => (
          <View style={{ flex: 1, backgroundColor: '#1C1E22' }} />
        ),
      }}
    >
      <Tabs.Screen
        name="index"
        options={{
          title: t('tabs.home'),
          tabBarLabel: ({ focused }: { focused: boolean }) => (focused ? null : <Text style={styles.tabLabel}>{t('tabs.home')}</Text>),
          tabBarIcon: ({ focused }: { focused: boolean }) => {
            if (focused) {
              return (
                <LinearGradient colors={['#9966FF', '#FF6699', '#FF9966']} start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }} style={styles.iconContainer}>
                  <HomeIcon />
                </LinearGradient>
              )
            }
            return (
              <View style={{ opacity: 0.6 }}>
                <HomeIcon />
              </View>
            )
          },
        }}
      />
      <Tabs.Screen
        name="video"
        options={{
          title: t('tabs.video'),
          tabBarLabel: ({ focused }: { focused: boolean }) => (focused ? null : <Text style={styles.tabLabel}>{t('tabs.video')}</Text>),
          tabBarIcon: ({ focused }: { focused: boolean }) => {
            if (focused) {
              return (
                <LinearGradient colors={['#9966FF', '#FF6699', '#FF9966']} start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }} style={styles.iconContainer}>
                  <VideoIcon />
                </LinearGradient>
              )
            }
            return (
              <View style={{ opacity: 0.6 }}>
                <VideoIcon />
              </View>
            )
          },
        }}
      />
      <Tabs.Screen
        name="message"
        options={{
          title: t('tabs.message'),
          tabBarLabel: ({ focused }: { focused: boolean }) => (focused ? null : <Text style={styles.tabLabel}>{t('tabs.message')}</Text>),
          tabBarIcon: ({ focused }: { focused: boolean }) => {
            if (focused) {
              return (
                <LinearGradient colors={['#9966FF', '#FF6699', '#FF9966']} start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }} style={styles.iconContainer}>
                  <MessageIcon />
                </LinearGradient>
              )
            }
            return (
              <View style={{ opacity: focused ? 1 : 0.6 }}>
                <MessageIcon />
              </View>
            )
          },
        }}
      />
      <Tabs.Screen
        name="my"
        options={{
          title: t('tabs.my'),
          tabBarLabel: ({ focused }: { focused: boolean }) => (focused ? null : <Text style={styles.tabLabel}>{t('tabs.my')}</Text>),
          tabBarIcon: ({ focused }: { focused: boolean }) => {
            if (focused) {
              return (
                <LinearGradient colors={['#9966FF', '#FF6699', '#FF9966']} start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }} style={styles.iconContainer}>
                  <MyIcon />
                </LinearGradient>
              )
            }
            return (
              <View style={{ opacity: focused ? 1 : 0.6 }}>
                <MyIcon />
              </View>
            )
          },
        }}
      />
    </Tabs>
  )
}

const styles = StyleSheet.create({
  iconContainer: {
    width: 87,
    height: 41,
    justifyContent: 'center',
    alignItems: 'center',
    borderRadius: 100,
  },
  tabLabel: {
    fontSize: 10,
    color: '#FFFFFF',
    textAlign: 'center',
  },
})
