package org.diagbot.graphWeb.work;

import org.diagbot.common.push.bean.FeatureRate;
import org.diagbot.common.push.bean.ResponseData;
import org.diagbot.common.push.bean.SearchData;
import org.diagbot.common.push.bean.neo4j.*;
import org.diagbot.pub.utils.PropertiesUtil;
import org.diagbot.pub.utils.http.HttpApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 推送诊断(疑诊,急诊,鉴别诊断)和治疗方案,不良反应,指标推送,管理评估
 */
public class DiseaseCalculate {
    Logger logger = LoggerFactory.getLogger(DiseaseCalculate.class);

    public ResponseData getDisease(SearchData searchData) throws Exception {
        ResponseData responseData = new ResponseData();
        String[] featureSplits = searchData.getFeatureType().split(",");
        List<String> featureList = Arrays.asList(featureSplits);
        Integer diseaseType = searchData.getDisType();
        PropertiesUtil propertiesUtil = new PropertiesUtil("url.properties");
        //调用图谱API
        HttpApi<ResponseData> api = new HttpApi<>();
        ResponseData diseaseResponse = api.doPost(propertiesUtil.getProperty("disease.http.url"), searchData,
                ResponseData.class);
        //走诊断推送(诊断推送包含诊断,指标和量表,管理评估)
        List<FeatureRate> dis = (List<FeatureRate>) diseaseResponse.getDis();
        List<FeatureRate> diseaseName = this.getDisease(dis);

        //走治疗推送(治疗里面包含治疗方案和不良反应)
        Map<String, Filnlly> treat = this.getTreat(diseaseResponse.getTreat());

        //走量表和指标推送
        List<MedicalIndication> medicalIndications = this.getMedicalIndications(diseaseResponse.getMedicalIndications());
        responseData.setMedicalIndications(medicalIndications);

        //走管理评估
//        Map managementEvaluation = diseaseResponse.getManagementEvaluation();
//        Map test = this.getMan(managementEvaluation);
//
//        responseData.setManagementEvaluation(test);
        responseData.setTreat(treat);
        responseData.setDis(diseaseName);
        return responseData;
    }

    public List<FeatureRate> getDisease(List<FeatureRate> dis) {
        List<FeatureRate> diseaseName = new ArrayList<>();
        if (dis != null && dis.size() > 0) {
            for (FeatureRate d : dis) {
                FeatureRate featureRate = new FeatureRate();
                featureRate.setFeatureName(d.getFeatureName());
                featureRate.setExtraProperty(d.getExtraProperty());
                featureRate.setDesc(d.getDesc());
                featureRate.setRate(d.getRate());
                diseaseName.add(featureRate);
            }
        }
        return diseaseName;
    }

    public Map<String, Filnlly> getTreat(Map<String, Filnlly> treat) {
        Map<String, Filnlly> diseaseTreat = new HashMap<String, Filnlly>();
        if (treat != null) {
            for (Map.Entry<String, Filnlly> tm : treat.entrySet()) {
                String dise = tm.getKey();
                Filnlly value = tm.getValue();
                List<Indicators> adverseEvent = value.getAdverseEvent();
                List<Indicators> newAdverseEvent = new ArrayList<>();
                if (adverseEvent != null && adverseEvent.size() > 0) {
                    for (Indicators i : adverseEvent) {
                        Indicators indicators = new Indicators();
                        indicators.setName(i.getName());
                        indicators.setControlType(i.getControlType());
                        List<Detail> details = i.getDetails();
                        List<Detail> newDetails = new ArrayList<>();
                        if (details != null && details.size() > 0) {
                            for (Detail d : details) {
                                Detail detail = new Detail();
                                detail.setName(d.getName());
                                detail.setValue(d.getValue());
                                newDetails.add(detail);
                            }
                        }
                        indicators.setDetails(newDetails);
                        newAdverseEvent.add(indicators);
                    }
                }

                ArrayList<Drugs> treatment = value.getTreatment();
                ArrayList<Drugs> newTreatment = new ArrayList<>();
                if (treatment != null && treatment.size() > 0) {
                    for (Drugs d : treatment) {
                        Drugs drugs = new Drugs();
                        String bigdrugsName = d.getBigdrugsName();
                        String subdrugsName = d.getSubdrugsName();
                        Integer drugsForbidden = d.getDrugsForbidden();
                        LinkedList<Medicition> medicitionsList = d.getMedicitionsList();
                        LinkedList<Medicition> newMedicitionsList = new LinkedList<>();
                        if (medicitionsList != null && medicitionsList.size() > 0) {
                            for (Medicition m : medicitionsList) {
                                Medicition newMedication = new Medicition();
                                newMedication.setMedicitionName(m.getMedicitionName());
                                newMedication.setForbidden(m.getForbidden());
                                newMedication.setIsShow(m.getIsShow());
                                newMedication.setRate(m.getRate());
                                newMedicitionsList.add(newMedication);
                            }
                        }
                        if(bigdrugsName.equals(subdrugsName)){
                            subdrugsName = "";
                        }
                        drugs.setBigdrugsName(bigdrugsName);
                        drugs.setSubdrugsName(subdrugsName);
                        drugs.setDrugsForbidden(drugsForbidden);
                        drugs.setMedicitionsList(newMedicitionsList);
                        newTreatment.add(drugs);
                    }
                }
                Filnlly filnlly = new Filnlly();
                filnlly.setTreatment(newTreatment);
                filnlly.setAdverseEvent(newAdverseEvent);
                diseaseTreat.put(dise, filnlly);
            }
        }
        return diseaseTreat;
    }

