import {useEffect, useState} from 'react';
import {Screen1Hook} from './components/Screen1Hook';
import {Screen2Upload} from './components/Screen2Upload';
import {Screen3Analysis} from './components/Screen3Analysis';
import {Screen4Payment} from './components/Screen4Payment';
import {Screen5Report} from './components/Screen5Report';
import wxLogin from "@/wx/wxLogin";
import toast, {Toaster} from "react-hot-toast";
import wxShare from "@/wx/wxShare";
import {enterpriseAnalyzeApi} from "@/api/service";
import {useUserStore} from "@/store/user-store";
import {uploadFileQoi} from "@/utils/upload/upload";

export default function App() {
    const userStore = useUserStore()
    //初始化
    const [init, setInit] = useState(false)
    const [currentScreen, setCurrentScreen] = useState(1);

    /**
     * 步骤往下
     */
    const handleNextScreen = () => {
        setCurrentScreen(prev => Math.min(prev + 1, 5));
    };

    /**
     * 上传文件
     */
    const handleUpload = async (file) => {
        try {
            handleNextScreen()
            let fileUrl = await uploadFileQoi(file, "/analyze")
            let res = await enterpriseAnalyzeApi({
                analys_image: fileUrl,
                analys_type: null,
            }) as any
            if (res.analysis_result.analyze_ret != "success") {
                toast.error("请重新上传结构清晰的组织架构图")
                setCurrentScreen(prev => prev = 2)
                return
            }
            userStore.setAnalysis(res)
            handleNextScreen()
        } catch (e) {
            setCurrentScreen((prev => prev = 2))
        }
    };

    const handlePayment = () => {
        handleNextScreen();
    };

    /**
     * 授权
     */
    useEffect(() => {
        wxLogin().then(() => {
            setInit(true)
            wxShare().then()
        })
    }, []);

    if (!init) {
        return <></>
    }

    return (
        <>
            <Toaster position="top-center"/>
            <div className="min-h-screen bg-[#0A0F24] text-white overflow-x-hidden relative">
                {/* 背景色 */}
                <div className="fixed inset-0 pointer-events-none">
                    <div
                        className="absolute inset-0 bg-gradient-to-b from-[#7B61FF]/10 via-transparent to-[#00F0FF]/10"/>
                </div>

                {/* Main content */}
                <div className="relative z-10">
                    {currentScreen === 1 && <Screen1Hook onNext={handleNextScreen}/>}
                    {currentScreen === 2 && <Screen2Upload onSuccess={handleUpload}/>}
                    {currentScreen === 3 && <Screen3Analysis/>}
                    {currentScreen === 4 && <Screen4Payment onPayment={handlePayment}/>}
                    {currentScreen === 5 && <Screen5Report/>}
                </div>

            </div>
        </>
    );
}
