import { Component, OnInit } from '@angular/core';
import {
  IonButton,
  IonCard,
  IonCardHeader,
  IonCardSubtitle,
  IonCardTitle,
  IonCol,
  IonContent,
  IonGrid,
  IonHeader,
  IonInput,
  IonItem,
  IonList,
  IonRow,
  IonTextarea,
  IonTitle,
  IonToolbar,
} from '@ionic/angular/standalone';
/** 引用：从fmode-ng库引用FmodeChatCompletion类 */
import { FmodeChatCompletion, MarkdownPreviewModule } from 'fmode-ng';

@Component({
  selector: 'app-aiplan-page',
  templateUrl: './aiplan-page.component.html',
  styleUrls: ['./aiplan-page.component.scss'],
  standalone: true,
  imports: [
    IonHeader,
    IonTitle,
    IonToolbar,
    IonCard,
    IonContent,
    IonList,
    IonItem,
    IonInput,
    IonCardHeader,
    IonCardTitle,
    IonCardSubtitle,
    IonTextarea,
    IonButton,
    IonGrid,
    IonRow,
    IonCol,
    MarkdownPreviewModule,
  ],
})
export class AiplanPageComponent implements OnInit {
  constructor() {}

  ngOnInit() {}

  trainingType: string = '';
  sex: string = '';
  weight: number = NaN;
  trainingTime: number = NaN;
  trainingIntensity: string = '';
  trainingSite: string = '';
  needs: string = '';

  aiplan: string = '';

  userInputOntrainingType(ev: any) {
    this.trainingType = ev.detail.value;
  }
  userInputOnsex(ev: any) {
    this.sex = ev.detail.value;
  }
  userInputOnweight(ev: any) {
    this.weight = ev.detail.value;
  }
  userInputOntrainingTime(ev: any) {
    this.trainingTime = ev.detail.value;
  }
  userInputOntrainingIntensity(ev: any) {
    this.trainingIntensity = ev.detail.value;
  }
  userInputOntrainingSite(ev: any) {
    this.trainingSite = ev.detail.value;
  }
  userInputOnneeds(ev: any) {
    this.needs = ev.detail.value;
  }
  reset() {
    // 重置表单数据(有问题)
    // this.trainingType = '';
    // this.weight = NaN;
    // this.trainingTime = NaN;
    // this.trainingIntensity = '';
    // this.trainingSite = '';
    // this.needs = '';

    // 重置表单数据(无错误)
    let ipts = document.querySelectorAll('ion-input');
    let textarea = document.querySelectorAll('ion-textarea');
    ipts.forEach((ipt) => {
      ipt.value = '';
    });
    textarea.forEach((text) => {
      text.value = '';
    });
  }

  // submit() {
  //   // 提交表单数据
  //   // let data = {
  //   //   trainingType: this.trainingType,
  //   //   weight: this.weight,
  //   //   trainingTime: this.trainingTime,
  //   //   trainingIntensity: this.trainingIntensity,
  //   //   trainingSite: this.trainingSite,
  //   //   needs: this.needs,
  //   // };

  //   // 用户输入数据提取
  //   let ipts = document.querySelectorAll('ion-input');
  //   let textarea = document.querySelectorAll('ion-textarea');
  //   let userinputs = '';
  //   ipts.forEach((ipt) => {
  //     userinputs += ipt.value + ' ';
  //   });
  //   textarea.forEach((text) => {
  //     userinputs += text.value + ' ';
  //   });
  //   let userinputlist = userinputs.split(' ');
  //   let data = {
  //     trainingType: userinputlist[0],
  //     sex: userinputlist[1],
  //     weight: userinputlist[2],
  //     trainingTime: userinputlist[3],
  //     trainingIntensity: userinputlist[4],
  //     trainingSite: userinputlist[5],
  //     needs: userinputlist[6],
  //   };
  //   console.log(data);

  //   // ai对话实现
  //   let prompt = `我是一名体重为${data.weight}kg的${data.sex}性，我只有${data.trainingTime}分钟来${data.trainingType}，我希望训练强度${data.trainingIntensity}一点，而我想训练的部位为${data.trainingSite}， 一些其他的需求有${data.needs}。 请你作为一名专业的健身教练，根据我提供的信息，为我制定${data.trainingType}计划。`;
  //   console.log(prompt);

  //   let token = `r:E4KpGvTEto-187799890851732794669`;
  //   localStorage.setItem('token', token);

  //   let messageList = [
  //     {
  //       role: 'system',
  //       content: `${new Date().toLocaleString}`,
  //     },
  //     {
  //       role: 'user',
  //       content: prompt,
  //     },
  //   ];

  //   //fmode-chat
  //   let completion = new TestChatCompletion(messageList);
  //   completion.createCompletionByStream();

  //   this.aiplan = '';
  //   setInterval(() => {
  //     // console.log(messageList);
  //     this.aiplan = messageList[messageList.length - 1].content;
  //   }, 1000);
  // }

  isComplete: boolean = true;

  sendMessage() {
    // 用户输入数据提取 不建议
    // let ipts = document.querySelectorAll('ion-input');
    // let textarea = document.querySelectorAll('ion-textarea');
    // let userinputs = '';
    // ipts.forEach((ipt) => {
    //   userinputs += ipt.value + ' ';
    // });
    // textarea.forEach((text) => {
    //   userinputs += text.value + ' ';
    // });
    // let userinputlist = userinputs.split(' ');
    // let data = {
    //   trainingType: userinputlist[0],
    //   sex: userinputlist[1],
    //   weight: userinputlist[2],
    //   trainingTime: userinputlist[3],
    //   trainingIntensity: userinputlist[4],
    //   trainingSite: userinputlist[5],
    //   needs: userinputlist[6],
    // };
    // console.log(data);

    this.isComplete = false;
    this.aiplan = '';

    // ai对话实现
    let prompt = `我是一名体重为${this.weight}kg的${this.sex}性，我只有${this.trainingTime}分钟来${this.trainingType}，我希望训练强度${this.trainingIntensity}一点，而我想训练的部位为${this.trainingSite}， 一些其他的需求有${this.needs}。 请你作为一名专业的健身教练，根据我提供的信息，为我制定${this.trainingType}计划。`;
    console.log(prompt);

    let completion = new FmodeChatCompletion([
      { role: 'system', content: '' },
      { role: 'user', content: prompt },
    ]);
    completion.sendCompletion().subscribe((message: any) => {
      // 打印消息体
      // console.log(message.content);
      // 赋值消息内容给组件内属性
      this.aiplan = message.content;

      if (message?.complete) {
        // 判断message为完成状态，则设置isComplete为完成
        this.isComplete = true;
      }
    });
  }
}
