package com.lantone.qc.trans.comsis;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.*;
import com.lantone.qc.pub.model.doc.consultation.ConsultationApplicationDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationRecordDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationResultsDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDiscussionDoc;
import com.lantone.qc.pub.model.doc.operation.OperationInformedConsentDoc;
import com.lantone.qc.pub.model.doc.operation.OperationRecordDoc;
import com.lantone.qc.pub.model.doc.operation.OperationSafetyChecklistDoc;
import com.lantone.qc.pub.model.doc.operation.PreoperativeDiscussionDoc;
import com.lantone.qc.pub.model.label.CaseCharacteristicLabel;
import com.lantone.qc.pub.model.label.ChiefLabel;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.model.label.DiagnosisLabel;
import com.lantone.qc.pub.model.label.FamilyLabel;
import com.lantone.qc.pub.model.label.MaritalLabel;
import com.lantone.qc.pub.model.label.MenstrualLabel;
import com.lantone.qc.pub.model.label.PacsLabel;
import com.lantone.qc.pub.model.label.PastLabel;
import com.lantone.qc.pub.model.label.PersonalLabel;
import com.lantone.qc.pub.model.label.PresentLabel;
import com.lantone.qc.pub.model.label.TreatPlanLabel;
import com.lantone.qc.pub.model.label.VitalLabel;
import com.lantone.qc.pub.model.label.VitalLabelSpecial;
import com.lantone.qc.pub.util.StringUtil;

import java.util.List;
import java.util.Map;

/**
 * 各个模块文档生成，各地公用
 *
 * @Description:
 * @author: rengb
 * @time: 2020/3/6 10:15
 */
public class ModelDocGenerate {

    /**
     * 会诊-会诊记录
     *
     * @param structureMap
     * @return
     */
    public static ConsultationRecordDoc consultationRecordDocGen(Map<String, String> structureMap) {
        ConsultationRecordDoc consultationRecordDoc = new ConsultationRecordDoc();
        consultationRecordDoc.setStructureMap(structureMap);
        return consultationRecordDoc;
    }

    /**
     * 会诊-会诊申请单
     *
     * @param structureMap
     * @return
     */
    public static ConsultationApplicationDoc consultationApplicationDocGen(Map<String, String> structureMap) {
        ConsultationApplicationDoc consultationApplicationDoc = new ConsultationApplicationDoc();
        consultationApplicationDoc.setStructureMap(structureMap);
        return consultationApplicationDoc;
    }

    /**
     * 会诊-会诊结果单
     *
     * @param structureMap
     * @return
     */
    public static ConsultationResultsDoc consultationResultsDocGen(Map<String, String> structureMap) {
        ConsultationResultsDoc consultationResultsDoc = new ConsultationResultsDoc();
        consultationResultsDoc.setStructureMap(structureMap);
        return consultationResultsDoc;
    }

    /**
     * 手术-术后首次病程及谈话记录
     *
     * @param structureMap
     * @return
     */
    public static OperationDiscussionDoc operationDiscussionDocGen(Map<String, String> structureMap) {
        OperationDiscussionDoc operationDiscussionDoc = new OperationDiscussionDoc();
        operationDiscussionDoc.setStructureMap(structureMap);
        return operationDiscussionDoc;
    }

    /**
     * 手术-手术记录
     *
     * @param structureMap
     * @return
     */
    public static OperationRecordDoc operationRecordDocGen(Map<String, String> structureMap) {
        OperationRecordDoc operationRecordDoc = new OperationRecordDoc();
        operationRecordDoc.setStructureMap(structureMap);
        return operationRecordDoc;
    }

    /**
     * 手术-术前讨论、术前小结
     *
     * @param structureMap
     * @return
     */
    public static PreoperativeDiscussionDoc preoperativeDiscussionDocGen(Map<String, String> structureMap) {
        PreoperativeDiscussionDoc preoperativeDiscussionDoc = new PreoperativeDiscussionDoc();
        preoperativeDiscussionDoc.setStructureMap(structureMap);
        return preoperativeDiscussionDoc;
    }

    /**
     * 手术-手术知情同意书文档
     *
     * @param structureMap
     * @return
     */
    public static OperationInformedConsentDoc operationInformedConsentDocGen(Map<String, String> structureMap) {
        OperationInformedConsentDoc operationInformedConsentDoc = new OperationInformedConsentDoc();
        operationInformedConsentDoc.setStructureMap(structureMap);
        return operationInformedConsentDoc;
    }

