import { CommonModule } from '@angular/common';
import { ChangeDetectorRef, Component } from '@angular/core';
import { IonHeader, IonToolbar, IonTitle, IonContent, IonItem, IonCardContent, IonCardSubtitle, IonCardTitle, IonCardHeader, IonCard } from '@ionic/angular/standalone';
import { IonTextarea, IonButton } from "@ionic/angular/standalone";
import { DalleOptions, ImagineWork } from 'fmode-ng';
// 引用设计的对象类和查询类
import { CloudObject, CloudQuery } from 'src/lib/ncloud';
import { RecommendationService } from 'src/lib/recommend';
import { CloudUser } from 'src/lib/ncloud';
import { openUserLoginModal } from 'src/lib/user/modal-user-login/modal-user-login.component';
import { ModalController } from '@ionic/angular/standalone';


@Component({
  selector: 'app-tab2', 
  templateUrl: 'tab2.page.html',
  styleUrls: ['tab2.page.scss'],
  standalone: true,
  imports: [
    IonHeader, IonToolbar, IonTitle, IonContent, IonCard,
    IonButton,IonCardTitle,IonCardHeader,IonCardHeader,
    IonTextarea,IonItem,CommonModule,IonCardContent,IonCardSubtitle
  ],
})
export class Tab2Page {
  userPrompt:string = "飞码产品LOGO，独角兽头部形象，极简风格，棱角分明，线条勾勒，蓝色紫色搭配。"
  ngOnInit() {
    // 生命周期：页面加载后，运行医生列表加载函数
    this.loadPerferList()
  }
// 创建用于数据列表存储的属性
perferList:Array<any> = []
// 查询并加载偏好物品列表的函数
async loadPerferList(){
  let recommendationService=new RecommendationService();
  let result=recommendationService.getItemRecommendations('266024222614683648', 2);
  this.perferList = await result;
}




  promptInput(ev:any){
    this.userPrompt = ev.detail.value;
  }
  imagineWork:ImagineWork|undefined
  images:Array<string> = []
  constructor(
    private cdRef:ChangeDetectorRef,
    private modalCtrl: ModalController
  ){
    // 示例任务，自己生成图片后请存储新的ID
    this.imagineWork = new ImagineWork("lpJGiFwWeA");
    this.imagineWork.fetchTask().then(work=>{
      this.images = this.imagineWork?.images || [];
    })
    this.currentUser = new CloudUser();
  }

  async createImage(){
    this.imagineWork = new ImagineWork();
    let options:DalleOptions = {prompt:this.userPrompt}
    this.imagineWork.draw(options).subscribe(work=>{
      console.log("imagineWork",work?.toJSON())
      console.log("images",work?.get("images"))
      if(work?.get("images")?.length){
        this.images = work?.get("images");
      }
    })
  }



  currentUser:CloudUser|undefined
 
  async login(){
    //弹窗登录
    let user=await openUserLoginModal(this.modalCtrl);
    if(user?.id){
      this.currentUser = user;
    }

  }
  async signup(){
    //弹窗注册
    let user=await openUserLoginModal(this.modalCtrl,"signup");
    if(user?.id){
      this.currentUser = user;
    }
  }
  logout(){
    this.currentUser?.logout();
  }
  editUser(){

  }

}