    public List<MedicalIndication> getMedicalIndications(List<MedicalIndication> medicalIndications) {
        List<MedicalIndication> newMedicalIndicationList = new ArrayList<>();
        if (medicalIndications != null && medicalIndications.size() > 0) {
            for (MedicalIndication m : medicalIndications) {
                MedicalIndication newMedicalIndication = new MedicalIndication();
                String name = m.getName();
                List<MedicalIndicationDetail> newDetails = new ArrayList<>();
                List<MedicalIndicationDetail> details = m.getDetails();
                if (details != null && details.size() > 0) {
                    for (MedicalIndicationDetail detail : details) {
                        MedicalIndicationDetail newMe = new MedicalIndicationDetail();
                        newMe.setType(detail.getType());
                        newMe.setContent(detail.getContent());
                        newDetails.add(newMe);
                    }
                }
                newMedicalIndication.setName(name);
                newMedicalIndication.setDetails(newDetails);
                newMedicalIndicationList.add(newMedicalIndication);
            }
        }
        return newMedicalIndicationList;
    }

    public Map getMan(Map input) {
        Map resultMap = new HashMap();
        Map lixiao = (HashMap) input.get("疗效评估");
        Map newlixiao = new HashMap();
        List<Map> lxRows = (ArrayList) lixiao.get("rows");
        List<Map> newlxRows = new ArrayList();
        if (lxRows != null && lxRows.size() > 0) {
            for (Map item : lxRows) {
                Map k = new HashMap();
                k.put("controltype", item.get("controltype"));
                k.put("name", item.get("name"));
                ArrayList<Map> h = (ArrayList<Map>) item.get("details");
                ArrayList<Map> newh = new ArrayList<>();
                if (h != null && h.size() > 0) {
                    for (Map d : h) {
                        Map ll = new HashMap();
                        ll.put("detailName", d.get("detailName"));
                        ll.put("state", d.get("state"));
                        newh.add(ll);
                    }
                }
                k.put("details", newh);
                newlxRows.add(k);
            }
        }
        newlixiao.put("rows", newlxRows);

        Map fenji = (HashMap) input.get("糖尿病分级管理");
        Map newfenji = new HashMap();
        List<Map> fjRows = (ArrayList) fenji.get("rows");
        List<Map> newfjRows = new ArrayList();
        if (fjRows != null && fjRows.size() > 0) {
            for (Map item : fjRows) {
                Map k = new HashMap();
                k.put("controltype", item.get("controltype"));
                k.put("name", item.get("name"));
                ArrayList<Map> h = (ArrayList<Map>) item.get("details");
                ArrayList<Map> newh = new ArrayList<>();
                if (h != null && h.size() > 0) {
                    for (Map d : h) {
                        Map ll = new HashMap();
                        ll.put("detailName", d.get("detailName"));
                        ll.put("state", d.get("state"));
                        newh.add(ll);
                    }
                }
                k.put("details", newh);
                newfjRows.add(k);
            }
        }
        newfenji.put("rows", newfjRows);
        resultMap.put("疗效评估", newlixiao);
        resultMap.put("糖尿病分级管理", newfenji);
        return resultMap;
    }
}