    /**
     * 手术-手术安全核查表文档
     *
     * @param structureMap
     * @return
     */
    public static OperationSafetyChecklistDoc operationSafetyChecklistDocGen(Map<String, String> structureMap) {
        OperationSafetyChecklistDoc operationSafetyChecklistDoc = new OperationSafetyChecklistDoc();
        operationSafetyChecklistDoc.setStructureMap(structureMap);
        return operationSafetyChecklistDoc;
    }

    /**
     * 入院记录
     *
     * @param structureMap
     * @return
     */
    public static BeHospitalizedDoc beHospitalizedDocGen(Map<String, String> structureMap) {
        BeHospitalizedDoc beHospitalizedDoc = new BeHospitalizedDoc();

        ChiefLabel chiefLabel = new ChiefLabel();
        chiefLabel.setText(structureMap.get("主诉"));
        beHospitalizedDoc.setChiefLabel(chiefLabel);
//        structureMap.remove("主诉");

        PresentLabel presentLabel = new PresentLabel();
        presentLabel.setText(structureMap.get("现病史"));
        beHospitalizedDoc.setPresentLabel(presentLabel);
//        structureMap.remove("现病史");

        PastLabel pastLabel = new PastLabel();
        pastLabel.setText(structureMap.get("既往史"));
        beHospitalizedDoc.setPastLabel(pastLabel);
//        structureMap.remove("既往史");

        PersonalLabel personalLabel = new PersonalLabel();
        personalLabel.setText(structureMap.get("个人史"));
        beHospitalizedDoc.setPersonalLabel(personalLabel);
//        structureMap.remove("个人史");

        MaritalLabel maritalLabel = new MaritalLabel();
        if (structureMap.get("婚育史") != null) {
            maritalLabel.setText("婚育史：" + structureMap.get("婚育史"));
        }
        beHospitalizedDoc.setMaritalLabel(maritalLabel);
//        structureMap.remove("婚育史");

        MenstrualLabel menstrualLabel = new MenstrualLabel();
        if(structureMap.get("月经史") != null){
            if(structureMap.get("月经史").contains("月 经 史")){
                String text = structureMap.get("月经史").replaceAll("月 经 史|:|：","");
                if (text.startsWith("，")){
                    text = text.substring(1);
                }
                structureMap.put("月经史",text);
            }

        }
        menstrualLabel.setText("月经史：" + structureMap.get("月经史"));
        beHospitalizedDoc.setMenstrualLabel(menstrualLabel);
//        structureMap.remove("月经史");

        FamilyLabel familyLabel = new FamilyLabel();
        familyLabel.setText(structureMap.get("家族史"));
        beHospitalizedDoc.setFamilyLabel(familyLabel);
//        structureMap.remove("家族史");

        VitalLabel vitalLabel = new VitalLabel();
        vitalLabel.setText(structureMap.get("体格检查"));
        beHospitalizedDoc.setVitalLabel(vitalLabel);
//        structureMap.remove("体格检查");

        VitalLabelSpecial vitalLabelSpecial = new VitalLabelSpecial();
        vitalLabelSpecial.setText(structureMap.get("专科体格检查"));
        beHospitalizedDoc.setVitalLabelSpecial(vitalLabelSpecial);
//        structureMap.remove("专科体格检查");

        PacsLabel pacsLabel = new PacsLabel();
        pacsLabel.setText(structureMap.get("辅助检查"));
        beHospitalizedDoc.setPacsLabel(pacsLabel);
//        structureMap.remove("辅助检查");

        DiagLabel initialDiagLabel = new DiagLabel();
        initialDiagLabel.setText(structureMap.get("初步诊断"));
        beHospitalizedDoc.setInitialDiagLabel(initialDiagLabel);
//        structureMap.remove("初步诊断");

        DiagLabel revisedDiagLabel = new DiagLabel();
        revisedDiagLabel.setText(structureMap.get("修正诊断"));
        beHospitalizedDoc.setRevisedDiagLabel(revisedDiagLabel);
//        structureMap.remove("修正诊断");

        DiagLabel suppleDiagLabel = new DiagLabel();
        suppleDiagLabel.setText(structureMap.get("补充诊断"));
        beHospitalizedDoc.setSuppleDiagLabel(suppleDiagLabel);
//        structureMap.remove("补充诊断");

        beHospitalizedDoc.setStructureMap(structureMap);

        return beHospitalizedDoc;
    }

