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.util.DiagEnhancer;
import com.lantone.structure.model.entity.*;
import com.lantone.structure.model.label.PastLabel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName : EntityProcessPast
 * @Description : 既往史
 * @Author : 楼辉荣
 * @Date: 2020-03-10 10:20
 */
@Component
public class EntityProcessPast extends EntityProcess {
    private Logger logger = LoggerFactory.getLogger(EntityProcessPast.class);

    public PastLabel extractEntity(JSONObject aiOut) {
        PastLabel pastLabel = new PastLabel();
        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));
                    //可能的
                    for (Lemma relationLemma : lemma.getRelationLemmas()) {
                        if (relationLemma.getProperty().equals(EntityEnum.POSSIBLE.toString())) {
                            Possible possible = new Possible();
                            possible.setName(relationLemma.getText());
                            diag.setPossible(possible);
                        }
                    }
                }
                pastLabel.addDiag(diag);
            }
            //手术名称
            List<Lemma> operationLemmas = createEntityTree(aiOut, EntityEnum.OPERATION.toString());
            for (Lemma lemma : operationLemmas) {
                Operation operation = new Operation();
                operation.setName(lemma.getText().replaceAll("[“”]",""));
                if (lemma.isHaveChildren()) {
                    //阴性
                    operation.setNegative(findNegative(lemma));
                    //时间
                    operation.setPd(findPD(lemma));
                    //手术结果
                    for (Lemma relationLemma : lemma.getRelationLemmas()) {
                        if (relationLemma.getProperty().equals(EntityEnum.OPERATION_RESULT.toString())) {
                            OperationResult operationResult = new OperationResult();
                            operationResult.setName(relationLemma.getText());
                            operation.setOperationResult(operationResult);
                        }
                    }
                    //手术原因
                    for (Lemma relationLemma : lemma.getRelationLemmas()) {
                        if (relationLemma.getProperty().equals(EntityEnum.DIEASE.toString())) {
                            Diag diag = DiagEnhancer.create(relationLemma.getText());
                            operation.setDiag(diag);
                        }
                    }
                }
                pastLabel.addOperation(operation);
            }
            //手术史
            List<Lemma> operationHistoryLemmas = createEntityTree(aiOut, EntityEnum.OPERATION_KEYWORD.toString());
            for (Lemma lemma : operationHistoryLemmas) {
                Operation operation = new Operation();
                operation.setName(lemma.getText().replaceAll("[“”]",""));
                if (lemma.isHaveChildren()) {
                    operation.setNegative(findNegative(lemma));//阴性
                }
                pastLabel.addOperation(operation);
            }

            //外伤史
            List<Lemma> woundLemmas = createEntityTree(aiOut, EntityEnum.INJURY.toString());
            for (Lemma lemma : woundLemmas) {
                Wound wound = new Wound();
                wound.setName(lemma.getText());
                if (lemma.isHaveChildren()) {
                    wound.setNegative(findNegative(lemma));//阴性
                    wound.setPd(findPD(lemma));//时间
                    wound.setBodyPart(findT(lemma, new BodyPart(), EntityEnum.BODY.toString()));//部位
                    wound.setDegree(findT(lemma, new Degree(), EntityEnum.TREND.toString()));//程度
                    wound.setTreat(findT(lemma, new Treat(), EntityEnum.CURE.toString()));//治疗
                    wound.setOperation(findT(lemma, new Operation(), EntityEnum.OPERATION.toString()));//手术
                }
                pastLabel.addWound(wound);
            }
            //过敏史
            List<Lemma> allergyLemmas = createEntityTree(aiOut, EntityEnum.ALLERGY.toString());
            for (Lemma lemma : allergyLemmas) {
                Allergy allergy = new Allergy();
                allergy.setName(lemma.getText());
                allergy.setNegative(findNegative(lemma));
                for (Lemma relationLemma : lemma.getRelationLemmas()) {
                    //食物过敏史
                    if (relationLemma.getProperty().equals(EntityEnum.FOOD_ALLERGY.toString())) {
                        AllergyFood allergyFood = new AllergyFood();
                        allergyFood.setName(relationLemma.getText());
                        allergyFood.setNegative(findNegative(relationLemma));//阴性
                        allergyFood.setPd(findPD(relationLemma));//时间
                        allergyFood.setDegree(findT(relationLemma, new Degree(), EntityEnum.TREND.toString()));//程度
                        allergyFood.setAllergyDesc(findT(relationLemma, new AllergyDesc(), EntityEnum.ALLERGY_SYMPTOM.toString()));//过敏表现
                        allergy.setAllergyFood(allergyFood);
                    }
                    //药物过敏史
                    if (relationLemma.getProperty().equals(EntityEnum.DRUG_ALLERGY.toString())) {
                        AllergyMedicine allergyMedicine = new AllergyMedicine();
                        allergyMedicine.setName(relationLemma.getText());
                        allergyMedicine.setNegative(findNegative(relationLemma));//阴性
                        allergyMedicine.setPd(findPD(relationLemma));//时间
                        allergyMedicine.setDegree(findT(relationLemma, new Degree(), EntityEnum.TREND.toString()));//程度
                        allergyMedicine.setAllergyDesc(findT(relationLemma, new AllergyDesc(), EntityEnum.ALLERGY_SYMPTOM.toString()));//过敏表现
                        allergy.setAllergyMedicine(allergyMedicine);
                    }
                }
                pastLabel.addAllergy(allergy);
            }
