package com.lantone.qc.trans.changx;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.qc.pub.model.doc.transferrecord.TransferIntoDoc;
import com.lantone.qc.pub.model.doc.transferrecord.TransferOutDoc;
import com.lantone.qc.pub.model.doc.transferrecord.TransferRecordDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.changx.util.CxXmlUtil;
import com.lantone.qc.trans.comsis.ModelDocGenerate;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.comsis.Preproc;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Description: 转科记录文档生成
 * @author: rengb
 * @time: 2020/3/20 16:10
 */
public class ChangxTransferRecordDocTrans extends ModelDocTrans {

    @Override
    public TransferRecordDoc extract(MedrecVo medrecVo) {
        TransferRecordDoc transferRecordDoc = new TransferRecordDoc();

        Map<String, List<String>> contentMap = (Map) medrecVo.getContent().get("content");
        if (contentMap == null) {
            return transferRecordDoc;
        }
        List<String> into = contentMap.get("转入记录");
        List<String> out = contentMap.get("转出记录");
        List<String> all = new ArrayList<>();
        all.addAll(into);
        all.addAll(out);
        Map<String, TransferIntoDoc> transferIntoDocMap = getTransferIntoDocMap(into);
        Map<String, TransferOutDoc> transferOutDocMap = getTransferOutDocMap(out);
        Map<String, TransferRecordDoc> transferAllDocMap = getTransferAllDocMap(all);

        //转入
        Map<Date, TransferIntoDoc> dateRecordIn = new TreeMap<>(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                // 升序排列
                return o1.compareTo(o2);
            }
        });
        for (TransferIntoDoc transferIntoDoc : transferIntoDocMap.values()) {
            Map<String, String> structureMap = transferIntoDoc.getStructureMap();
            String inDateStr = structureMap.get("转科日期");
            if (StringUtil.isBlank(inDateStr)) {
                continue;
            }
            Date inDate = StringUtil.parseDateTime(inDateStr);
            if (inDate == null) {
                continue;
            }
            dateRecordIn.put(inDate, transferIntoDoc);
        }
        transferRecordDoc.setTransferIntoDocs(new ArrayList<>(dateRecordIn.values()));
        //转出
        Map<Date, TransferOutDoc> dateRecordOut = new TreeMap<>(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                // 升序排列
                return o1.compareTo(o2);
            }
        });
        for (TransferOutDoc transferOutDoc : transferOutDocMap.values()) {
            Map<String, String> structureMap = transferOutDoc.getStructureMap();
            String outDateStr = structureMap.get("转科日期");
            if (StringUtil.isBlank(outDateStr)) {
                continue;
            }
            Date outDate = StringUtil.parseDateTime(outDateStr);
            if (outDate == null) {
                continue;
            }
            dateRecordOut.put(outDate, transferOutDoc);
        }
        transferRecordDoc.setTransferOutDocs(new ArrayList<>(dateRecordOut.values()));

        //全部转科记录
        Map<Date, TransferRecordDoc> dateRecordAll = new TreeMap<>(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                // 升序排列
                return o1.compareTo(o2);
            }
        });
        for (TransferRecordDoc transferRecordAllDoc : transferAllDocMap.values()) {
            Map<String, String> structureMap = transferRecordAllDoc.getStructureMap();
            String transferDateStr = structureMap.get("转科日期");
            if (StringUtil.isBlank(transferDateStr)) {
                continue;
            }
            Date transferDate = StringUtil.parseDateTime(transferDateStr);
            if (transferDate == null) {
                continue;
            }
            dateRecordAll.put(transferDate, transferRecordAllDoc);
        }
        transferRecordDoc.setAllTransferDocs(new ArrayList<>(dateRecordAll.values()));

        return transferRecordDoc;
    }


    /**************************************************转入*******************************************************/
    private Map<String, TransferIntoDoc> getTransferIntoDocMap(List<String> contents) {
        Map<String, TransferIntoDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contents)) {
            return retMap;
        }
        int index = 1;
        String transferRecordName = null;
        for (String content : contents) {
            if (StringUtil.isBlank(content)) {
                continue;
            }
            transferRecordName = index + "";
            TransferIntoDoc transferIntoDoc = getTransferIntoDoc(content);
            transferIntoDoc.setTransferRecordName(transferRecordName);
            retMap.put(transferRecordName, transferIntoDoc);
            index++;
        }
        return retMap;
    }

    private TransferIntoDoc getTransferIntoDoc(String content) {
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue("//DocObjContent//Region", content);
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, keyContrasts);

//        sourceMap.put("记录时间", sourceMap.get("病程记录时间"));
        TransferIntoDoc transferIntoDoc = new TransferIntoDoc();
        transferIntoDoc.setText(content);
        transferIntoDoc.setStructureMap(structureMap);
        transferIntoDoc.setPageData((Map)xmlNodeValueMap);

        return transferIntoDoc;
    }

    /**************************************************全部*******************************************************/
    private Map<String, TransferRecordDoc> getTransferAllDocMap(List<String> contents) {
        Map<String, TransferRecordDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contents)) {
            return retMap;
        }
        int index = 1;
        String transferRecordName = null;
        for (String content : contents) {
            if (StringUtil.isBlank(content)) {
                continue;
            }
            transferRecordName = index + "";
            TransferRecordDoc transferRecordDoc = getTransferAllDoc(content);
            transferRecordDoc.setTransferRecordName(transferRecordName);
            retMap.put(transferRecordName, transferRecordDoc);
            index++;
        }
        return retMap;
    }

    private TransferRecordDoc getTransferAllDoc(String content) {
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue("//DocObjContent//Region", content);
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, keyContrasts);
        TransferRecordDoc transferAllDoc = new TransferRecordDoc();
        transferAllDoc.setText(content);
        transferAllDoc.setStructureMap(structureMap);
        transferAllDoc.setPageData((Map) xmlNodeValueMap);
        return transferAllDoc;
    }


    /**************************************************转出*******************************************************/
    private Map<String, TransferOutDoc> getTransferOutDocMap(List<String> contents) {
        Map<String, TransferOutDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contents)) {
            return retMap;
        }
        int index = 1;
        String transferRecordName = null;
        for (String content : contents) {
            if (StringUtil.isBlank(content)) {
                continue;
            }
            transferRecordName = index + "";
            TransferOutDoc transferOutDoc = getTransferOutDoc(content);
            transferOutDoc.setTransferRecordName(transferRecordName);
            retMap.put(transferRecordName, transferOutDoc);
            index++;
        }
        return retMap;
    }

    private TransferOutDoc getTransferOutDoc(String content) {
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue("//DocObjContent//Region", content);
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, keyContrasts);

        TransferOutDoc transferOutDoc = new TransferOutDoc();
        transferOutDoc.setText(content);
        transferOutDoc.setStructureMap(structureMap);
        transferOutDoc.setPageData((Map) xmlNodeValueMap);

        return transferOutDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "转出日期=转科日期",
            "转入日期=转科日期",
            "转出日期=转科日期",
            "姓名++++患者姓名=姓名",
            "签名++++=医师签名",
            "++++入院日期=入院日期",
            "入院诊断++++初步诊断=初步诊断",
            "转入目的++++目的=转入目的",
            "转出目的++++目的=转出目的"
    );
}