package com.lantone.structure.ai.process;


import com.alibaba.fastjson.JSONObject;
import com.lantone.common.util.ListUtil;
import com.lantone.structure.ai.model.EntityEnum;
import com.lantone.structure.ai.model.Lemma;
import com.lantone.structure.model.Development;
import com.lantone.structure.model.doc.BeHospitalizedDoc;
import com.lantone.structure.model.entity.*;
import com.lantone.structure.model.label.PastLabel;
import com.lantone.structure.model.label.PersonalLabel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 恩泽过敏史、输血史、手术史、预防接种史、疾病史（含外伤）、传染病史
 */
public class EntityProcessEZaAll extends EntityProcess {
    private Logger logger = LoggerFactory.getLogger(EntityProcessEZaAll.class);

    public BeHospitalizedDoc extractEntity(JSONObject aiOut) {
        BeHospitalizedDoc beHospitalizedDoc = new BeHospitalizedDoc();
        PersonalLabel personalLabel = new PersonalLabel();
        PastLabel pastLabel= new PastLabel();
        List<BloodTransfusion> bloodTransfusions = new ArrayList<>();//输血史
        List<Vaccinate> vaccinates = new ArrayList<>();//预防接种史
        List<Diag> diags = new ArrayList<>();//疾病史
        List<Allergy> allergies = new ArrayList<>();//过敏史
        List<Operation> operations = new ArrayList<>();//手术史
        List<DiagInfectious> diagInfectiouses = new ArrayList<>();//传染病史
        Development development = new Development(); //生长发育史

        try {
            //生长发育史-
            List<Lemma> uprightType = createEntityTree(aiOut, EntityEnum.UPRIGHTTYPE.toString());
            if(ListUtil.isNotEmpty(uprightType)){
                long count = uprightType.stream().filter(obj -> EntityEnum.UPRIGHTTYPE.toString().equals(obj.getProperty())).count();
                if(count>0 && uprightType.size() == count){
                    development.setName(EntityEnum.UPRIGHTTYPE.toString());
                }
            }
            List<Lemma> astheniaType = createEntityTree(aiOut, EntityEnum.ASTHENIATYPE.toString());
            if(ListUtil.isNotEmpty(astheniaType)){
                long count = astheniaType.stream().filter(obj -> EntityEnum.ASTHENIATYPE.toString().equals(obj.getProperty())).count();
                if(count>0){
                    development.setName(EntityEnum.ASTHENIATYPE.toString());
                }
            }
            List<Lemma> superforceType = createEntityTree(aiOut, EntityEnum.SUPERFORCETYPE.toString());
            if(ListUtil.isNotEmpty(superforceType)){
                long count = superforceType.stream().filter(obj -> EntityEnum.SUPERFORCETYPE.toString().equals(obj.getProperty())).count();
                if(count>0){
                    development.setName(EntityEnum.SUPERFORCETYPE.toString());
                }
            }

            //输血史-
            List<Lemma> bloods = createEntityTree(aiOut, EntityEnum.BLOOD_TRANSFUSION.toString());
            for (Lemma lemma : bloods) {
                BloodTransfusion bloodTransfusionFir = new BloodTransfusion();
                String text = lemma.getText();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemmaRec : lemma.getRelationLemmas()) {
                        BloodTransfusion bloodTransfusionSec = new BloodTransfusion();
                        if (relationLemmaRec.getProperty().equals(EntityEnum.NEGATIVE.toString())) {
                            text = relationLemmaRec.getText()+text;
                            bloodTransfusionSec.setName(text);
                            bloodTransfusions.add(bloodTransfusionSec);
                        }
                    }
                }else{
                    if("输血史".equals(text)){
                        text = "有"+text;
                    }
                    bloodTransfusionFir.setName(text);
                    bloodTransfusions.add(bloodTransfusionFir);
                }
            }
            //预防接种史-
            List<Lemma> vaccinateList = createEntityTree(aiOut, EntityEnum.VACCINATION.toString());
            for (Lemma lemma : vaccinateList) {
                Vaccinate vaccinateFir = new Vaccinate();
                String text = lemma.getText();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemmaRec : lemma.getRelationLemmas()) {
                        Vaccinate vaccinateSec = new Vaccinate();
                        if (relationLemmaRec.getProperty().equals(EntityEnum.NEGATIVE.toString())) {
                            text = relationLemmaRec.getText()+text;
                            vaccinateSec.setName(text);
                            vaccinates.add(vaccinateSec);
                        }
                    }
                }else{
                    vaccinateFir.setName(text);
                    vaccinates.add(vaccinateFir);
                }
            }


            //疾病史  NEGATIVE-TIME  DIEASE
            //否定
            Set<String> diagSet = new HashSet<>();
            Set<String> diagRetSet = new HashSet<>();
            List<Lemma> diagList = createEntityTree(aiOut, EntityEnum.NEGATIVE.toString());
           /* if( ListUtil.isEmpty(diagList)){
                //时间
                diagList = createEntityTree(aiOut, EntityEnum.TIME.toString());
            }*/
            for (Lemma lemma : diagList) {
                Diag diagFir = new Diag();
                String text = lemma.getText();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemmaRec : lemma.getRelationLemmas()) {
                        Diag diagSec = new Diag();
                        //疾病名称
                        if (relationLemmaRec.getProperty().equals(EntityEnum.DIEASE.toString())||
                            relationLemmaRec.getProperty().equals(EntityEnum.INJURY.toString())) {
                            diagSet.add(relationLemmaRec.getText());
                          String flagText = text+relationLemmaRec.getText();
                            diagSec.setName(flagText);
                            diags.add(diagSec);
                        }
                    }
                }
            }

            //肯定
            List<Lemma>  diaList = createEntityTree(aiOut, EntityEnum.DIEASE.toString());
            if(ListUtil.isEmpty(diaList)){
                diaList = createEntityTree(aiOut, EntityEnum.INJURY.toString());
            }
            for (Lemma lemma : diaList) {
                String text = lemma.getText();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemmaRec : lemma.getRelationLemmas()) {
                    if(!diagSet.contains(lemma.getText())){
                        if(relationLemmaRec.getProperty().equals(EntityEnum.TIME.toString())){
                            diagRetSet.add(relationLemmaRec.getText()+text);
                        }else{
                            diagRetSet.add(text);
                        }
                    }
                    }
                }
            }
            if(null != diagRetSet && diagRetSet.size()>0){
                for (String str : diagRetSet) {
                    Diag diagSec = new Diag();
                    diagSec.setName(str);
                    diags.add(diagSec);
                }
            }


            //过敏史-药物过敏原表现为使用后皮疹
            //否定 NEGATIVE  过敏 ALLERGY 食物过敏原 FOOD_ALLERGY 药物过敏原 DRUG_ALLERGY
            Set<String> allergieSet = new HashSet<>();
            Set<String> allergieRetSet = new HashSet<>();
            List<Lemma> allergieList = createEntityTree(aiOut, EntityEnum.NEGATIVE.toString());
            for (Lemma lemma : allergieList) {
                Allergy allergyFir = new Allergy();
                String text = lemma.getText();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemma : lemma.getRelationLemmas()) {
                        Allergy allergySec= new Allergy();
                        //过敏
                        if (relationLemma.getProperty().equals(EntityEnum.ALLERGY.toString())) {
                            String flagText = relationLemma.getText();
                            String firText = text +relationLemma.getText();
                                if(ListUtil.isEmpty(relationLemma.getRelationLemmas())){
                                    allergySec.setName(firText);
                                    allergies.add(allergySec);
                                }
                            for (Lemma relationLemmaRe : relationLemma.getRelationLemmas()) {
                                Allergy allergyThr= new Allergy();
                                //食物过敏原 - 药物过敏原-情况不详
                                if (relationLemmaRe.getProperty().equals(EntityEnum.FOOD_ALLERGY.toString()) ||
                                    relationLemmaRe.getProperty().equals(EntityEnum.DRUG_ALLERGY.toString())||
                                    relationLemmaRe.getProperty().equals(EntityEnum.UNKNOWN.toString())) {
                                    String secText = text+relationLemmaRe.getText()+flagText;
                                    allergieSet.add(relationLemmaRe.getText()+flagText);
                                    if(ListUtil.isEmpty(relationLemmaRe.getRelationLemmas())){
                                        allergyThr.setName(secText);
                                        allergies.add(allergyThr);
                                    }
                                    for (Lemma relationLemmaRec : relationLemmaRe.getRelationLemmas()) {
                                        Allergy allergyFor = new Allergy();
                                        //过敏表现
                                        if (relationLemmaRec.getProperty().equals(EntityEnum.ALLERGY_SYMPTOM.toString())) {
                                            String thrText = secText+relationLemmaRec.getText();
                                            allergyFor.setName(thrText);
                                            allergies.add(allergyFor);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }else{
                    allergyFir.setName(text);
                    allergies.add(allergyFir);
                }
            }
            //确定
            List<Lemma> allergieLists = createEntityTree(aiOut, EntityEnum.ALLERGY.toString());
            for (Lemma lemma : allergieLists) {
                Allergy allergyFir = new Allergy();
                String text = lemma.getText();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemma : lemma.getRelationLemmas()) {
                //食物过敏原 - 药物过敏原-情况不详
                if (relationLemma.getProperty().equals(EntityEnum.FOOD_ALLERGY.toString()) ||
                        relationLemma.getProperty().equals(EntityEnum.DRUG_ALLERGY.toString())||
                        relationLemma.getProperty().equals(EntityEnum.UNKNOWN.toString())) {
                    String secText = relationLemma.getText()+text;
                    if(null == relationLemma.getRelationLemmas() || relationLemma.getRelationLemmas().size()==0){
                        if(!allergieSet.contains(secText)){
                            allergieRetSet.add(secText);
                        }
                    }
                        for (Lemma relationLemmaRec : relationLemma.getRelationLemmas()) {
                         //   Allergy allergyFor = new Allergy();
                            //过敏表现
                            if (relationLemmaRec.getProperty().equals(EntityEnum.ALLERGY_SYMPTOM.toString())) {
                                String thrText = secText+relationLemmaRec.getText();
                                if(!allergieSet.contains(thrText)){
                                    allergieRetSet.add(thrText);
                                }
                            }
                        }

                }
            }
        }else{
            allergyFir.setName(text);
            allergies.add(allergyFir);
        }

                if(null != allergieRetSet && allergieRetSet.size()>0){
                    for (String allergySecs : allergieRetSet) {
                        Allergy allergySec = new Allergy();
                        allergySec.setName(allergySecs);
                        String flagName = "";
                        if(allergies.size()>0) {
                            flagName = allergies.get(allergies.size() - 1).getName();
                        }
                        if(flagName.contains(allergySecs)){
                            continue;
                        }else {
                            allergies.add(allergySec) ;
                        }
                    }
                }
}

            //手术史
            //否定 NEGATIVE  时间 TIME 手术名称 OPERATION
            List<Lemma> operationList = createEntityTree(aiOut, EntityEnum.NEGATIVE.toString());
            for (Lemma lemma : operationList) {
                Operation operationFir = new Operation();
                String text = lemma.getText();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemmaRec : lemma.getRelationLemmas()) {
                        Operation operationSec = new Operation();
                        //手术史
                        if (relationLemmaRec.getProperty().equals(EntityEnum.OPERATION_KEYWORD.toString())) {
                            text = text+relationLemmaRec.getText();
                                operationSec.setName(text);
                                operations.add(operationSec);
                            for (Lemma relationLemmaThr : relationLemmaRec.getRelationLemmas()) {
                                operations.remove(0);
                                Operation operationThr = new Operation();
                                //时间-情况不详
                                if (relationLemmaThr.getProperty().equals(EntityEnum.TIME.toString()) ||
                                    relationLemmaThr.getProperty().equals(EntityEnum.UNKNOWN.toString())) {
                                    text =text+ relationLemmaThr.getText();
                                        operationThr.setName(text);
                                        operations.add(operationThr);
                                    for (Lemma relationLemmaFor : relationLemmaThr.getRelationLemmas()) {
                                        operations.remove(0);
                                        Operation allergyFor = new Operation();
                                        //手术名称
                                        if (relationLemmaFor.getProperty().equals(EntityEnum.OPERATION.toString())) {
                                            text = text+relationLemmaFor.getText();
                                                allergyFor.setName(text);
                                                operations.add(allergyFor);
                                            for (Lemma relationLemmaFiv : relationLemmaFor.getRelationLemmas()) {
                                                operations.remove(0);
                                                Operation allergyFiv = new Operation();
                                                //手术结果-情况不详
                                                if (relationLemmaFiv.getProperty().equals(EntityEnum.OPERATION_RESULT.toString())||
                                                    relationLemmaFiv.getProperty().equals(EntityEnum.UNKNOWN.toString())) {
                                                    text = text+relationLemmaFiv.getText();
                                                        allergyFiv.setName(text);
                                                        operations.add(allergyFiv);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }else{
                    operationFir.setName(text);
                    operations.add(operationFir);
                }
            }

            //手术名称
            List<Lemma> operationsList = createEntityTree(aiOut, EntityEnum.OPERATION.toString());
                for (Lemma lemma : operationsList) {
                    Operation operationFir = new Operation();
                    String text = lemma.getText();
                    if (lemma.isHaveChildren()) {
                        for (Lemma relationLemmaRec : lemma.getRelationLemmas()) {
                            Operation operationSec = new Operation();
                            //手术史
                            if (relationLemmaRec.getProperty().equals(EntityEnum.TIME.toString())) {
                               String firText ="手术时间："+relationLemmaRec.getText()+"、"+"手术："+text;
                                operationSec.setName(firText);
                                operations.add(operationSec);
                            }
                        }}else{
                        operationFir.setName(text);
                        operations.add(operationFir);
                    }
                }

            //传染病史-正向处理
          //  否定 NEGATIVE 传染病史 INFECTIOUS_KEYWORD
            List<Lemma> diagInfectiouList = createEntityTree(aiOut, EntityEnum.INFECTIOUS_KEYWORD.toString());
            for (Lemma lemma : diagInfectiouList) {
                DiagInfectious diagInfectiousFir = new DiagInfectious();
                String text = lemma.getText();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemmaRec : lemma.getRelationLemmas()) {
                        DiagInfectious diagInfectiousSec = new DiagInfectious();
                        //传染病史
                        if (relationLemmaRec.getProperty().equals(EntityEnum.NEGATIVE.toString())) {
                            String FirText = relationLemmaRec.getText()+text;
                            diagInfectiousSec.setName(FirText);
                            diagInfectiouses.add(diagInfectiousSec);
                        }
                    }
                } else{
                    diagInfectiousFir.setName(text);
                    diagInfectiouses.add(diagInfectiousFir);
                }
            }
            pastLabel.setBloodTransfusions(bloodTransfusions);
            pastLabel.setVaccinates(vaccinates);
            pastLabel.setDiags(diags);
            pastLabel.setOperations(operations);
            pastLabel.setAllergies(allergies);
            pastLabel.setDiagInfectiouses(diagInfectiouses);
            personalLabel.setDevelopment(development);
            beHospitalizedDoc.setPersonalLabel(personalLabel);
            beHospitalizedDoc.setPastLabel(pastLabel);
        }     catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage(), e);
    }
        return beHospitalizedDoc;
    }
}
