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.*;
import com.lantone.qc.pub.model.label.PresentLabel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * 现病史处理
 */
public class EntityProcessClinic extends EntityProcess {
    private Logger logger = LoggerFactory.getLogger(EntityProcessClinic.class);

    public PresentLabel extractEntity(JSONObject aiOut) {
        PresentLabel presentLabel = new PresentLabel();
        try {
            //临床表现
            List<Lemma> clinicLemmas = createEntityTree(aiOut, EntityEnum.CLINICAL_FEATURE.toString());
            for (Lemma lemma : clinicLemmas) {
                Clinical clinical = new Clinical();
                clinical.setName(lemma.getText());

                List<PD> timestamp = new LinkedList<>();
                for (Lemma relationLemma : lemma.getRelationLemmas()) {
                    if (relationLemma.getProperty().equals(EntityEnum.NEGATIVE.toString())) {
                        clinical.setNegative(findNegative(lemma));
                    }
                    clinical.setModification(findT(lemma, new Modification(), EntityEnum.MODIFICATION.toString()));//修饰
                    clinical.setBodyPart(findT(lemma, new BodyPart(), EntityEnum.BODY.toString()));//部位
                    clinical.setTrend(findT(lemma, new Trend(), EntityEnum.TREND.toString()));//趋势
                    clinical.setCause(findT(lemma, new Cause(), EntityEnum.CAUSE.toString()));//诱因
                    clinical.setProperty(findT(lemma, new Property(), EntityEnum.PROPERTY.toString()));//性质
                    clinical.setDegree(findT(lemma, new Degree(), EntityEnum.DEGREE.toString()));//程度
                    clinical.setAggravate(findT(lemma, new Aggravate(), EntityEnum.AGGRAVATE.toString()));//加重因素
                    clinical.setRelief(findT(lemma, new Relief(), EntityEnum.RELIEF.toString()));//缓解因素
                    if (relationLemma.getProperty().equals(EntityEnum.TIME.toString())) {
                        String[] time_split = relationLemma.getText().split(",");
                        for (String time : time_split) {
                            PD pd = new PD();
                            String[] val_unit = new String[2];
                            if (time.trim().length() > 0) {
                                val_unit = extract_digit(time);
                            }
                            pd.setValue(val_unit[0]);
                            pd.setUnit(val_unit[1]);
                            pd.setName(time);
                            timestamp.add(pd);
                        }
                    }
                }
                clinical.setTimestamp(timestamp);
                presentLabel.add(presentLabel.getClinicals(), clinical);
            }
            //入院途径
            List<Lemma> beHospitalizedWayLemmas = createEntityTree(aiOut, EntityEnum.BEHOSPITALIZEDWAY.toString());
            for (Lemma lemma : beHospitalizedWayLemmas) {
                BeHospitalizedWay beHospitalizedWay = new BeHospitalizedWay();
                beHospitalizedWay.setName(lemma.getText());
                presentLabel.setBeHospitalizedWay(beHospitalizedWay);
            }
            //诊断信息
            EntityProcessDiag entityProcessDiag = new EntityProcessDiag();
            List<Diag> diags = entityProcessDiag.extractEntity(aiOut);
            presentLabel.setDiags(diags);

            //一般情况信息
            List<Lemma> geneLemmas = createEntityTree(aiOut, EntityEnum.GENERAL_DESCRIPT.toString());
            for (Lemma lemma : geneLemmas) {
                General general = new General();
                general.setName(lemma.getText());
                presentLabel.add(presentLabel.getGens(), general);
            }
            List<Lemma> causeLemmas = createEntityTree(aiOut, EntityEnum.CAUSE.toString());
            List<Cause> causes = new ArrayList<>();
            for (Lemma lemma : causeLemmas) {
                Cause cause = new Cause();
                cause.setName(lemma.getText());
                causes.add(cause);
            }
            presentLabel.setCauses(causes);

            //一般情况描述信息
            EntityProcessGenerals entityProcessGeneral = new EntityProcessGenerals();
            List<GeneralDesc> generals = entityProcessGeneral.extractEntity(aiOut);
            presentLabel.setGenerals(generals);

            //辅检信息
            EntityProcessPacs entityProcessLis = new EntityProcessPacs();
            List<Pacs> pacses = entityProcessLis.extractEntity(aiOut);
            presentLabel.setPacses(pacses);

            //治疗
            List<Lemma> cureLemmas = createEntityTree(aiOut, EntityEnum.CURE.toString());
            List<Treat> treats = new ArrayList<>();
            for (Lemma lemma : cureLemmas) {
                Treat treat = new Treat();
                treat.setName(lemma.getText());
                treats.add(treat);
            }
            presentLabel.setTreats(treats);
            //治疗
            List<Lemma> cureLemmas_me = createEntityTree(aiOut, EntityEnum.DRUG.toString());
            List<Medicine> medicines = new ArrayList<>();
            for (Lemma lemma : cureLemmas_me) {
                Medicine medicine = new Medicine();
                medicine.setName(lemma.getText());
                medicines.add(medicine);
            }
            presentLabel.setMedicines(medicines);

            //手术
            List<Lemma> operationLemmas = createEntityTree(aiOut, EntityEnum.OPERATION.toString());
            for (Lemma lemma : operationLemmas) {
                Operation operation = new Operation();
                operation.setName(lemma.getText());
                presentLabel.add(presentLabel.getOperations(), operation);
            }

            //时间
            List<Lemma> pdLemmas = createEntityTree(aiOut, EntityEnum.TIME.toString());
            List<PD> pds = new ArrayList<>();
            for (Lemma lemma : pdLemmas) {
                PD pd = new PD();
                pd.setName(lemma.getText());
                pds.add(pd);
            }
            presentLabel.setPds(pds);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return presentLabel;
    }
}
