import { Component,CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/angular/standalone';
import { ExploreContainerComponent } from '../explore-container/explore-container.component';
import { IonButton, IonCard, IonCardContent, IonCardHeader, IonCardTitle, IonItem, IonLabel,
   IonList,IonSelect, IonSelectOption } from '@ionic/angular/standalone';
import { CommonModule } from '@angular/common';
import { Router } from '@angular/router';
import { ChatPanelOptions, FmodeChat, FmodeChatMessage, openChatPanelModal } from 'fmode-ng';
import { ModalController } from '@ionic/angular/standalone';
import { CloudObject, CloudQuery } from 'src/lib/ncloud';

@Component({
  selector: 'app-tab2',
  templateUrl: 'tab2.page.html',
  styleUrls: ['tab2.page.scss'],
  standalone: true,
  imports: [IonHeader, IonToolbar, IonTitle, IonContent, ExploreContainerComponent,
    IonCard,IonCardHeader,IonCardTitle,IonCardContent,
    IonItem,IonLabel,
    IonSelect,IonSelectOption,
    IonButton,IonList,
    CommonModule
  ],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class Tab2Page {

  private modalCtrl: ModalController;
  constructor(private router: Router,modalCtrl: ModalController) {
    this.modalCtrl = modalCtrl;
    // 其他构造函数代码
  }
  clickToConsult(chatpartner:CloudObject) {
    // 弹窗形式聊天：开始咨询
    localStorage.setItem("company","E4KpGvTEto")
    let consult = new CloudObject("ChatRecord")
    let now = new Date();
    let dateStr = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}`
    consult.set({
      title:`${chatpartner.get('expertise') || ""}聊天记录${dateStr}-${chatpartner.get('name')}`,
      chatpartner:chatpartner.toPointer(),
    })
    let options:ChatPanelOptions = {
      roleId:"2DXJkRsjXK",
      onChatInit: (chat: FmodeChat) => {
        console.log("onChatInit");
        console.log("预设角色", chat.role);
        chat.role.set("name", chatpartner.get("name"));
        chat.role.set("bio",chatpartner.get("bio"));
        chat.role.set("expertise", chatpartner.get("expertise"));
        chat.role.set("avatar", chatpartner.get("avatar") || "/assets/img/2.png")
        chat.role.set("prompt", `
        # 角色设定
        您是${chatpartner.get("name")}，一位${chatpartner.get("bio")}，${chatpartner.get("expertise")}，需要为用户提供陪伴和支持等积极情绪。
        # 对话环节
        用户提及到关于不想聊了、不想说了、有事先走了类似的话语，耐心积极开导回复结束后请在消息结尾附带: [聊天结束]
        # 开始话语
        当您准备好了，可以以一个关心用户的朋友的身份，向来访的用户打招呼。
        `);
      },
      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();
          }
        }
      },
        // chat?.chatSession?.id 本次会话的 chatId
        onChatSaved:(chat:FmodeChat)=>{
        console.log("onChatSaved",chat,chat?.chatSession,chat?.chatSession?.id)
      }
    }
    openChatPanelModal(this.modalCtrl,options)
  }
  //selectedIssue:string='';

  matchedCounselor: { name: string; specialty: string } | null = null;

  questions = [
    {
      title: '如何应对焦虑？',
      advice: '尝试深呼吸和正念冥想，保持规律的作息，健康饮食，设定小目标，培养兴趣爱好，适当锻炼。',
      expanded: false,
    },
    {
      title: '如何提高自信心？',
      advice: '设定小目标并逐步实现，进行积极自我对话，培养技能，保持健康的生活方式，多与他人社交。',
      expanded: false,
    },
    {
      title: '如何改善人际关系？',
      advice: '积极多与他人沟通，倾听对方的感受，理解他人的感受，多参加社交活动，保持积极的态度，尊重他人。',
      expanded: false,
    },
  ];

  matchCounselor() {
  }
  goChat(){
      this.router.navigateByUrl("/chat/session/role/2DXJkRsjXK")
  } 
  ngOnInit() {
    this.loadChatPartnerList()
  }

  chatpartnerList:Array<CloudObject>=[]
  async loadChatPartnerList(){
    let query = new CloudQuery("ChatPartner");
    this.chatpartnerList = await query.find()
  }
}