import { AgentTaskStep } from 'src/agent/agent.task';
import { getUserInput } from 'src/agent/agent.input';
import { ModalController } from '@ionic/angular/standalone';
import { FmodeChatCompletion } from 'fmode-ng';
import { extactAndParseJsonFromString } from 'src/agent/agent.json';

export function TaskInqueryUserAnswer(options:{
    modalCtrl:ModalController
    shareData:any}
    ):AgentTaskStep{
        /**
         shareData.userStory // 已经拥有的医生主动询问
         {
            "questionList": [
                {
                    "title": "头痛的性质",
                    "desc": "请问您的偏头痛是怎样的感觉？是刺痛、跳动还是压迫感？"
                },
            ]
        }
         */
        let task1 = new AgentTaskStep({title:"诊断：患者回答具体内容后，医生给出诊断结果",shareData:options.shareData})
        task1.handle = ()=>{
            return new Promise(async (resolve,reject)=>{
                // 获取用户输入的问题清单
                let questionList = options.shareData.userStory.questionList.map((item:any)=>{return {name:item.title,desc:item.desc,type:"text"}})
                
                let userInputResult = await getUserInput(options.modalCtrl,{fieldsArray:questionList});
                console.log(userInputResult)
                questionList.forEach((question:any)=>{
                    question.answer = userInputResult[question.name]
                })
            
                // 文本生成
                let qaContent = options.shareData.userStory.questionList.map((item:any)=>`医生问:${item.title},${item.desc}\n患者答:${item.answer||"没回答"}`).join("\n")
                let PromptTemplate = `您是一名专业的${options.shareData.userStory.keshi}主任医生，根据具体的询问，给出初步诊断。
                症状口述：${options.shareData.userStory['症状口述']}
                具体询问：${qaContent}
                结果以JSON格式表示：
                症状名称为具体的症状，症状描述为用户的感受，持续时间若没有直接说，可以写近期即可。
                {
                    "title":"病例标题",
                    "desc":"病情概括",
                    "content":"给出完整的治疗方案和建议"
                }
                `
                let completion = new FmodeChatCompletion([
                    {role:"system",content:""},
                    {role:"user",content:PromptTemplate}
                    ])
                    completion.sendCompletion().subscribe((message:any)=>{
                        if(task1.progress < 0.5){
                            task1.progress += 0.05
                        }
                        if(task1.progress >= 0.5 && task1.progress <= 0.8){
                            task1.progress += 0.01
                        }
                        if(task1.progress >= 0.8){
                            task1.progress += 0.001
                        }
                        // 打印消息体
                        console.log(message.content)
                        // 赋值消息内容给组件内属性
                        if(message.complete){ // 判断message为完成状态，则设置isComplete为完成
                            options.shareData.diagResult = extactAndParseJsonFromString(message.content)
                            task1.progress = 1
                            resolve(true)
                        }
                })
            })

        }
        return task1
}


const TestShareData = {
userStory : {
  "keshi": "神经内科",
  "sympList": [
      {
          "title": "偏头疼",
          "desc": "已经持续了2天了",
          "duration": "2天"
      },
      {
          "title": "发冷",
          "desc": "感觉已经有一天",
          "duration": "1天"
      }
  ],
  "症状口述": "偏头疼已经持续了2天了，发冷感觉已经有一天。",
  "questionList": [
      {
          "title": "头痛的性质",
          "desc": "请描述您的头痛是搏动性、压迫性还是其他类型？",
          "answer":"压迫性头疼"
      },
      {
          "title": "头痛的部位",
          "desc": "您的头痛主要集中在头部的哪个区域？",
          "answer":"左侧头疼"
      },
      {
          "title": "伴随症状",
          "desc": "除了头痛和发冷，您还有其他的症状吗？例如恶心、呕吐、视力模糊或对光敏感等？",
          "answer":"有点恶心但是没有呕吐"
      },
      {
          "title": "发冷的性质",
          "desc": "您感到发冷时是否伴随有发热、出汗或其他症状？",
          "answer":"没有"
      },
      {
          "title": "既往病史",
          "desc": "您是否有偏头痛或其他头痛的病史？",
          "answer":"没有"
      },
      {
          "title": "生活习惯",
          "desc": "您最近的生活习惯是否有变化？例如睡眠不足、压力增大或饮食不规律？",
          "answer":"经常熬夜，学习压力大"
      },
      {
          "title": "药物使用",
          "desc": "您是否有服用任何药物来缓解头痛或其他症状？",
          "answer":"没有"
      },
      {
          "title": "家族病史",
          "desc": "您的家族中是否有人有类似的头痛或神经系统疾病史？",
          "answer":"没有"
      },
      {
          "title": "过敏史",
          "desc": "您是否有药物或其他物质的过敏史？",
          "answer":"没有"
      }
  ]
}
}