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


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
      }, 500)
      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
}