import { ModalController } from "@ionic/angular";
import { FmodeChatCompletion } from "fmode-ng";
import { extactAndParseJsonFromString } from "src/agent/agent.json";
import { AgentTaskStep } from "src/agent/agent.task";

export function TaskGenerateUserValiate(options: {
    modalCtrl: ModalController
    shareData: any
}
): AgentTaskStep {
    let task1 = new AgentTaskStep({ title: "检测输入合法性", shareData: options.shareData.userProfile })
    task1.handle = () => {
        console.log("任务1:检测输入合法性(userValiate)执行中...");
        return new Promise(async (resolve, reject) => {
            //获取当前任务集共享数据：userProfile
            let userProfile = options.shareData.userProfile
            //若必填项未填写，则提示用户补充
            if (userProfile.gender == '不限制' || userProfile.age == '不限制' || userProfile.height == '不限制' || userProfile.weight == '不限制' || userProfile.season == '不限制') {
                task1.error = "必填项不完整，请补充完整"
                resolve(false);
                return;
            }
            //若必填项已填写，任务继续
            //穿搭提示词生成
            const userProfileJson=JSON.stringify(options.shareData.userProfile,null,);
            console.log("接受输入："+userProfileJson);
            let PromptTemplate = `您是一名专业的语境分析师，现传入数据如下：${userProfileJson}。
            分析其中的customDesc字段，如果与个性化服装搭配的主题无关，则只需要给我返回[无关]，若有关，则返回[有关]。
            当检测customDesc为空，即没有输入时，仍返回[有关]。`

            let completion = new FmodeChatCompletion([
                { role: "system", content: "" },
                { role: "user", content: PromptTemplate }
            ])
            completion.sendCompletion().subscribe((message: any) => {
                if (task1.progress < 0.5) {
                    task1.progress += 0.1
                }
                if (task1.progress >= 0.5 && task1.progress <= 0.9) {
                    task1.progress += 0.01
                }
                if (task1.progress >= 0.9) {
                    task1.progress += 0.001
                }
                // 打印消息
                console.log(message.content)
                // 赋值消息内容给任务集共享数据
                if (message.complete) {
                    if (message.content == "[无关]") {
                        task1.progress = 1
                        task1.error = "请勿输入与穿搭主题无关的内容！"
                        resolve(false);
                        return;
                    }
                    options.shareData.userProfileJson = userProfileJson
                    task1.progress = 1
                    console.log(options.shareData)
                    resolve(true);
                }
            })
        })
    }
    return task1
}