package com.lantone.qc.trans.changx;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
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.comsis.ModelDocGenerate;
import com.lantone.qc.trans.comsis.Preproc;

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

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

    @Override
    public List<TransferRecordDoc> extract(MedrecVo medrecVo) {
        List<TransferRecordDoc> retList = Lists.newArrayList();

        Map<String, List<String>> contentMap = (Map) medrecVo.getContent().get("content");
        if (contentMap == null) {
            return retList;
        }

        Map<String, TransferIntoDoc> transferIntoDocMap = getTransferIntoDocMap(contentMap.get("转入"));
        Map<String, TransferOutDoc> transferOutDocMap = getTransferOutDocMap(contentMap.get("转出"));

        Set<String> transferRecordNameSet = Sets.newHashSet();
        transferRecordNameSet.addAll(transferIntoDocMap.keySet());
        transferRecordNameSet.addAll(transferOutDocMap.keySet());

        transferRecordNameSet.forEach(transferRecordName -> {
            TransferRecordDoc transferRecordDoc = new TransferRecordDoc();
            transferRecordDoc.setTransferRecordName(transferRecordName);
            transferRecordDoc.setTransferIntoDoc(transferIntoDocMap.get(transferRecordName));
            transferRecordDoc.setTransferOutDoc(transferOutDocMap.get(transferRecordName));
            retList.add(transferRecordDoc);
        });

        return retList;
    }


    /**************************************************转入*******************************************************/
    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> pageData = Preproc.extract_doc_pub(true, transferInto_pageDataTitles, content);
        pageData.put("病程记录内容",
                pageData.get("病程记录内容")
                        .replace(pageData.get("病程记录时间"), "")
                        .replace(pageData.get("病程记录名称"), "")
        );

        List<String> targetTitles = Lists.newArrayList();
        transferInto_sourceTitles.forEach(sourceTitle -> {
            String targetTitle = "";
            for (int index = 0; index < sourceTitle.length(); index++) {
                if (index == sourceTitle.length() - 1) {
                    targetTitle += sourceTitle.substring(index, index + 1);
                } else {
                    targetTitle += sourceTitle.substring(index, index + 1) + "[\\s\\p{Zs}]*";
                }
            }
            targetTitles.add(targetTitle);
        });

        Map<String, String> sourceMap = Preproc.extract_doc_pub(true, targetTitles, content);
        sourceMap.put("记录时间", sourceMap.get("病程记录时间"));

        TransferIntoDoc transferIntoDoc = ModelDocGenerate.transferIntoDocGen(sourceMap);
        transferIntoDoc.setText(content);
        transferIntoDoc.setPageData((Map) pageData);

        return transferIntoDoc;
    }

    private List<String> transferInto_pageDataTitles = Lists.newArrayList(
            "病程记录时间",
            "病程记录名称",
            "病程记录内容",
            "记录医师"
    );
    private List<String> transferInto_sourceTitles = Lists.newArrayList(
            "病程记录时间",
            "病程记录名称",
            "病程记录内容",
            "记录时间",
            "记录医师"
    );


    /**************************************************转出*******************************************************/
    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> pageData = Preproc.extract_doc_pub(true, transferOut_pageDataTitles, content);
        pageData.put("病程记录内容",
                pageData.get("病程记录内容")
                        .replace(pageData.get("病程记录时间"), "")
                        .replace(pageData.get("病程记录名称"), "")
        );

        List<String> targetTitles = Lists.newArrayList();
        transferOut_sourceTitles.forEach(sourceTitle -> {
            String targetTitle = "";
            for (int index = 0; index < sourceTitle.length(); index++) {
                if (index == sourceTitle.length() - 1) {
                    targetTitle += sourceTitle.substring(index, index + 1);
                } else {
                    targetTitle += sourceTitle.substring(index, index + 1) + "[\\s\\p{Zs}]*";
                }
            }
            targetTitles.add(targetTitle);
        });

        Map<String, String> sourceMap = Preproc.extract_doc_pub(true, targetTitles, content);
        sourceMap.put("记录时间", sourceMap.get("病程记录时间"));

        TransferOutDoc transferOutDoc = ModelDocGenerate.transferOutDocGen(sourceMap);
        transferOutDoc.setText(content);
        transferOutDoc.setPageData((Map) pageData);

        return transferOutDoc;
    }

    private List<String> transferOut_pageDataTitles = Lists.newArrayList(
            "病程记录时间",
            "病程记录名称",
            "病程记录内容",
            "记录医师"
    );
    private List<String> transferOut_sourceTitles = Lists.newArrayList(
            "病程记录时间",
            "病程记录名称",
            "病程记录内容",
            "记录时间",
            "记录医师"
    );

}