import './proj-edit.css';
import {Link, useNavigate, useParams} from "react-router-dom";
import {Breadcrumb, Button, message} from "antd";
import StepProjEdit from "../../components/step/StepProjEdit.tsx";
import CardProjEdit from "../../components/card/CardProjEdit.tsx";
import {Process} from "../../interfaces/step/IStepProj.ts";
import CardProjDownload from "../../components/card/CardProjDownload.tsx";
import CardProjJump from "../../components/card/CardProjJump.tsx";
import {useEffect, useState} from "react";
import {Axios, get} from "../../util/AjaxUtils.ts";
import {EditStepEnum, IProjEdit} from "../../interfaces/card/ICardProj.ts";
import {MAX_MOD_SIZE} from "./edit/ProjConfigModList.tsx";
import {GenerateStatus} from "../../interfaces/proj/IProj.ts";

export default function ProjEditAll() {
    const nav = useNavigate();
    const pathParams = useParams();

    const [messageApi, contextHolder] = message.useMessage();
    const [editStepArray, setEditStepArray] = useState<IProjEdit[]>([]);
    const [isEditStepEdited, setIsEditStepEdited] = useState(false);
    const [isConfigEdited, setIsConfigEdited] = useState(false);
    const [generateStatus, setGenerateStatus] = useState(GenerateStatus.NONE);
    const [previewUrl, setPreviewUrl] = useState('');

    const height = window.innerHeight - 240;

    const renderEditStep = (editSteps: any[], isEdited: boolean, isGenerateSuccess: boolean) => {
        const editStepArray: IProjEdit[] = [];
        editStepArray.push(
            {
                title: '标题简介',
                desc: '完善代码、样式类型和详细介绍等内容',
                step: 1,
                btnName: !isGenerateSuccess ? '设置' : '查看',
                status: editSteps[0].editStatus,
                handleEdit() {
                    if (!isGenerateSuccess) {
                        nav(`/proj-edit/step1/${pathParams.projId}`)
                    } else {
                        nav(`/proj-edit/step1-show/${pathParams.projId}`)
                    }
                }
            },
            {
                title: '著作人信息',
                desc: '请完善著作人相关信息',
                step: 4,
                btnName: !isGenerateSuccess ? '设置' : '查看',
                status: editSteps[3].editStatus,
                handleEdit() {
                    if (!isGenerateSuccess) {
                        nav(`/proj-edit/step4/${pathParams.projId}`)
                    } else {
                        nav(`/proj-edit/step4-show/${pathParams.projId}`)
                    }
                }
            },
            {
                title: '申请人信息',
                desc: '请完善申请人信息',
                step: 5,
                btnName: !isGenerateSuccess ? '设置' : '查看',
                status: editSteps[4].editStatus,
                handleEdit() {
                    if (!isGenerateSuccess) {
                        nav(`/proj-edit/step5/${pathParams.projId}`)
                    } else {
                        nav(`/proj-edit/step5-show/${pathParams.projId}`)
                    }
                }
            },
        )
        setEditStepArray(editStepArray);
        setIsEditStepEdited(isEdited);
    }

    const renderData = () => {
        get<any>({
            messageApi: messageApi,
            url: `/api/proj/get/${pathParams.projId}`,
            onSuccess({data}) {
                const isEdited = data.editSteps[0].editStatus == EditStepEnum.EDITED
                    && data.editSteps[1].editStatus == EditStepEnum.EDITED
                    && data.editSteps[2].editStatus == EditStepEnum.EDITED
                    && data.editSteps[3].editStatus == EditStepEnum.EDITED
                    && data.editSteps[4].editStatus == EditStepEnum.EDITED
                    && data.editSteps[5].editStatus == EditStepEnum.EDITED;
                const isConfig = data.loginpage.loginpageId && MAX_MOD_SIZE > 0;
                const isGenerateSuccess: boolean = data.generate.generateStatus == GenerateStatus.SUCCESS;
                renderEditStep(data.editSteps, isEdited, isGenerateSuccess);
                setIsConfigEdited(isConfig);
                setGenerateStatus(data.generate.generateStatus);
                setPreviewUrl(data.previewUrl);
            }
        })
    }

    useEffect(() => {
        renderData();
    }, [])

    return (
        <>
            {contextHolder}
            <Breadcrumb
                items={[
                    {title: <Link to={'/'}>首页</Link>},
                    {title: <Link to={'/proj-create'}>创建项目</Link>},
                    {title: '编辑项目'},
                ]}
            />
            <div className="proj-edit" style={{height: `${height}px`, padding: '15px 175px'}}>
                <StepProjEdit step={1} process={isEditStepEdited ? Process.COMPLETE : Process.PROCESSING}
                              descTitle="完善信息"
                              descDetail="完善项目的基本信息"
                              hasNext={true}>
                    {
                        editStepArray.map((item, index) => {
                            return <CardProjEdit key={`editStep-${index}`}
                                                 title={item.title}
                                                 desc={item.desc}
                                                 status={item.status}
                                                 btnName={item.btnName}
                                                 handleEdit={item.handleEdit}
                            />
                        })
                    }

                </StepProjEdit>
                <StepProjEdit step={2}
                              process={!isEditStepEdited ? Process.PENDING : (isConfigEdited ? Process.COMPLETE : Process.PROCESSING)}
                              descTitle="功能设置"
                              descDetail="设置系统的菜单功能" hasNext={true}>
                    {
                        <CardProjJump title="预览系统"
                                      desc={isEditStepEdited && isConfigEdited ? "点击查看预览系统" : "制作中"}
                                      canBtnClick={isEditStepEdited && isConfigEdited}
                                      handleJump={() => {
                                          window.open(`${Axios.defaults?.baseURL}/${previewUrl}`, '_blank')
                                      }}
                        />
                    }
                </StepProjEdit>
                <StepProjEdit step={3}
                              process={generateStatus == GenerateStatus.SUCCESS ? Process.PROCESSING : Process.PENDING}
                              descTitle="资料下载">
                    <CardProjDownload title="申请表"
                                      desc="点击下载申请表"
                                      canBtnClick={generateStatus == GenerateStatus.SUCCESS}
                                      handleDownload={() => {
                                          window.open(`${Axios.defaults?.baseURL}/route/proj/download/apply/${pathParams.projId}`)
                                      }}
                    />
                    <CardProjDownload title="操作手册"
                                      desc="点击下载操作手册"
                                      canBtnClick={generateStatus == GenerateStatus.SUCCESS}
                                      handleDownload={() => {
                                          window.open(`${Axios.defaults?.baseURL}/route/proj/download/manual/${pathParams.projId}`)
                                      }}
                    />
                    <CardProjDownload title="代码压缩包"
                                      desc="点击下载代码压缩包"
                                      canBtnClick={generateStatus == GenerateStatus.SUCCESS}
                                      handleDownload={() => {
                                          window.open(`${Axios.defaults?.baseURL}/route/proj/download/code-zip/${pathParams.projId}`)
                                      }}
                    />
                    <CardProjDownload title="代码文档"
                                      desc="点击下载代码文档"
                                      canBtnClick={generateStatus == GenerateStatus.SUCCESS}
                                      handleDownload={() => {
                                          window.open(`${Axios.defaults?.baseURL}/route/proj/download/code/${pathParams.projId}`)
                                      }}
                    />
                </StepProjEdit>
            </div>
            <div className="btn-container">
                <Button size="large" style={{
                    width: '200px',
                    fontSize: '14px',
                    backgroundColor: 'var(--color-primary)',
                    color: 'var(--color-light)'
                }} onClick={() => {
                    nav(-1);
                }}>返回</Button>
            </div>
        </>
    )
}