import * as React from 'react';
import { useNavigate } from "react-router-dom";
import { Dropdown, Spin,Tooltip } from 'antd';
import { Chat } from './DeepSeekChat';
import whiteLogo from "../icons/whiteLogo.png";
import jkxz from "../icons/jkxz.png";
import { useChatStore } from "../store";
import { useMobileScreen } from '../utils';
import api from "@/app/api/api";
import './deepSeekHome.scss';

const DeekSeek: React.FC = () => {
    const chatStore = useChatStore();
    const isMobileScreen = useMobileScreen();

    const navigate = useNavigate();

    const [listLoading, setListLoading] = React.useState(false);

    type List = {
        title: string,
        children: {
            title: string,
            showMenu: string,
            chatMode: string,
            appId: string,
            children?: List[number]['children'],
        }[],
    }[];

    const [list, setList] = React.useState<List>([
        {
            "children": [
                {
                    "chatMode": "LOCAL",
                    "showMenu": "false",
                    "appId": "2935625422066814976",
                    "title": "公司作业指导书（2023版）"
                },
                {
                    "chatMode": "LOCAL",
                    "showMenu": "false",
                    "appId": "2935200536655695872",
                    "title": "常用验收规范问答"
                },
                {
                    "chatMode": "LOCAL",
                    "showMenu": "false",
                    "appId": "2942568790025965568",
                    "title": "钢结构AI监理师"
                }
            ],
            "title": "专业知识"
        },
        {
            "children": [
                {
                    "chatMode": "LOCAL",
                    "showMenu": "false",
                    "appId": "2919677614293913600",
                    "title": "员工入职小百科"
                },
                {
                    "chatMode": "LOCAL",
                    "showMenu": "false",
                    "appId": "2919668410128666624",
                    "title": "数字系统答疑"
                },
                {
                    "chatMode": "LOCAL",
                    "showMenu": "false",
                    "appId": "2945774476037853184",
                    "title": "企业介绍"
                }
            ],
            "title": "职能管理"
        },
        {
            "children": [],
            "title": "项目级应用"
        }
    ]);

    const init = async () => {
        setListLoading(true);
        try {
            const res = await api.get('/deepseek/api/appType');
            setList(res.data);
        } catch (error) {
            console.error(error);
        } finally {
            setListLoading(false);
        }
    }

    React.useEffect(() => {
        chatStore.clearSessions();
        const userInfo = localStorage.getItem('userInfo');

        if (userInfo) {
            init();
        }
    }, []);

    return (
        <Spin spinning={listLoading}>
            <div className='deekSeek'>
                <div className='deekSeek-header' style={{ justifyContent: isMobileScreen ? 'flex-start' : 'center' }}>
                    <div style={{ display: 'flex', alignItems: 'center', margin: '0 20px' }}>
                        <img src={whiteLogo.src} style={{ width: 20, marginRight: 10 }} />
                        <div style={{ whiteSpace: 'nowrap' }}>
                            上海盈科
                        </div>
                    </div>
                    {
                        list.map((item, index) => {
                            return <Dropdown
                                menu={{
                                    items: item.children.map((child, i) => {
                                        return {
                                            key: 'child' + i,
                                            label: <div
                                            className='deekSeek-menuds'
                                                onClick={() => {
                                                    const search = `?showMenu=${child.showMenu}&chatMode=${child.chatMode}&appId=${child.appId}`;
                                                    if (child.appId) {
                                                        navigate({
                                                            pathname: '/knowledgeChat',
                                                            search: search,
                                                        })
                                                    }
                                                }}
                                            >
                                                <Tooltip placement="left" title={child.title}>
                                                    {child.title} 
                                                </Tooltip>
                                            </div>,
                                            children: child.children ? child.children.map((record, ind) => {
                                                return {
                                                    key: 'record' + ind,
                                                    label: <div
                                                        onClick={() => {
                                                            const search = `?showMenu=${record.showMenu}&chatMode=${record.chatMode}&appId=${record.appId}`;
                                                            if (record.appId) {
                                                                navigate({
                                                                    pathname: '/knowledgeChat',
                                                                    search: search,
                                                                })
                                                            }
                                                        }}
                                                    >
                                                        {record.title}
                                                    </div>
                                                };
                                            }) : undefined,
                                        };
                                    })
                                }}
                                key={index}
                            >
                                <div style={{ whiteSpace: 'nowrap', marginRight: 20, color: '#fff', cursor: 'pointer' }}>
                                    {item.title}
                                </div>
                            </Dropdown>
                        })
                    }
                    {/*<div style={{ whiteSpace: 'nowrap', marginRight: 20, color: '#98b4fa', cursor: 'pointer' }} onClick={() => {*/}
                    {/*    navigate({*/}
                    {/*        pathname: '/deepseekChat',*/}
                    {/*    })*/}
                    {/*}}>*/}
                    {/*    DeepSeek问答*/}
                    {/*</div>*/}

                    {/* 右侧区域 - 开放平台按钮 */}
                    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'flex-end' }}>
                        <button
                            className='open-platform-btn'
                            onClick={() => {
                                // 跳转到其他平台的逻辑，这里使用window.open举例
                                const userInfo = localStorage.getItem('userInfo');
                                const tokenParam = userInfo ? "?token=" + JSON.parse(userInfo).token : "";
                                window.open('https://llm.jkec.info:11431/deepseek/questionAnswer' + tokenParam, '_blank');
                            }}
                        >
                            更多
                        </button>
                    </div>
                </div>
                <div className='deekSeek-content'>
                    <div className='deekSeek-content-title'>
                        <img src={jkxz.src} />
                    </div>
                    <div className='deekSeek-content-title-sm' style={{ marginBottom: isMobileScreen ? 14 : 20 }}>
                        智能问答助手
                    </div>
                    <div className={isMobileScreen ? 'deekSeek-content-mobile' : 'deekSeek-content-pc'}>
                        <Chat />
                    </div>
                </div>
            </div>
        </Spin>
    );
};

export default DeekSeek;