    /**
     * 输血/血制品病程记录
     *
     * @param structureMap
     * @return
     */
    public static ClinicalBloodDoc clinicalBloodDocGen(Map<String, String> structureMap) {
        ClinicalBloodDoc clinicalBloodDoc = new ClinicalBloodDoc();
        clinicalBloodDoc.setStructureMap(structureMap);
        return clinicalBloodDoc;
    }

    /**
     * 输血后效果评价
     *
     * @param structureMap
     * @return
     */
    public static ClinicBloodEffectDoc clinicBloodEffectDoc(Map<String, String> structureMap) {
        ClinicBloodEffectDoc clinicBloodEffectDoc = new ClinicBloodEffectDoc();
        clinicBloodEffectDoc.setStructureMap(structureMap);
        return clinicBloodEffectDoc;
    }


    /**
     * 危急值记录
     *
     * @param structureMap
     * @return
     */
    public static CrisisValueReportDoc crisisValueReportDocGen(Map<String, String> structureMap) {
        CrisisValueReportDoc crisisValueReportDoc = new CrisisValueReportDoc();
        crisisValueReportDoc.setStructureMap(structureMap);
        return crisisValueReportDoc;
    }

    /**
     * 病危通知书
     *
     * @param structureMap
     * @return
     */
    public static CriticallyIllNoticeDoc criticallyIllNoticeDocGen(Map<String, String> structureMap) {
        CriticallyIllNoticeDoc criticallyIllNoticeDoc = new CriticallyIllNoticeDoc();
        criticallyIllNoticeDoc.setStructureMap(structureMap);
        return criticallyIllNoticeDoc;
    }

    /**
     * 死亡病例讨论记录
     *
     * @param structureMap
     * @return
     */
    public static DeathCaseDiscussDoc deathCaseDiscussDocGen(Map<String, String> structureMap) {
        DeathCaseDiscussDoc deathCaseDiscussDoc = new DeathCaseDiscussDoc();
        deathCaseDiscussDoc.setStructureMap(structureMap);
        return deathCaseDiscussDoc;
    }

    /**
     * 死亡记录
     *
     * @param structureMap
     * @return
     */
    public static DeathRecordDoc deathRecordDocGen(Map<String, String> structureMap) {
        DeathRecordDoc deathRecordDoc = new DeathRecordDoc();
        deathRecordDoc.setStructureMap(structureMap);
        return deathRecordDoc;
    }

    /**
     * 疑难病例讨论记录
     *
     * @param structureMap
     * @return
     */
    public static DifficultCaseDiscussDoc difficultCaseDiscussDocGen(Map<String, String> structureMap) {
        DifficultCaseDiscussDoc difficultCaseDiscussDoc = new DifficultCaseDiscussDoc();
        difficultCaseDiscussDoc.setStructureMap(structureMap);
        return difficultCaseDiscussDoc;
    }

    /**
     * 医嘱信息
     *
     * @param structureMap
     * @return
     */
    public static DoctorAdviceDoc doctorAdviceDocGen(Map<String, String> structureMap) {
        DoctorAdviceDoc doctorAdviceDoc = new DoctorAdviceDoc();
        doctorAdviceDoc.setStructureMap(structureMap);
        return doctorAdviceDoc;
    }

    /**
     * 值班交接制度
     *
     * @param structureMap
     * @return
     */
    public static DutyShiftSystemDoc dutyShiftSystemDocGen(Map<String, String> structureMap) {
        DutyShiftSystemDoc dutyShiftSystemDoc = new DutyShiftSystemDoc();
        dutyShiftSystemDoc.setStructureMap(structureMap);
        return dutyShiftSystemDoc;
    }

