import { AgentTaskStep } from 'src/app/agent/agent.task';
import { getUserInput } from 'src/app/agent/agent.input';
import { ModalController } from '@ionic/angular/standalone';
import { FmodeChatCompletion } from 'fmode-ng';

export function TaskPoemPictureDesc(options: {
    modalCtrl: ModalController
    shareData: any
}
): AgentTaskStep {
    let task1 = new AgentTaskStep({ title: "输入动作需求", shareData: options.shareData })
    task1.handle = () => {
        return new Promise(async (resolve, reject) => {
            // 获取用户输入的诗词
            let userInput = await getUserInput(options.modalCtrl, {
                fieldsArray: [
                    { name: "目标部位", type: "text", desc: "请输入目标锻炼的肌肉部位" },
                    { name: "设备限制", type: "text", desc: "是否需要特定设备限制（如徒手、哑铃等）" }
                ]
            });
            if (!userInput?.['目标部位']) {
                task1.error = "缺少目标部位，请重新开始"
                resolve(false);
            }

            // 文本生成
            let PromptTemplate = `您是一名专业的健身教练，请根据以下需求生成一个详细的健身动作画面，包括用箭头标记动作细节并描述(用中文描述)等。
                描述如下：
                ${userInput['目标部位']}${userInput['设备限制']}，请确保动作正确。
                `
            let completion = new FmodeChatCompletion([
                { role: "system", content: "" },
                { role: "user", content: PromptTemplate }
            ])
            completion.sendCompletion().subscribe((message: any) => {
                if (task1.progress < 0.5) {
                    task1.progress = Math.min(task1.progress + 0.02, 1); // 保证不超过1
                } else if (task1.progress < 1) {
                    task1.progress = Math.min(task1.progress + 0.004, 1); // 保证不超过1
                }
                // 打印消息体
                // console.log(message.content)
                // 赋值消息内容给组件内属性
                options.shareData.PictureDescResult = message.content
                if (message.complete) { // 判断message为完成状态，则设置isComplete为完成
                    task1.progress = 1
                    resolve(true)
                }
            })
        })

    }
    return task1
}