package com.lantone.structure.ai.process;


import com.alibaba.fastjson.JSONObject;
import com.lantone.structure.ai.model.EntityEnum;
import com.lantone.structure.ai.model.Lemma;
import com.lantone.structure.model.entity.*;
import com.lantone.structure.model.label.RescueLabel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * 抢救记录处理
 */
public class EntityProcessRescue extends EntityProcess {
    private Logger logger = LoggerFactory.getLogger(EntityProcessRescue.class);

    public RescueLabel extractEntity(JSONObject aiOut) {
        RescueLabel rescueLabel = new RescueLabel();
        try {
            //疾病诊断名称
            List<Lemma> diagnosisLemma = createEntityTree(aiOut, EntityEnum.DIAGNOSIS_NAME.toString());
            List<Diagnosis> diagnosisList = new ArrayList<>();
            for (Lemma lemma : diagnosisLemma) {
                Diagnosis diagnosis = new Diagnosis();
                diagnosis.setName(lemma.getText());
                diagnosisList.add(diagnosis);
            }
            rescueLabel.setDiagnosis(diagnosisList);

            //抢救病情
            List<Lemma> conditionLemma = createEntityTree(aiOut, EntityEnum.CONDITION.toString());
            List<Condition> conditionList = new ArrayList<>();
            for (Lemma lemma : conditionLemma) {
                Condition condition = new Condition();
                condition.setName(lemma.getText());
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemma : lemma.getRelationLemmas()) {
                        if (relationLemma.getProperty().equals(EntityEnum.TIME.toString())) {
                            condition.setPd(addPD(relationLemma));
                        }
                    }
                }
                conditionList.add(condition);
            }
            rescueLabel.setConditions(conditionList);

            //手术及操作名称
            List<Lemma> operationLemma = createEntityTree(aiOut, EntityEnum.OPERATION_OF_NAME.toString());
            List<Operation> operationList = new ArrayList<>();
            for (Lemma lemma : operationLemma) {
                Operation operation = new Operation();
                operation.setName(lemma.getText());
                operation.setOperationLocation(findT(lemma, new OperationLocation(), EntityEnum.OPERATION_OF_POSITION_NAME.toString()));
                operationList.add(operation);
            }
            rescueLabel.setOperations(operationList);

            //操作方法
            List<Lemma> methodLemma = createEntityTree(aiOut, EntityEnum.OPERATION_METHOD.toString());
            List<Method> methodList = new ArrayList<>();
            for (Lemma lemma : methodLemma) {
                Method method = new Method();
                method.setName(lemma.getText());
                method.setCount(findT(lemma, new Count(), EntityEnum.OPERATION_COUNT.toString()));
                methodList.add(method);
            }
            rescueLabel.setMethods(methodList);

            //辅检名称
            List<Lemma> auxiliaryTestLemma = createEntityTree(aiOut, EntityEnum.AUXILIARY_TEST.toString());
            List<AuxiliaryTest> auxiliaryTestList = new ArrayList<>();
            for (Lemma lemma : auxiliaryTestLemma) {
                AuxiliaryTest auxiliaryTest = new AuxiliaryTest();
                auxiliaryTest.setName(lemma.getText());
                if (lemma.isHaveChildren()) {
                    List<AuxiliaryResult> auxiliaryResultList = new ArrayList<>();
                    for (Lemma relationLemma : lemma.getRelationLemmas()) {
                        if (relationLemma.getProperty().equals(EntityEnum.AUXILIARY_RESULT.toString())) {
                            AuxiliaryResult auxiliaryResult = new AuxiliaryResult();
                            auxiliaryResult.setName(relationLemma.getText());
                            auxiliaryResultList.add(auxiliaryResult);
                        }
                    }
                    auxiliaryTest.setAuxiliaryResult(auxiliaryResultList);
                }
                auxiliaryTest.setAuxiliaryQuantitative(findT(lemma, new AuxiliaryQuantitative(), EntityEnum.AUXILIARY_QUANTITATIVE.toString()));
                auxiliaryTestList.add(auxiliaryTest);
            }
            rescueLabel.setAuxiliaryTest(auxiliaryTestList);

            //抢救药品
            List<Lemma> drugLemma = createEntityTree(aiOut, EntityEnum.SALVAGE_DRUG.toString());
            List<Drug> DrugList = new ArrayList<>();
            for (Lemma lemma : drugLemma) {
                Drug drug = new Drug();
                drug.setName(lemma.getText());
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemma : lemma.getRelationLemmas()) {
                        if (relationLemma.getProperty().equals(EntityEnum.TIME.toString())) {
                            drug.setPd(addPD(relationLemma));
                        }
                    }
                }
                DrugList.add(drug);
            }
            rescueLabel.setDrugs(DrugList);

            //介入物名称
            List<Lemma> interventionLemma = createEntityTree(aiOut, EntityEnum.INTERVENTION.toString());
            List<Intervention> interventionList = new ArrayList<>();
            for (Lemma lemma : interventionLemma) {
                Intervention intervention = new Intervention();
                intervention.setName(lemma.getText());
                interventionList.add(intervention);
            }
            rescueLabel.setInterventions(interventionList);

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


    /**
     * 时间
     *
     * @param timeLemma
     * @return
     */
    private PD addPD(Lemma timeLemma) {
        PD pd = new PD();
        pd.setName(timeLemma.getText());
        pd.setValue(timeLemma.getText());
        return pd;
    }
}
