/**
 * 导航状态管理 Hook
 * 管理导航相关状态，如当前路由、导航历史等
 */

import { useState, useCallback, useEffect } from 'react';
import { useNavigationState as useRNNavigationState, Route } from '@react-navigation/native';
import type { RootStackParamList } from '../../../navigation/types';

type TabName = 'HomeTab' | 'MessageTab' | 'ScheduleTab' | 'ProfileTab';

interface NavigationState {
  currentTab: TabName;
  isCollapsed: boolean;
  isReady: boolean;
}

interface NavigationActions {
  setCurrentTab: (tab: TabName) => void;
  toggleCollapse: () => void;
  setIsReady: (ready: boolean) => void;
}

/**
 * 导航状态管理 Hook
 */
export function useNavigationState(): NavigationState & NavigationActions {
  const [currentTab, setCurrentTab] = useState<TabName>('HomeTab');
  const [isCollapsed, setIsCollapsed] = useState(false);
  const [isReady, setIsReady] = useState(false);

  const toggleCollapse = useCallback(() => {
    setIsCollapsed(prev => !prev);
  }, []);

  return {
    currentTab,
    isCollapsed,
    isReady,
    setCurrentTab,
    toggleCollapse,
    setIsReady,
  };
}

/**
 * 获取当前路由名称
 */
export function useCurrentRouteName(): string | null {
  const routeName = useRNNavigationState(state => state?.routes[state.index]?.name ?? null);
  return routeName;
}

/**
 * 检查是否在指定路由
 */
export function useIsRoute(routeName: keyof RootStackParamList): boolean {
  const currentRoute = useCurrentRouteName();
  return currentRoute === routeName;
}
