import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { CloudObject,CloudUser } from '../lib/ncloud';
import { IonHeader, IonToolbar, IonTitle, IonContent, IonButton, ModalController, IonTextarea, IonInput, IonCard, IonItem, IonList, IonLabel, AlertController, IonButtons, IonBackButton, IonAvatar, IonCardContent, IonCardHeader, IonCardSubtitle, IonCardTitle, 
  IonIcon, IonProgressBar, IonSelect, IonSelectOption, IonText, IonThumbnail } from '@ionic/angular/standalone';
import { CommonModule } from '@angular/common';
import { AvatarModule, ChatPanelOptions, DalleOptions, FmodeChat, FmodeChatCompletion, FmodeChatMessage, ImagineWork, openChatPanelModal } from 'fmode-ng';
import { CompUploaderHwobsComponent } from "../comp-uploader-hwobs/comp-uploader-hwobs.component";

@Component({
  selector: 'app-agent-create',
  templateUrl: './agent-create.page.html',
  styleUrls: ['./agent-create.page.scss'],
  standalone: true,
  imports: [
    IonHeader, IonToolbar, IonTitle, IonContent, IonButton, IonTextarea, IonInput,
    IonCard, IonBackButton,
    IonItem, IonList, CommonModule,
    IonButtons,
    CompUploaderHwobsComponent
]
})
export class AgentCreatePage implements OnInit {

  currentUser: CloudUser;
  constructor(
    private modalCtrl:ModalController,
    private router:Router,
    private alertController: AlertController
  ) {
   this.currentUser = new CloudUser();
  // 示例任务，自己生成图片后请存储新的ID 
  }
 images:Array<string> = []
  back:string = "<";
  backhome() {
    this.router.navigate(['tabs/tab1']);
  }
  ngOnInit() {

  }
  uploadUrl:string = ""
  onUrlChange(ev:any){
    console.log(ev);
   this.uploadUrl=ev;
  }
  name: string = ''
  nameInput(e:any) {
    this.name = e.detail.value;
  }
  age: string = ''
  ageInput(e:any) {
    this.age = e.detail.value;
  }
  gender: string = '人族'
  genderChange(e:any) {
    this.gender = e.detail.value;}
  genderCancel(){}
  selectedImage: string | ArrayBuffer | null = null;
  genderDismiss(){}
  desc: string = ''
  descInput(e:any) {
    this.desc = e.detail.value;}
    onFileSelected(event: Event) {
      const file = (event.target as HTMLInputElement).files?.[0];
      if (file) {
        const reader = new FileReader();
        reader.onload = () => {
          this.selectedImage = reader.result; // 将图片数据存储到 selectedImage
        };
        reader.readAsDataURL(file);
      }
    }
  PictureDescResult:string = `` // 画面描述结果
  async createAgent() {
    localStorage.setItem("company","E4KpGvTEto")
    let consult = new CloudObject("NovelCharacter")
    let now = new Date();
    let dateStr = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}`
    // 对象权限的精确指定
      let completion = new FmodeChatCompletion([
        {role:"system",content:""},
      ])
      completion.sendCompletion().subscribe((message:any)=>{
        // 打印消息体
        console.log(message.content)
        // 赋值消息内容给组件内属性
        if(message?.complete){ // 判断message为完成状态，则设置isComplete为完成
              // 图片生成
            let PicturePrompt = ``
            let options:DalleOptions = {prompt:PicturePrompt}
                  consult.set({
                    name:`${this.name}`,
                    age:`${this.age}`,
                    gender:`${this.gender}`,
                    desc:`${this.desc}`,
                    username: this.currentUser.data["username"],
                    avatar: this.uploadUrl,
                    user:this.currentUser.toPointer(), 
                  })
                  consult.save();
                  console.log(consult);
        }
      })
 
  }

}
