import { ModalController } from '@ionic/angular/standalone';
import { AgentTaskStep } from 'src/agent/agent.task';
import { ImagineWork, DalleOptions } from 'fmode-ng';


export function TaskPoemPictureCreate(options:{
    modalCtrl:ModalController
    shareData:any}):AgentTaskStep{
    let task2 = new AgentTaskStep({title:"意境绘制",shareData:options.shareData})
    task2.handle = ()=>{
      return new Promise(resolve=>{
        // let userInput = await getUserInput(options.modalCtrl,{fieldsArray:[
        //   {name:"绘图要求",type:"text",desc:"画风、构图等等"}
        // ]});
        // console.log(userInput)
        console.log("意境绘制:执行过程")

        if(!options.shareData.PictureDescResult){
          task2.error = "缺少古风描述结果，请重新执行。"
          resolve(false)
        }
        let imagineWork = new ImagineWork();
        // 图片生成
        let PicturePrompt = `${options.shareData.PictureDescResult}\n风格：中国古风。画面不带任何文字。`
        let imgOptions:DalleOptions = {prompt:PicturePrompt}
        let countDownInt = setInterval(()=>{
          task2.progress += 0.01
        },1000)
        imagineWork?.draw(imgOptions).subscribe((work:any)=>{
            console.log("imagineWork",work?.toJSON())
            console.log("images",work?.get("images"))
            if(work?.get("images")?.length){
              options.shareData.images = work?.get("images");
              clearInterval(countDownInt);
              task2.progress = 1
              resolve(true)
            }
        })
      })

    }
    return task2
}