import { Component, OnInit } from '@angular/core';
import { IonicModule, LoadingController } from '@ionic/angular';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { FmodeChatCompletion, MarkdownPreviewModule } from 'fmode-ng';
import { CompWordEntryComponent } from '../comp-word-entry/comp-word-entry.component';


import { CloudObject, CloudQuery, CloudUser } from '../lib/ncloud';
import { IonHeader, IonToolbar, IonTitle, IonContent, IonButton,IonIcon, ModalController, IonTextarea, IonInput, IonCard, IonCardHeader, IonCardTitle, IonThumbnail, IonCardContent, IonCardSubtitle, IonItem, IonList, IonLabel, IonAvatar, IonSelect, IonSelectOption, AlertController, IonButtons, IonProgressBar, IonText } from '@ionic/angular/standalone';
import { CommonModule } from '@angular/common';
import { AvatarModule, ChatPanelOptions, DalleOptions, FmodeChat, FmodeChatMessage, ImagineWork, openChatPanelModal } from 'fmode-ng';

@Component({
  selector: 'app-short-generator',
  templateUrl: './short-generator.page.html',
  styleUrls: ['./short-generator.page.scss'],
  standalone: true,
  imports: [
    IonicModule,
    FormsModule,
    MarkdownPreviewModule,
    CompWordEntryComponent,
    // IonHeader,
    // IonToolbar,
    // IonTitle,
    // IonContent,
    // IonButton,
    // IonTextarea,
    // IonInput,
    IonIcon,
    IonCard,
    IonCardHeader,
    IonCardTitle,
    IonCardSubtitle,
    IonCardContent,
    IonThumbnail,
    IonItem,
    IonList,
    CommonModule,
    IonLabel,
    IonAvatar,
    IonSelect,
    IonSelectOption,
    // IonButtons,
    IonProgressBar,
    IonText
  ],
})
export class ShortGeneratorPage implements OnInit {
  constructor(private router: Router) { }

  ngOnInit() { }

  navigateToContentGenerator() {
    this.router.navigate(['/content-generator'], { queryParams: { outline: this.generatedOutline } });
  }

  // 用户输入提示词
  titlel: string = "";
  titleInput(ev: any) {
    console.log(ev.detail.value);
    this.titlel = ev.detail.value;
  }

  style: string = "玄幻";
  styleInput(ev: any) {
    this.style = ev.detail.value;
  }


  // 人物词条
  entryList: Array<any> = []
  list: any = [];
  entry: string = ''; 
  onEntryListChange(ev: any) {
    this.entryList = ev;
    let list =JSON.stringify(this.entryList) ;
    console.log(list);

  }
  
  showEntryList() {
    console.log(JSON.stringify(this.entryList))
    
  }
  // 生成的小说大纲
  generatedOutline: string = "";

  // 生成的小说内容
  generatedContent: string = "";

  // 属性：组件内用于展示消息内容的变量
  responseMsg: any = "";

  // 方法：实例化completion对象，传入消息数组，并订阅生成的可观察对象。
  isComplete: boolean = false; // 定义完成状态属性，用来标记是否补全完成

  sendMessage() {
    console.log("create");
    console.log(JSON.stringify(this.entryList));
    console.log(this.titlel);
    this.entry = JSON.stringify(this.entryList); // 将 entryList 转换为字符串并存储在 entry 中
    console.log(this.entry); // 输出 entry 的值
    let PromptTemplate = `
    您作为一名专业的${this.style}作者，请您根据用户提供的标题${this.titlel}，根据这个词条${this.entry}的内容，添加文章信息，并给出短篇小说大纲。
    
    `;
    console.log(PromptTemplate);
    let completion = new FmodeChatCompletion([
      { role: "system", content: "" },
      { role: "user", content: PromptTemplate }
    ]);

    completion.sendCompletion().subscribe((message: any) => {
      // 打印消息体
      console.log(message.content);
      // 赋值消息内容给组件内属性
      this.responseMsg = message.content;
      if (message?.complete) { // 判断message为完成状态，则设置isComplete为完成
        this.isComplete = true;
        // 将生成的小说大纲放入文本框中
        this.generatedOutline = this.responseMsg;
      }
    });
  }

  sendOutline() {
    console.log("create");

    let PromptTemplate = `
    根据短篇小说大纲${this.generatedOutline}，生成一个短篇小说。
    `;

    let completion = new FmodeChatCompletion([
      { role: "system", content: "" },
      { role: "user", content: PromptTemplate }
    ]);

    completion.sendCompletion().subscribe((message: any) => {
      // 打印消息体
      console.log(message.content);
      // 赋值消息内容给组件内属性
      this.responseMsg = message.content;
      if (message?.complete) { // 判断message为完成状态，则设置isComplete为完成
        this.isComplete = true;
        // 将生成的小说内容放入文本框中
        this.generatedContent = this.responseMsg;
      }
    });
  }

  saveNovel() {
    let consult = new CloudObject("NovelAriticle")
    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  ) {
         
          
          consult.set({
            // name: `${this.name}`,
            // age: `${this.age}`,
            // gender: `${this.gender}`,
            // desc: `${this.desc}`,
            // user: this.currentUser.toPointer(),
            title: `${this.titlel}`,
            topic: `${this.style}`,
            content2: `${this.generatedContent}`,
            date: dateStr,
            category: `短篇小说` 
          });
                  consult.save();
                  console.log(consult);
        }
      })
  }
}