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.FamilyLabel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * 家族史标签处理
 */
public class EntityProcessFamily extends EntityProcess {
    private Logger logger = LoggerFactory.getLogger(EntityProcessFamily.class);

    public FamilyLabel extractEntity(JSONObject aiOut) {
        FamilyLabel familyLabel = new FamilyLabel();
        List<Family> families = loadFamilies(aiOut);
        familyLabel.setFamilies(families);
        return familyLabel;
    }

    public List<Family> loadFamilies(JSONObject aiOut) {
        //家属情况
        List<Family> families = new ArrayList<>();
        try {
            //读取家属所有本体及关联信息
            List<Lemma> familyLemmas = createEntityTree(aiOut, EntityEnum.RELATIVES.toString());
            for (Lemma lemma : familyLemmas) {
                Family family = new Family();
                family.setName(lemma.getText());
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemma : lemma.getRelationLemmas()) {
                        if (relationLemma.getProperty().equals(EntityEnum.DEAD.toString())) {
                            if (family.getDead() == null) {
                                family.setDead(addDeadLemma(relationLemma));
                            }
                        } else if (relationLemma.getProperty().equals(EntityEnum.INFECTIOUS_KEYWORD.toString())) {
                            family.setDiagInfectious(addInfectious(relationLemma));
                        } else if (relationLemma.getProperty().equals(EntityEnum.GENETIC_DISEASE_KEYWORD.toString())) {
                            family.setGeneticDiseaseKeyword(addGeneticDisease(relationLemma));
                        } else if (relationLemma.getProperty().equals(EntityEnum.HEALTH.toString())) {
                            family.setHealthCondition(addHealthCondition(relationLemma));
                        } else if (relationLemma.getProperty().equals(EntityEnum.DIEASE.toString())) {
                            family.addDiag(addDiags(relationLemma));
                        } else if (relationLemma.getProperty().equals(EntityEnum.TUMOUR.toString())) {
                            family.setTumour(findT(lemma, new Tumour(), EntityEnum.TUMOUR.toString()));//肿瘤病史
                        }
                    }
                }
                families.add(family);
            }
            List<Lemma> similarDiseaseLemmas = createEntityTree(aiOut, EntityEnum.SIMILAR_DISEASE_KEYWORD.toString());
            for (Lemma relationLemma : similarDiseaseLemmas) {
                Family family = new Family();
                family.setSimilarDiag(addSimilarDiag(relationLemma));
                families.add(family);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return families;
    }

    /**
     * 家属死亡
     *
     * @param deadLemma
     * @return
     */
    private Dead addDeadLemma(Lemma deadLemma) {
        Dead dead = new Dead();
        dead.setName(deadLemma.getText());
        if (deadLemma.isHaveChildren()) {
            for (Lemma lemma : deadLemma.getRelationLemmas()) {
                if (lemma.getProperty().equals(EntityEnum.DEAD_REASON.toString())) {
                    DeadReason deadReason = new DeadReason();
                    deadReason.setName(lemma.getText());
                    dead.setDeadReason(deadReason);
                } else if (lemma.getProperty().equals(EntityEnum.UNKNOWN.toString())) {
                    Unknow unknow = new Unknow();
                    unknow.setName(lemma.getText());
                    dead.setUnknow(unknow);
                } else if (lemma.getProperty().equals(EntityEnum.AGE.toString())) {
                    Age age = new Age();
                    age.setName(lemma.getText());
                    dead.setAge(age);
                } else if (lemma.getProperty().equals(EntityEnum.NEGATIVE.toString())) {
                    Negative negative = new Negative();
                    negative.setName(lemma.getText());
                    dead.setNegative(negative);
                }
            }
        }
        return dead;
    }

    /**
     * 家属传染病史
     *
     * @param infectiouLemma
     * @return
     */
    private DiagInfectious addInfectious(Lemma infectiouLemma) {
        DiagInfectious diagInfectious = new DiagInfectious();
        diagInfectious.setName(infectiouLemma.getText());
        if (infectiouLemma.isHaveChildren()) {
            diagInfectious.setNegative(findNegative(infectiouLemma));
        }
        return diagInfectious;
    }

    /**
     * 家属遗传病史
     *
     * @param geneticLemma
     * @return
     */
    private GeneticDiseaseKeyword addGeneticDisease(Lemma geneticLemma) {
        GeneticDiseaseKeyword geneticDiseaseKeyword = new GeneticDiseaseKeyword();
        geneticDiseaseKeyword.setName(geneticLemma.getText());
        if (geneticLemma.isHaveChildren()) {
            geneticDiseaseKeyword.setNegative(findNegative(geneticLemma));
        }
        return geneticDiseaseKeyword;
    }

    /**
     * 家属健康情况
     *
     * @param healthConditionLemma
     * @return
     */
    private HealthCondition addHealthCondition(Lemma healthConditionLemma) {
        HealthCondition healthCondition = new HealthCondition();
        healthCondition.setName(healthConditionLemma.getText());
        return healthCondition;
    }

    /**
     * 家属疾病史
     *
     * @param diagLemma
     * @return
     */
    private Diag addDiags(Lemma diagLemma) {
        Diag diag = DiagEnhancer.create(diagLemma.getText());
        if (diagLemma.isHaveChildren()) {
            diag.setNegative(findNegative(diagLemma));
        }
        return diag;
    }

    /**
     * 相似疾病
     *
     * @param diagLemma
     * @return
     */
    private SimilarDiag addSimilarDiag(Lemma diagLemma) {
        SimilarDiag similarDiag = new SimilarDiag();
        similarDiag.setName(diagLemma.getText());
        if (diagLemma.isHaveChildren()) {
            similarDiag.setNegative(findNegative(diagLemma));
        }
        return similarDiag;
    }
}
