import React, { useEffect, useState } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { Spin } from 'antd'
import type { AppDispatch } from '../store/store'
import { 
  login, 
  verifyToken, 
  restoreAuthState, 
  clearError,
  clearAuthState,
  selectIsAuthenticated,
  selectAuthLoading,
  selectAuthError,
  selectToken
} from '../store/slices/authSlice'
import LoginPage from './LoginPage'
import InstallPage from './InstallPage'
import apiClient from '../services/apiClient'

interface AuthGuardProps {
  children: React.ReactNode
}

/**
 * 认证守卫组件
 * 负责验证用户登录状态，未登录时显示登录页面
 */
const AuthGuard: React.FC<AuthGuardProps> = ({ children }) => {
  const dispatch = useDispatch<AppDispatch>()
  const isAuthenticated = useSelector(selectIsAuthenticated)
  const authLoading = useSelector(selectAuthLoading)
  const authError = useSelector(selectAuthError)
  const token = useSelector(selectToken)
  
  const [isInitializing, setIsInitializing] = useState(true)
  const [loginError, setLoginError] = useState<string | null>(null)
  const [systemInitialized, setSystemInitialized] = useState<boolean | null>(null)

  // 调试：监听认证状态变化
  useEffect(() => {
    console.log('[AuthGuard] Auth state changed:', { 
      isAuthenticated, 
      authLoading, 
      token: token ? '***' : null,
      isInitializing,
      systemInitialized
    })
  }, [isAuthenticated, authLoading, token, isInitializing, systemInitialized])

  // 组件挂载时检查系统初始化状态
  useEffect(() => {
    const initializeAuth = async () => {
      try {
        console.log('[Auth] Checking system initialization...')
        
        // 首先检查系统是否已初始化
        // const initResult = await apiClient.get<any>('/')
        const initResult = await apiClient.get<any>('/api/auth/check-initialization')
        
        if (initResult.success) {
          setSystemInitialized(initResult.isInitialized)
          console.log(`[Auth] System initialized: ${initResult.isInitialized}`)
          
          // 如果系统已初始化，继续认证流程
          if (initResult.isInitialized) {
            console.log('[Auth] Restoring auth state...')
            
            // 先尝试从本地存储恢复状态
            dispatch(restoreAuthState())
            
            // 等待一个tick让状态更新
            await new Promise(resolve => setTimeout(resolve, 0))
            
            // 获取恢复后的token
            const currentToken = localStorage.getItem('auth_token')
            
            if (currentToken) {
              console.log('[Auth] Found local token, verifying...')
              // 验证token是否仍然有效
              try {
                const result = await dispatch(verifyToken(currentToken))
                
                if (verifyToken.fulfilled.match(result)) {
                  console.log('[Auth] Token verified successfully')
                } else {
                  console.log('[Auth] Token verification failed:', result.payload)
                  setLoginError('登录已过期，请重新登录')
                }
              } catch (error) {
                console.log('[Auth] Token verification error:', error)
                setLoginError('登录验证失败，请重新登录')
              }
            } else {
              console.log('[Auth] No local token found')
            }
          }
        } else {
          console.error('检查系统初始化状态失败')
          setSystemInitialized(false) // 默认为未初始化
        }
      } catch (error) {
        console.error('初始化检查失败:', error)
        setSystemInitialized(false) // 出错时默认为未初始化
      } finally {
        setIsInitializing(false)
      }
    }

    initializeAuth()
  }, [dispatch])

  // 监听token过期事件
  useEffect(() => {
    const handleTokenExpired = () => {
      console.log('[Auth] Token expired, clearing auth state')
      dispatch(clearAuthState())
      setLoginError('登录已过期，请重新登录')
    }

    window.addEventListener('auth:token-expired', handleTokenExpired)
    
    return () => {
      window.removeEventListener('auth:token-expired', handleTokenExpired)
    }
  }, [dispatch])

  // 处理登录
  const handleLogin = async (username: string, password: string) => {
    try {
      console.log('[Auth] Login attempt:', username)
      setLoginError(null)
      dispatch(clearError())
      
      const result = await dispatch(login({ username, password }))
      
      if (login.fulfilled.match(result)) {
        console.log('[Auth] Login successful')
        setLoginError(null)
      } else if (login.rejected.match(result)) {
        const errorMessage = result.payload || '登录失败'
        console.log('[Auth] Login failed:', errorMessage)
        setLoginError(errorMessage)
        throw new Error(errorMessage)
      }
    } catch (error: any) {
      console.error('登录过程出错:', error)
      throw error // 重新抛出错误，让LoginPage显示加载状态
    }
  }

  // 清除登录错误
  useEffect(() => {
    if (authError && authError !== loginError) {
      setLoginError(authError)
    }
  }, [authError, loginError])

  // 处理安装完成
  const handleInstallComplete = () => {
    console.log('[Auth] Install complete, refreshing init state')
    setSystemInitialized(true)
    setLoginError(null)
  }

  // 初始化加载中
  if (isInitializing || systemInitialized === null) {
    return (
      <div style={{
        minHeight: '100vh',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        background: 'var(--md-surface)'
      }}>
        <Spin size="large" style={{ color: 'white' }} />
      </div>
    )
  }

  // 系统未初始化，显示安装页面
  if (!systemInitialized) {
    return (
      <InstallPage onInstallComplete={handleInstallComplete} />
    )
  }

  // 系统已初始化但未登录，显示登录页面
  if (!isAuthenticated) {
    return (
      <LoginPage
        onLogin={handleLogin}
        loading={authLoading}
        error={loginError || undefined}
      />
    )
  }

  // 已登录，显示主应用
  return <>{children}</>
}

export default AuthGuard 