package com.lantone.structure.facade.tran;
import com.lantone.common.util.ListUtil;
import com.lantone.structure.model.doc.ClinicalBloodDoc;
import com.lantone.structure.model.entity.Amount;
import com.lantone.structure.model.entity.Indication;
import com.lantone.structure.model.entity.ResponseType;
import com.lantone.structure.model.label.ClinicalBloodLabel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: rengb
 * @time: 2021/2/2 14:16
 */
@Slf4j
public class ClinicalBloodTran extends TargetTran {

    @Override
    public Map<String, String> convert(String text) {
        List<ClinicalBloodDoc> clinicalBloodDocList = new ArrayList<>();
        ClinicalBloodDoc clinicalBloodDoc = new ClinicalBloodDoc();
        clinicalBloodDoc.setText(text);
        clinicalBloodDocList.add(clinicalBloodDoc);
        inputInfo.setClinicalBloodDocs(clinicalBloodDocList);
        aiProcess();
        Map<String, String> structureMap = new HashMap<>();
        bloodContrast(inputInfo.getClinicalBloodDocs(), structureMap);
        return structureMap;
    }

        public void bloodContrast( List<ClinicalBloodDoc> clinicalBloodDocs, Map<String, String> retMap) {
            if(ListUtil.isNotEmpty(clinicalBloodDocs)) clinicalBloodDocs.forEach(clinicalBloodDoc -> {
                int indicationCount = 1;
                int responseTypeCount = 1;
                int amountCount = 1;
                int measureCount = 1;
                StringBuffer indicationString = new StringBuffer();
                StringBuffer responseTypeString = new StringBuffer();
                StringBuffer timeString = new StringBuffer();
                StringBuffer amountString = new StringBuffer();
                StringBuffer measureString = new StringBuffer();
                StringBuffer bloodNoteString = new StringBuffer();
                ClinicalBloodLabel clinicalBlood = clinicalBloodDoc.getClinicalBloodLabel();

                if (ListUtil.isNotEmpty(clinicalBlood.getIndication())) {
                    for (Indication indications : clinicalBlood.getIndication()) {
                        if (StringUtils.isNotEmpty(indications.getName())) {
                            if (clinicalBlood.getIndication().size() > 1) {
                                indicationString.append((indicationCount++) + ".");
                            }
                            indicationString.append(indications.getName());
                        }

                    }
                    retMap.put("输血指征", indicationString.toString());
                }

                if (clinicalBlood.getType() != null) {
                    if (ListUtil.isNotEmpty(clinicalBlood.getType().getAmount())) {
                        for (Amount amount : clinicalBlood.getType().getAmount()) {
                            if (amount != null && StringUtils.isNotEmpty(amount.getName())) {
                                if (clinicalBlood.getType().getAmount().size() > 1) {
                                    amountString.append((amountCount++) + ".");
                                }
                                amountString.append(amount.getName());
                                retMap.put("输血量（mL）", amountString.toString());
                            }
                            if (amount != null && amount.getMeasurementUnit() != null && StringUtils.isNotEmpty(amount.getMeasurementUnit().getName())) {
                                if (clinicalBlood.getType().getAmount().size() > 1) {
                                    measureString.append((measureCount++) + ".");
                                }
                                measureString.append(amount.getMeasurementUnit().getName());
                                retMap.put("输血量计量单位", measureString.toString());
                            }
                            if (StringUtils.isNotEmpty(clinicalBlood.getType().getName())) {
                                retMap.put("输血类型", clinicalBlood.getType().getName());
                            }
                        }
                    }
                }
                if (clinicalBlood.getReason() != null && StringUtils.isNotEmpty(clinicalBlood.getReason().getName())) {
                    retMap.put("输血原因", clinicalBlood.getReason().getName());
                }
                if (clinicalBlood.getStartTime() != null && StringUtils.isNotEmpty(clinicalBlood.getStartTime().getName())) {
                    timeString.append("输血开始时间:" + clinicalBlood.getStartTime().getName() + ";");
                    bloodNoteString.append("输血开始时间:" + clinicalBlood.getStartTime().getName() + ";");
                }
                if (clinicalBlood.getEndTime() != null && StringUtils.isNotEmpty(clinicalBlood.getEndTime().getName())) {
                    timeString.append("输血结束时间:" + clinicalBlood.getEndTime().getName() + ";");
                    bloodNoteString.append("输血结束时间:" + clinicalBlood.getEndTime().getName() + ";");

                }
                if (clinicalBlood.getStartTime() != null || clinicalBlood.getEndTime() != null) {
                    String timeStr = timeString.toString();
                    if(timeStr.endsWith(";")){
                        timeStr = timeStr.substring(0, timeStr.lastIndexOf(";"));
                    }
                    retMap.put("输血日期时间", timeStr);
                }

                if (ListUtil.isNotEmpty(clinicalBlood.getResponseType())) {
                    for (ResponseType responseTypes : clinicalBlood.getResponseType()) {
                        if (StringUtils.isNotEmpty(responseTypes.getName())) {
                            if (clinicalBlood.getResponseType().size() > 1) {
                                responseTypeString.append((responseTypeCount++) + ".");
                            }
                            responseTypeString.append(responseTypes.getNegative().getName() + responseTypes.getName());
                        }
                    }
                    retMap.put("输血反应类型", responseTypeString.toString());
                    bloodNoteString.append("输血反应类型:" +  responseTypeString.toString() + ";");
                }

                if (null != clinicalBlood.getFrequency() && StringUtils.isNotEmpty(clinicalBlood.getFrequency().getName())) {
                    retMap.put("输血次数", clinicalBlood.getFrequency().getName());
                    bloodNoteString.append("输血次数:" +  clinicalBlood.getFrequency().getName() + ";");
                }
                if (!retMap.containsKey("输血次数") && StringUtils.isEmpty(retMap.get("输血次数"))) {
                  if(StringUtils.isNotEmpty(retMap.get("输血量（mL）"))){
                        String amountRet = retMap.get("输血量（mL）");
                        int count = findString(amountRet, ".");
                        if(count==0){
                            count=1;
                        }
                        retMap.put("输血次数",(String.valueOf(count)+"次"));
                      bloodNoteString.append("输血次数:" +  (String.valueOf(count)+"次") + ";");
                   }
                }
                String bloodNoteStr = bloodNoteString.toString();
                if(bloodNoteStr.endsWith(";")){
                    bloodNoteStr = bloodNoteStr.substring(0, bloodNoteStr.lastIndexOf(";"));
                }
                retMap.put("输血过程记录",(bloodNoteStr));

            });
            }
    public int findString(String source,String target){
        //计数器
        int count = 0;
        //每次查询开始的位置
        int fromIndex = 0;
        while((fromIndex = source.indexOf(target, fromIndex)) != -1){
            count ++;
            fromIndex = fromIndex + target.length();
        }
        return count;
    }
}