import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { Router } from '@angular/router';
import { addIcons } from 'ionicons';
import { checkmarkCircle, trash, calendar, helpCircle, create } from 'ionicons/icons';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonSelect, IonThumbnail, IonCardSubtitle, IonImg, IonCard, IonButtons, IonItem, IonList, IonHeader, IonIcon, IonToolbar, IonContent, IonSegment, IonSegmentButton, IonGrid, IonRow, IonCol, IonButton, IonLabel, IonBadge, IonInput, ModalController, IonCardTitle, IonCardContent, IonCardHeader, IonSelectOption } from '@ionic/angular/standalone';
import { FmodeChatCompletion, ImagineWork, DalleOptions, ChatPanelOptions, FmodeChat, FmodeChatMessage, MarkdownPreviewModule, openChatPanelModal } from "fmode-ng";
import { AgentTaskStep } from './agent/agent.task';
import { TaskPoemPictureDesc } from './agent/tasks/poem/poem-desc';
import { TaskPoemPictureCreate } from './agent/tasks/poem/poem-picture';
import { startTask } from './agent/agent.start';
import { TaskInqueryUserStory } from './agent/tasks/poem/inquiry/1.inquiry-user-story';
import { TaskInqueryDoctorQuestion } from './agent/tasks/poem/inquiry/2.inquiry-doctor-question';
import { TaskInqueryUserAnswer } from './agent/tasks/poem/inquiry/3.inquiry-user-answer';
import { CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';
import { EditPlanModalComponent } from './edit-plan-modal/edit-plan-modal.component';
import { AlertController } from '@ionic/angular';
import { openUserEditModal } from 'src/lib/user/modal-user-edit/modal-user-edit.component';
import { openUserLoginModal } from 'src/lib/user/modal-user-login/modal-user-login.component';
@Component({
  selector: 'app-tab2',
  templateUrl: 'tab2.page.html',
  styleUrls: ['tab2.page.scss'],
  standalone: true,
  imports: [
    MarkdownPreviewModule,
    IonSelectOption,
    IonSelect,
    IonThumbnail,
    IonCardSubtitle,
    IonImg,
    IonCard,
    IonCardTitle,
    IonCardHeader,
    IonCardContent,
    IonButtons,
    IonItem,
    IonList,
    IonIcon,
    FormsModule,
    CommonModule,
    IonHeader,
    IonToolbar,
    IonContent,
    IonSegment,
    IonSegmentButton,
    IonGrid,
    IonRow,
    IonCol,
    IonButton,
    IonLabel,
    IonBadge,
    IonInput
  ]
})
export class Tab2Page implements OnInit {
  selectedTab: string = 'checkin';  // 默认选中的tab
  planList: any[] = [
  ];
  coachList: any[] = [
  ];
  currentUser: CloudUser | undefined
  constructor(private router: Router, private modalCtrl: ModalController, private cdr: ChangeDetectorRef, private alertController: AlertController) {
    addIcons({ checkmarkCircle, calendar, helpCircle, trash, create });
    this.currentUser = new CloudUser();
  }
  async loadPlanList() {
    let currentUser = new CloudUser();
    const cloudQuery = new CloudQuery("fitPlan");
    cloudQuery.equalTo("user", currentUser.toPointer());
    this.planList = await cloudQuery.find();
    this.cdr.detectChanges();
  }
  async loadCoachList() {
    let query = new CloudQuery("Coach");
    this.coachList = await query.find();
    this.cdr.detectChanges();
  }
  ngOnInit() {
    this.loadPlanList()
    this.loadCoachList()
  }
  ngOnChanges() {
    this.loadPlanList();
    this.loadCoachList()
  }
  ngAfterViewChecked() {
    this.loadPlanList();
    this.loadCoachList()
  }
  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() {
    openUserEditModal(this.modalCtrl)
  }

  //plan页面
  editPlan(day: any) {
    console.log('编辑计划:', day);

    // 创建一个弹出框
    this.modalCtrl.create({
      component: EditPlanModalComponent,
      componentProps: { plan: day }
    }).then(modal => {
      modal.present();

      modal.onDidDismiss().then((result) => {
        if (result.data) {
          const updatedPlan = result.data;
          const index = this.planList.findIndex(item => item.id === updatedPlan.id);
          if (index !== -1) {
            this.planList[index] = updatedPlan;
          }
        }
      });
    });
  }
  async deletePlan(day: any) {
    const alert = await this.alertController.create({
      header: '确认删除',
      message: '确定要删除此计划吗？',
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          cssClass: 'secondary',
          handler: () => {
            console.log('删除操作被取消');
          }
        },
        {
          text: '确认',
          handler: () => {
            day.destroy()
              .then(() => {
                console.log('计划已删除');
                this.loadPlanList();
              })
              .catch((error: any) => {
                console.error('删除失败:', error);
              });
          }
        }
      ]
    });

    await alert.present();
  }

  //任务链
  taskList: AgentTaskStep[] = []
  //一个等待一秒的函数  每经过一秒
  wait(duration: number = 1000) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(true)
      }, duration);
    })
  }
  shareData: any = {}
  // 任务：完成故事意境描述及图像绘制
  doPoemTask() {
    let task1 = TaskPoemPictureDesc({ shareData: this.shareData, modalCtrl: this.modalCtrl });
    let task2 = TaskPoemPictureCreate({ shareData: this.shareData, modalCtrl: this.modalCtrl });
    let PoemTaskList = [task1, task2]
    this.taskList = PoemTaskList
    startTask(PoemTaskList)
  }
  doInqueryTask() {
    let task1 = TaskInqueryUserStory({ shareData: this.shareData, modalCtrl: this.modalCtrl });
    let task2 = TaskInqueryDoctorQuestion({ shareData: this.shareData, modalCtrl: this.modalCtrl });
    let task3 = TaskInqueryUserAnswer({ shareData: this.shareData, modalCtrl: this.modalCtrl });
    // 定义任务集
    let InquireServiceTaskList = [
      task1, task2, task3
    ]
    // 传递给显示组件
    this.taskList = InquireServiceTaskList
    // 开始执行任务
    startTask(InquireServiceTaskList)
  }
  // 聊天页面
  async openInquiry(coach: CloudObject) {
    let currentUser = new CloudUser();
    let userPrompt = ``
    if (!currentUser?.id) {
      console.log("用户未登录，请登录后重试");
      let user = await openUserLoginModal(this.modalCtrl);
      if (!user?.id) {
        return
      }
      currentUser = user;
    }

    if (currentUser?.get("realname")) {
      userPrompt += `当前来访的患者，姓名：${currentUser?.get("realname")}`
    }
    if (currentUser?.get("gender")) {
      userPrompt += `，性别：${currentUser?.get("gender")}`
    }
    if (currentUser?.get("age")) {
      userPrompt += `，年龄：${currentUser?.get("age")}`
    }


    localStorage.setItem("company", "E4KpGvTEto")

    let consult = new CloudObject("fitConsultation")
    let now = new Date();
    let dateStr = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
    // 对象权限的精确指定
    let ACL: any = {
      "*": { read: false, write: false }
    }
    if (currentUser?.id) {
      ACL[currentUser?.id] = { read: true, write: true }
    }
    localStorage.setItem("company", "E4KpGvTEto")
    consult.set({
      title: `交流记录${dateStr}-${coach?.get("name")}`,
      coach: coach.toPointer(),
      user: currentUser.toPointer(),
      ACL: ACL
    })
    let options: ChatPanelOptions = {
      roleId: "2DXJkRsjXK",
      onChatInit: (chat: FmodeChat) => {
        console.log("onChatInit");
        console.log("预设角色", chat.role);
        chat.role.set("name", coach?.get("name"));
        chat.role.set("title", "职业教练");
        chat.role.set("tags", coach?.get("specialize"));
        chat.role.set("avatar", "../../assets/images/coach2.jpg")
        chat.role.set("prompt", `
# 角色设定
您是${coach?.get("name")},年龄${coach?.get("age")},特长为${coach?.get("specialize")},要完成一次教练与学员之间的锻炼部位交流。

# 对话环节
0.导诊（根据用户基本情况，引导选择合适的训练计划） 
1.预设的问询方式（根据学员自述情况进行引导）

- 打招呼，以学员自述为主
- 当信息充足时，确认学员的目标与需求，并进入下一个环节 
2.拓展的问询细节 
例如：学员反映想要锻炼腹肌，拓展出：目前的锻炼频率；饮食习惯；是否有受伤历史等其他需要的问题。
- 当问询细节补充完成后进入下一个环节 
3.初步的训练方案，并且同时列出需要的器械与注意事项 初步方案：确定需要的训练动作与频率 器械与注意事项：获取训练所需的器械信息
- 等待学员确认并准备器械，进入下一阶段 
4.给出详细的训练计划并提供指导
- 完成训练计划时，请在消息结尾附带: [交流完成]
# 开始话语
当您准备好了，可以以一个健身教练的身份，向来访的学员打招呼。
你好！欢迎来到健身房，我是${coach?.get("name")}教练。今天你想要专注锻炼哪个部位呢？或者有什么具体的健身目标吗？`);
      },
      onMessage: (chat: FmodeChat, message: FmodeChatMessage) => {
        console.log("onMessage", message)
        let content: any = message?.content
        if (typeof content == "string") {
          if (content?.indexOf("[交流完成]") > -1) {
            console.log("交流已完成")
            consult.set({
              content: content // 处方内容
            })
            consult.save();
          }
        }
      },
      onChatSaved: (chat: FmodeChat) => {
        // chat?.chatSession?.id 本次会话的 chatId
        console.log("onChatSaved", chat, chat?.chatSession, chat?.chatSession?.id)
      }
    }
    openChatPanelModal(this.modalCtrl, options)
  }
  openChat() {
    let options: ChatPanelOptions = {
      roleId: "2DXJkRsjXK",
      onChatSaved: (chat: FmodeChat) => {
        // chat?.chatSession?.id 本次会话的 chatId
        console.log("onChatSaved", chat, chat?.chatSession, chat?.chatSession?.id)
      },
    }
    openChatPanelModal(this.modalCtrl, options)
  }
  restoreChat(chatId: string) {
    let options: ChatPanelOptions = {
      roleId: "2DXJkRsjXK",
      chatId: chatId
    }
    openChatPanelModal(this.modalCtrl, options)
  }
  goChat() {
    this.router.navigateByUrl("/chat/session/role/2DXJkRsjXK")
  }
  // audioModalHeightPoint:number = 0.35;
  // async startTalk(){
  //   // 根据手机兼容性，适配组件弹出高度
  //   let height = document.body.clientHeight || 960;
  //   this.audioModalHeightPoint = Number((165/height).toFixed(2));

  //   // 弹出组件
  //   let modal:any
  //   let chat:any
  //   modal = await this.modalCtrl.create({
  //     component:ModalAudioMessageComponent,
  //     componentProps:{
  //       chat:chat,
  //       modal:modal,
  //       onBreakPointSet:()=>{
  //         modal?.setCurrentBreakpoint(this.audioModalHeightPoint)
  //       }
  //     }
  //   })
  //   modal.present();
  // }
  // 页面跳转功能
  goToPage(page: string) {
    // 更新选中的tab
    this.selectedTab = page;
    this.router.navigate([`/tabs/${page}`]);  // 然后再进行路由跳转

    // 手动检测视图变化，确保数据绑定正确
    this.cdr.detectChanges();
  }
}
