package com.lantone.qc.kernel.structure.ai.process;

import com.alibaba.fastjson.JSONObject;
import com.lantone.qc.kernel.structure.ai.model.EntityEnum;
import com.lantone.qc.kernel.structure.ai.model.Lemma;
import com.lantone.qc.kernel.util.DiagEnhancer;
import com.lantone.qc.pub.model.entity.BodyPart;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.entity.OuterCourtyard;
import com.lantone.qc.pub.model.entity.PD;
import com.lantone.qc.pub.model.entity.Pacs;
import com.lantone.qc.pub.model.entity.PacsValue;
import com.lantone.qc.pub.model.entity.Possible;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class EntityProcessPacs extends EntityProcess {
    private Logger logger = LoggerFactory.getLogger(EntityProcessPacs.class);

    public List<Pacs> extractEntity(JSONObject aiOut) {
        //辅检情况
        List<Pacs> pacses = new ArrayList<>();
        List<Lemma> pacsLemmas = createEntityTree(aiOut, EntityEnum.AUXILIARY_EXAMINATION.toString());
        for (Lemma lemma : pacsLemmas) {
            Pacs pacs = new Pacs();
            pacs.setName(lemma.getText());
            if (lemma.isHaveChildren()) {
                for (Lemma relationLemma : lemma.getRelationLemmas()) {
                    if (relationLemma.getProperty().equals(EntityEnum.AUXILIARY_DESCRIPT.toString())) {
                        pacs.setPacsValues(addPacsValue(relationLemma));
                    } else if (relationLemma.getProperty().equals(EntityEnum.BODY.toString())) {
                        pacs.setBodyPart(addBodyPart(relationLemma));
                    } else if (relationLemma.getProperty().equals(EntityEnum.OUTERCOURTYARD.toString())) {
                        pacs.setOuterCourtyard(addOuterCourtyard(relationLemma));
                    }
                }
            }
            pacses.add(pacs);
        }
        //辅检提取疾病
        List<Diag> diags = new ArrayList<>();
        try {
            //读取疾病信息
            List<Lemma> diagLemmas = createEntityTree(aiOut, EntityEnum.DIEASE.toString());
            for (Lemma lemma : diagLemmas) {
                Diag diag = DiagEnhancer.create(lemma.getText());
                if (lemma.isHaveChildren()) {
                    diag.setNegative(findNegative(lemma));//阴性
                    diag.setPossible(findT(lemma, new Possible(), EntityEnum.POSSIBLE.toString()));//可能的
                    diag.setPd(findT(lemma, new PD(), EntityEnum.TIME.toString()));//时间
                }
                diags.add(diag);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        Pacs pacs = new Pacs();
        pacs.setDiags(diags);
        pacses.add(pacs);
        //辅检时间(不是辅检具体项目时间)
        List<Lemma> pdLemmas = createEntityTree(aiOut, EntityEnum.TIME.toString());
        for (Lemma lemma : pdLemmas) {
            PD pd = new PD();
            pd.setName(lemma.getText());
            pacs = new Pacs();
            pacs.setPd(pd);
            pacses.add(pacs);
        }
        return pacses;
    }

    /**
     * 添加辅助检查描述
     *
     * @param relationLemma
     * @return
     */
    private PacsValue addPacsValue(Lemma relationLemma) {
        PacsValue pacsValue = new PacsValue();
        pacsValue.setName(relationLemma.getText());
        return pacsValue;
    }

    /**
     * 添加身体部位
     *
     * @param relationLemma
     * @return
     */
    private BodyPart addBodyPart(Lemma relationLemma) {
        BodyPart bodyPart = new BodyPart();
        bodyPart.setName(relationLemma.getText());
        return bodyPart;
    }

    /**
     * 添加外院
     *
     * @param relationLemma
     * @return
     */
    private OuterCourtyard addOuterCourtyard(Lemma relationLemma) {
        OuterCourtyard outerCourtyard = new OuterCourtyard();
        outerCourtyard.setName(relationLemma.getText());
        if (relationLemma.isHaveChildren()) {
            outerCourtyard.setPd(findTime(relationLemma));
            for (Lemma lemma : relationLemma.getRelationLemmas()) {
                if (lemma.getProperty().equals(EntityEnum.AUXILIARY_DESCRIPT.toString())) {
                    outerCourtyard.setPacsValue(addPacsValue(lemma));
                    break;
                }
            }
        }
        return outerCourtyard;
    }

}
