// entrypoints/popup/App.tsx
import { useState, useEffect } from 'react';
import { HashRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import { Link, useNavigate } from 'react-router-dom';
import { Button } from '@alifd/next';
import './App.css';
import '../../assets/tailwind.css'
import '@alifd/next/dist/next.css';
import '@alifd/next/dist/next.min.css';
import Login from './pages/Login'
import DesignPic from './pages/DesignPic';

// 创建一个保护路由组件
function ProtectedRoute({ children }: { children: React.ReactNode }) {
  const isLoggedIn = localStorage.getItem('isLoggedIn');
  return isLoggedIn ? <>{children}</> : <Navigate to="/login" replace />;
}

// 创建带登出功能的导航组件
function Navigation({ onLogout, userinfo }: { onLogout: () => void, userinfo: any }) {
  const navigate = useNavigate();

  const handleLogout = () => {
    onLogout();
    navigate('/login');
  };

  return (
    <div className="flex gap-4 items-center justify-end">
      <div>{userinfo && userinfo.nickname ? <div>
        等级: {userinfo.version_custom_name}
        账户: {userinfo.nickname}
      </div>:<>556</>}</div>
      <nav className="flex gap-4 items-center">
        {/* <Link to="/" className="text-blue-500 hover:underline">首页</Link> */}
        {/* <Link to="/about" className="text-blue-500 hover:underline">关于</Link>
      <Link to="/home" className="text-blue-500 hover:underline">主页</Link> */}
        <Button
          type="normal" warning
          onClick={handleLogout}
        // className="ml-auto text-red-500 cursor-pointer"
        >
          退出登录
        </Button>
      </nav>
    </div>
  );
}

function App() {
  const [count, setCount] = useState(0);
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  const [userinfo, setUserinfo] = useState<any>(null);

  // 检查登录状态
  useEffect(() => {
    const loggedIn = localStorage.getItem('isLoggedIn') === 'true';
    setIsLoggedIn(loggedIn);
    // 获取用户信息
    if (loggedIn) {
      if (localStorage.getItem('userinfo')) {
        let userinfoStr = localStorage.getItem('userinfo');
        console.log(userinfoStr,"userinfoStr")
        try {
          // 尝试解析用户信息
          const parsedUserinfo = userinfoStr ? JSON.parse(userinfoStr) : null;
          setUserinfo(parsedUserinfo);
        } catch (e) {
          // 如果解析失败，可能是字符串格式
          setUserinfo({ nickname: userinfoStr });
        }
      }
    }
  }, []);

  // 处理登出逻辑
  const handleLogout = () => {
    localStorage.removeItem('isLoggedIn');
    localStorage.removeItem('access_token');
    setIsLoggedIn(false);
  };

  return (
    <Router>
      {/* 只在登录后显示导航栏 */}
      {isLoggedIn && <Navigation onLogout={handleLogout} userinfo={userinfo} />}

      <Routes>
        <Route path="/login" element={<Login setIsLoggedIn={setIsLoggedIn} />} />
        <Route path="/" element={
          <ProtectedRoute>
            <DesignPic />
          </ProtectedRoute>
        } />
        {/* <Route path="/about" element={
          <ProtectedRoute>
            <About />
          </ProtectedRoute>
        } />
        <Route path="/home" element={
          <ProtectedRoute>
            <Home />
          </ProtectedRoute>
        } /> */}
      </Routes>
    </Router>
  );
}

export default App;