//            //食物过敏史
//            List<Lemma> allergyFoodLemmas = createEntityTree(aiOut, EntityEnum.FOOD_ALLERGY.toString());
//            for (Lemma lemma : allergyFoodLemmas) {
//                AllergyFood allergyFood = new AllergyFood();
//                allergyFood.setName(lemma.getText());
//                if (lemma.isHaveChildren()) {
//                    allergyFood.setNegative(findNegative(lemma));
//                    allergyFood.setPd(findPD(lemma));//时间
//                    allergyFood.setDegree(findT(lemma, new Degree(), EntityEnum.TREND.toString()));//程度
//                }
//                pastLabel.addAllergyFood(allergyFood);
//            }
//            //药物过敏史
//            List<Lemma> allergyMedicineLemmas = createEntityTree(aiOut, EntityEnum.DRUG_ALLERGY.toString());
//            for (Lemma lemma : allergyMedicineLemmas) {
//                AllergyMedicine allergyMedicine = new AllergyMedicine();
//                allergyMedicine.setName(lemma.getText());
//                if (lemma.isHaveChildren()) {
//                    allergyMedicine.setNegative(findNegative(lemma));
//                    allergyMedicine.setPd(findPD(lemma));//时间
//                    allergyMedicine.setDegree(findT(lemma, new Degree(), EntityEnum.TREND.toString()));//程度
//                }
//                pastLabel.addAllergyMedicine(allergyMedicine);
//            }
            //输血史
            List<Lemma> bloodTransfusionLemmas = createEntityTree(aiOut, EntityEnum.BLOOD_TRANSFUSION.toString());
            for (Lemma lemma : bloodTransfusionLemmas) {
                BloodTransfusion bloodTransfusion = new BloodTransfusion();
                bloodTransfusion.setName(lemma.getText());
                if (lemma.isHaveChildren()) {
                    bloodTransfusion.setNegative(findNegative(lemma));
                    bloodTransfusion.setQuantity(findT(lemma, new Quantity(), EntityEnum.QUANTITY.toString()));//数量
                    bloodTransfusion.setPd(findPD(lemma));//时间
                }
                pastLabel.addBloodTransfusion(bloodTransfusion);
            }
            //预防接种史
            List<Lemma> vaccinateLemmas = createEntityTree(aiOut, EntityEnum.VACCINATION.toString());
            for (Lemma lemma : vaccinateLemmas) {
                Vaccinate vaccinate = new Vaccinate();
                vaccinate.setName(lemma.getText());
                if (lemma.isHaveChildren()) {
                    vaccinate.setNegative(findNegative(lemma));
                }
                pastLabel.addVaccinate(vaccinate);
            }
            //传染病史
            List<Lemma> diagInfectiousLemmas = createEntityTree(aiOut, EntityEnum.INFECTIOUS_KEYWORD.toString());
            for (Lemma lemma : diagInfectiousLemmas) {
                DiagInfectious diagInfectious = new DiagInfectious();
                diagInfectious.setName(lemma.getText());
                if (lemma.isHaveChildren()) {
                    diagInfectious.setNegative(findNegative(lemma));
                }
                pastLabel.addDiagInfectious(diagInfectious);
            }

            //健康情况
            List<Lemma> healthLemmas = createEntityTree(aiOut, EntityEnum.HEALTH.toString());
            for (Lemma lemma : healthLemmas) {
                pastLabel.setHeathCondition(lemma.getText());
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return pastLabel;
    }
}