    /**
     * 首次病程录
     *
     * @param structureMap
     * @return
     */
    public static FirstCourseRecordDoc firstCourseRecordDocGen(Map<String, String> structureMap) {
        FirstCourseRecordDoc firstCourseRecordDoc = new FirstCourseRecordDoc();

        DiagLabel initialDiagLabel = new DiagLabel();
        initialDiagLabel.setText(structureMap.get("初步诊断"));
        firstCourseRecordDoc.setInitialDiagLabel(initialDiagLabel);
//        structureMap.remove("初步诊断");

        CaseCharacteristicLabel caseCharacteristicLabel = new CaseCharacteristicLabel();
        caseCharacteristicLabel.setText(structureMap.get("病例特点"));
        firstCourseRecordDoc.setCaseCharacteristicLabel(caseCharacteristicLabel);
//        structureMap.remove("病例特点");

        DiagnosisLabel diagnosisLabel = new DiagnosisLabel();
        diagnosisLabel.setText(structureMap.get("诊断依据"));
        firstCourseRecordDoc.setDiagnosisLabel(diagnosisLabel);
//        structureMap.remove("诊断依据");

        DiagLabel differentialDiagLabel = new DiagLabel();
        differentialDiagLabel.setText(structureMap.get("鉴别诊断"));
        firstCourseRecordDoc.setDifferentialDiagLabel(differentialDiagLabel);
//        structureMap.remove("鉴别诊断");

        TreatPlanLabel treatPlanLabel = new TreatPlanLabel();
        treatPlanLabel.setText(structureMap.get("诊疗计划"));
        firstCourseRecordDoc.setTreatPlanLabel(treatPlanLabel);
//        structureMap.remove("诊疗计划");

        firstCourseRecordDoc.setStructureMap(structureMap);
        return firstCourseRecordDoc;
    }

    /**
     * 出院小结
     *
     * @param structureMap
     * @return
     */
    public static LeaveHospitalDoc leaveHospitalDocGen(Map<String, String> structureMap) {
        LeaveHospitalDoc leaveHospitalDoc = new LeaveHospitalDoc();

        DiagLabel beDiagLabel = new DiagLabel();
        beDiagLabel.setText(structureMap.get("入院诊断"));
        leaveHospitalDoc.setBeHospitalizedLabel(beDiagLabel);

        DiagLabel leaveDiagLabel = new DiagLabel();
        leaveDiagLabel.setText(structureMap.get("出院诊断"));
        leaveHospitalDoc.setLeaveDiagLabel(leaveDiagLabel);

        leaveHospitalDoc.setStructureMap(structureMap);

        return leaveHospitalDoc;
    }

    /**
     * 分级护理制度
     *
     * @param structureMap
     * @return
     */
    public static NursingSystemDoc nursingSystemDocGen(Map<String, String> structureMap) {
        NursingSystemDoc nursingSystemDoc = new NursingSystemDoc();
        nursingSystemDoc.setStructureMap(structureMap);
        return nursingSystemDoc;
    }

    /**
     * 抢救记录
     *
     * @param structureMap
     * @return
     */
    public static RescueDoc rescueDocGen(Map<String, String> structureMap) {
        RescueDoc rescueDoc = new RescueDoc();
        rescueDoc.setStructureMap(structureMap);
        return rescueDoc;
    }

    /**
     * 病重通知书
     *
     * @param structureMap
     * @return
     */
    public static SeriouslyIllNoticeDoc seriouslyIllNoticeDocGen(Map<String, String> structureMap) {
        SeriouslyIllNoticeDoc seriouslyIllNoticeDoc = new SeriouslyIllNoticeDoc();
        seriouslyIllNoticeDoc.setStructureMap(structureMap);
        return seriouslyIllNoticeDoc;
    }

    /**
     * 阶段小结
     *
     * @param structureMap
     * @return
     */
    public static StagesSummaryDoc stagesSummaryDocGen(Map<String, String> structureMap) {
        StagesSummaryDoc stagesSummaryDoc = new StagesSummaryDoc();
        stagesSummaryDoc.setStructureMap(structureMap);
        return stagesSummaryDoc;
    }

    /**
     * 查房记录
     *
     * @param structureMap
     * @return
     */
    public static ThreeLevelWardDoc threeLevelWardDocGen(Map<String, String> structureMap) {
        ThreeLevelWardDoc threeLevelWardDoc = new ThreeLevelWardDoc();
        threeLevelWardDoc.setStructureMap(structureMap);
        return threeLevelWardDoc;
    }

    /**
     * 拼接结构化数据
     **/
    public static String structureMapJoin(Map<String, String> structureMap, List<String> keys) {
        String textJoin = "";
        for (String key : keys) {
            if (StringUtil.isNotBlank(structureMap.get(key))) {
                textJoin += key + "：" + structureMap.get(key) + "\n";
            }
        }
        return textJoin;
    }

}