import { Component, OnInit } from '@angular/core';
import { IonicModule, ToastController } from '@ionic/angular';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { TagInputComponent } from '../tag-input/tag-input.component';
import { addIcons } from 'ionicons';
import { barbellOutline, personOutline, square, alarmOutline } from 'ionicons/icons';
import { FmodeChatCompletion, MarkdownPreviewModule } from 'fmode-ng';

@Component({
  selector: 'app-test-page',
  templateUrl: './test-page.component.html',
  styleUrls: ['./test-page.component.scss'],
  standalone: true,
  imports: [IonicModule, FormsModule, CommonModule, TagInputComponent,
    // 引入fm-markdown-preview组件模块
    MarkdownPreviewModule]  // 添加 CommonModule
})
export class TestPageComponent implements OnInit {
  selectedTags: string[] = [];
  exercisePreference: string = '';
  workoutFrequency: string = '';
  height: number | null = null;
  weight: number | null = null;
  age: number | null = null;
  goalDescription: string = '';
  generatedPlan: string = '';  // 用来存储生成的健身计划
  isGenerating: boolean = false;  // 是否正在生成计划
  messageList: any[] = [];  // 存储消息列表

  constructor(private toastController: ToastController) {
    addIcons({ personOutline, barbellOutline, alarmOutline, square });
  }

  ngOnInit() { }

  onTagsChanged(tags: string[]) {
    this.selectedTags = tags;
    console.log('当前标签:', this.selectedTags);
  }

  onGoalDescriptionChange(event: any) {
    this.goalDescription = event.detail.value;
  }

  onExercisePreferenceChange(event: any) {
    this.exercisePreference = event.detail.value;
  }

  onWorkoutFrequencyChange(event: any) {
    this.workoutFrequency = event.detail.value;
  }

  onHeightChange(event: any) {
    this.height = event.detail.value;
  }

  onWeightChange(event: any) {
    this.weight = event.detail.value;
  }

  onAgeChange(event: any) {
    this.age = event.detail.value;
  }

  // 显示toast提示
  async showToast(message: string) {
    const toast = await this.toastController.create({
      message: message,
      duration: 2000,
      position: 'top',
      color: 'danger',
    });
    toast.present();
  }

  async generatePlan() {
    // 检查身高和体重是否为空
    if (!this.height || !this.weight) {
      // 如果为空，显示提示信息
      await this.showToast('身高或体重为空，无法生成个人健身计划');
      return; // 退出函数，不继续生成健身计划
    }

    this.generatedPlan = '';  // 清空之前的结果
    this.messageList = [];  // 清空旧的消息列表，避免重复内容
    this.isGenerating = true;
    console.log('生成健身计划', {
      tags: this.selectedTags,
      exercisePreference: this.exercisePreference,
      workoutFrequency: this.workoutFrequency,
      height: this.height,
      weight: this.weight,
      age: this.age,
      goalDescription: this.goalDescription
    });

    let prompt = `您作为一名专业的健身计划定制大师，请帮我根据以下情况制定健身计划(健身计划请给每天的运动标上序号)。关键词：${this.selectedTags.join(",")},目标描述:${this.goalDescription},运动偏好:${this.exercisePreference},
    健身频率一周:${this.workoutFrequency},身高:${this.height}cm,体重:${this.weight}kg,年龄:${this.age}`;

    let messageList = new FmodeChatCompletion([
      { role: "system", content: '' },
      { role: "user", content: prompt }
    ]);
    messageList.sendCompletion().subscribe((message: any) => {
      console.log(message.content)
      this.generatedPlan = message.content
      if (message?.complete) {
        this.isGenerating = false
      }
    })
  }
}
