import React from 'react';
import { createStackNavigator, StackNavigationOptions } from '@react-navigation/stack';
import { RouteProp } from '@react-navigation/native';
import HomeScreen from './Home/HomeScreen';
import DeviceList from './device/deviceList';
import BindIndex from './bind/bind_index';
import SnBind from './bind/sn_bind';
import ConfirmBind from './bind/ConfirmBind';
import DeviceMap from './device/deviceMap';
import DeviceSet from './device/deviceSet'; 
import UnlockSetting from './device/UnlockSetting';
import BleDistance from './device/BleDistance';
import DeviceShare from './device/DeviceShare';
import AddShare from './device/AddShare';
// import BleBind from './bind/ble_bind';
import { getFocusedRouteNameFromRoute } from '@react-navigation/native';

type RootStackParamList = {
  Home: undefined;
  DeviceList: undefined;
  BindIndex: undefined;
  SnBind: undefined;
  ConfirmBind: undefined;
  DeviceMap: undefined;
  DeviceSet: undefined;
  BleDistance: undefined;
  UnlockSetting: undefined;
  DeviceShare: undefined;
  AddShare: undefined;
};

const Stack = createStackNavigator<RootStackParamList>();

// 修改类型定义
const createScreenOptions = (title: string): StackNavigationOptions => {
  return {
    title,
    headerTitleAlign: 'center',
    headerStyle: {
      backgroundColor: '#F3FCFF',
    },
    headerTitleStyle: {
      fontSize: 18,
      fontWeight: '500',
    },
  };
};

type Props = {
  navigation: any;
  route: any;
};

export default function HomeStackNavigator({ navigation, route }: Props) {
  React.useEffect(() => {
    const routeName = getFocusedRouteNameFromRoute(route) ?? 'Home';
    const hideTabBarRoutes = ['DeviceList', 'BindIndex', 'SnBind', 'BleBind', 'ConfirmBind', 'DeviceMap', 'DeviceSet', 'UnlockSetting', 'BleDistance', 'DeviceShare']; // 添加新的路由名
    const shouldHideTabBar = hideTabBarRoutes.includes(routeName);
    
    navigation.getParent()?.setOptions({
      tabBarStyle: shouldHideTabBar ? { display: 'none' } : undefined
    });
  }, [navigation, route]);


  return (
    <Stack.Navigator>
      <Stack.Screen 
        name="Home" 
        component={HomeScreen}  
        options={{
          headerShown: false,
        }}
      />
      <Stack.Screen 
        name="DeviceList" 
        component={DeviceList}
        options={createScreenOptions('设备列表')}
      />
      <Stack.Screen 
        name="BindIndex" 
        component={BindIndex}
        options={createScreenOptions('扫码绑定')}
      />
      <Stack.Screen 
        name="SnBind" 
        component={SnBind}
        options={createScreenOptions('手动绑车')}
    />
    <Stack.Screen 
        name="ConfirmBind" 
        component={ConfirmBind}
        options={createScreenOptions('确认绑定')}
      />
        <Stack.Screen 
        name="DeviceMap" 
        component={DeviceMap}
        options={createScreenOptions('设备位置')}
      />  
      <Stack.Screen 
        name="DeviceSet" 
        component={DeviceSet}
        options={createScreenOptions('车辆设置')}
      />  
      <Stack.Screen 
        name="UnlockSetting" 
        component={UnlockSetting}
        options={createScreenOptions('无感解锁')}
      />  
      <Stack.Screen 
        name="BleDistance" 
        component={BleDistance}
        options={createScreenOptions('蓝牙距离')}
      />  
      <Stack.Screen 
        name="DeviceShare" 
        component={DeviceShare}
        options={createScreenOptions('车辆共享')}
      />  
      <Stack.Screen 
        name="AddShare" 
        component={AddShare}
        options={createScreenOptions('添加共享人')}
      />  
    </Stack.Navigator>
  );
}