import { Component, OnInit } from '@angular/core';
import { NgModel } from '@angular/forms';
import { Router } from '@angular/router';
import { IonButton, IonCard, IonCardContent, IonCardHeader, IonCardTitle, IonContent, IonHeader, IonIcon, IonInput, IonItem, IonLabel, IonList, IonToolbar, ModalController } from '@ionic/angular/standalone';
import { CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';
import { CommonModule } from '@angular/common';
import { TaskInqueryUserStory } from 'src/agent/tasks/inquiry/1.inquiry-user-story';
import { TaskInqueryDoctorQuestion } from 'src/agent/tasks/inquiry/2.inquiry-doctor-question';
import { TaskInqueryUserAnswer } from 'src/agent/tasks/inquiry/3.inquiry-user-answer';
import { TaskExecutor } from 'src/agent/agent.start';
import { AgentTaskStep } from 'src/agent/agent.task';
import { DalleOptions, FmodeChatCompletion, ImagineWork } from 'fmode-ng';

@Component({
  selector: 'page-ai-knowledge',
  templateUrl: './page-ai-knowledge.component.html',
  styleUrls: ['./page-ai-knowledge.component.scss'],
  standalone: true,
  imports:[
    IonHeader,IonToolbar,IonButton,IonContent,IonCard,IonCardTitle, IonCardHeader,
    IonCardContent,CommonModule, IonInput,IonList, IonItem, IonLabel, IonIcon
  ]
})
export class PageAiKnowledgeComponent  implements OnInit {

  back: string = 'back'; //返回
  backHome(){
    this.router.navigate(['/tabs/tab2']);
  }
  gotouser(){

  }

  avatar : string = "";
  currentUser : CloudUser | undefined ;
  objectId : string = "";

  constructor(
    private router: Router,
    private modalCtrl: ModalController,
  ) {
    this.loadData();
     // 示例任务，自己生成图片后请存储新的ID 
    //  this.imagineWork = new ImagineWork("");
     this.imagineWork.fetchTask().then(work=>{
       if(work){
         this.imagineWork.id = work.id
       }
       this.images = this.imagineWork?.images;
     })
  }
  allMessage : Array<CloudObject> = [];
  async loadData(){
    this.currentUser = new CloudUser();
    this.avatar = this.currentUser.data["avatar"];
    this.objectId = this.currentUser.data['objectId'];
  }
   
  ngOnInit() {
  }
  kepu:string = ""
  kepuInput(ev:any){
    this.kepu = ev.detail.value;
    console.log(this.kepu);
  }
  imagineWork:ImagineWork = new ImagineWork; // 图片生成
  images:Array<string> = [] // 图片数组
  PictureDescResult:string = `` // 画面描述结果
  doInqueryTask(){
    this.imagineWork = new ImagineWork();
        if (this.kepu.length > 0){
        // 文本生成
        let PromptTemplate = `您是一名专业的医学教育工作者，擅长将晦涩难懂的医学名词转化为一些通俗易懂的话语，请您根据下面提供的内容，将其描述的画面、场景、人物、物品等用故事的方式表达并将内容压缩在五个画面中，直接写出画面，并且描述的画风以漫画的风格为主
        需求如下：
        ${this.kepu}
        `
        let completion = new FmodeChatCompletion([
          {role:"system",content:""},
          {role:"user",content:PromptTemplate}
        ])
        completion.sendCompletion().subscribe((message:any)=>{
          // 打印消息体
          console.log("消息体",message.content)
          // 赋值消息内容给组件内属性
          this.PictureDescResult = message.content
          if(message?.complete){ // 判断message为完成状态，则设置isComplete为完成
            for(let i = 0; i < 5; i++){
              // 图片生成
              let PicturePrompt = `${this.PictureDescResult}\n根据画面${i+1}：进行绘画，如果图片中有谈话内容，请以普通话为主，风格：画面不带任何其他文字。其中以中国漫画人物为主体，人物诙谐幽默，符合中国现代人审美。图片展示的内容要和前面提供的内容保持一致`
              console.log(PicturePrompt)
              let options:DalleOptions = {prompt:PicturePrompt}
              this.imagineWork?.draw(options).subscribe(work=>{
                console.log("imagineWork",work?.toJSON())
                console.log("images",work?.get("images"))
                if (work?.get("images")?.length){
                  // 将生成的每一张图片存储到数组中
                  this.images.push(work?.get("images")[0]);
                  console.log("work?.get('images')",work?.get("images"))
                }
                console.log("this.images",this.images)
              })
            }
          }
        })
      }
      else{
        this.kepu = "请提供您合理的健康知识，我将根据其意境为您创作一幅画面的简短描述。"
      }
  }

}
