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.Clinical;
import com.lantone.structure.model.entity.LastMenstrual;
import com.lantone.structure.model.entity.Leukorrhea;
import com.lantone.structure.model.entity.MenarcheAge;
import com.lantone.structure.model.entity.MenopauseAge;
import com.lantone.structure.model.entity.MenopauseTime;
import com.lantone.structure.model.entity.Menses;
import com.lantone.structure.model.entity.MensesDuration;
import com.lantone.structure.model.entity.MenstrualCycle;
import com.lantone.structure.model.label.MenstrualLabel;

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

/**
 * 月经史标签处理
 */
public class EntityProcessMenses extends EntityProcess {

    public MenstrualLabel extractEntity(JSONObject aiOut) {
        MenstrualLabel menstrualLabel = new MenstrualLabel();
        //月经情况
        List<Menses> mensesList = new ArrayList<>();
        //读取月经所有本体及关联信息
        List<Lemma> mensesLemmas = createEntityTree(aiOut, EntityEnum.MENSES.toString());
        List<Clinical> clinicals;
        for (Lemma lemma : mensesLemmas) {
            Menses menses = new Menses();
            clinicals = new ArrayList<>();
            menses.setName(lemma.getText());
            if (lemma.isHaveChildren()) {
                for (Lemma relationLemma : lemma.getRelationLemmas()) {
                    if (relationLemma.getProperty().equals(EntityEnum.CLINICAL_FEATURE.toString())) {
                        addClinicalLemma(clinicals, relationLemma);
                    } else if (relationLemma.getProperty().equals(EntityEnum.AGE.toString())) {
                        findMensesAge(menses, relationLemma);
                    } else if (relationLemma.getProperty().equals(EntityEnum.TIME.toString())) {
                        findMensesTime(menses, relationLemma);
                    }
                }
                menses.setClinicals(clinicals);
            }
            mensesList.add(menses);
        }
        menstrualLabel.setMensesList(mensesList);
        //白带情况
        List<Leukorrhea> leukorrheas = new ArrayList<>();
        //读取月经所有本体及关联信息
        List<Lemma> leukorrheaLemmas = createEntityTree(aiOut, EntityEnum.LEUKORRHEA.toString());
        for (Lemma lemma : leukorrheaLemmas) {
            Leukorrhea leukorrhea = new Leukorrhea();
            clinicals = new ArrayList<>();
            leukorrhea.setName(lemma.getText());
            if (lemma.isHaveChildren()) {
                for (Lemma relationLemma : lemma.getRelationLemmas()) {
                    if (relationLemma.getProperty().equals(EntityEnum.CLINICAL_FEATURE.toString())) {
                        addClinicalLemma(clinicals, relationLemma);
                    }
                }
                leukorrhea.setClinical(clinicals);
            }
            leukorrheas.add(leukorrhea);
        }
        menstrualLabel.setLeukorrheas(leukorrheas);
        return menstrualLabel;
    }

    /**
     * 查找临床表现
     *
     * @param clinicals
     * @param clinicalLemma
     */
    private void addClinicalLemma(List<Clinical> clinicals, Lemma clinicalLemma) {
        Clinical clinical = new Clinical();
        clinical.setName(clinicalLemma.getText());
        if (clinicalLemma.isHaveChildren()) {
            clinical.setTimestamp(findTime(clinicalLemma));
            clinical.setNegative(findNegative(clinicalLemma));
        }
        clinicals.add(clinical);
    }

    /**
     * 查找初潮年龄
     *
     * @param detailLemma
     * @return
     */
    private void findMensesAge(Menses menses, Lemma detailLemma) {
        if (detailLemma.getRelationName().contains("初潮")) {
            MenarcheAge menarcheAge = new MenarcheAge();
            menarcheAge.setName(detailLemma.getText());
            menses.setMenarcheAge(menarcheAge);
        } else if (detailLemma.getRelationName().contains("绝经")) {
            MenopauseAge menopauseAge = new MenopauseAge();
            menopauseAge.setName(detailLemma.getText());
            menses.setMenopauseAge(menopauseAge);
        }
    }

    private void findMensesTime(Menses menses, Lemma detailLemma) {
        if (detailLemma.getRelationName().contains("行经天数")) {
            MensesDuration mensesDuration = new MensesDuration();
            mensesDuration.setName(detailLemma.getText());
            menses.setMensesDuration(mensesDuration);
        } else if (detailLemma.getRelationName().contains("月经周期")) {
            MenstrualCycle menstrualCycle = new MenstrualCycle();
            menstrualCycle.setName(detailLemma.getText());
            menses.setMenstrualCycle(menstrualCycle);
        } else if (detailLemma.getRelationName().contains("末次月经")) {
            LastMenstrual lastMenstrual = new LastMenstrual();
            lastMenstrual.setName(detailLemma.getText());
            menses.setLastMenstrual(lastMenstrual);
        } else if (detailLemma.getRelationName().contains("绝经")) {
            MenopauseTime menopauseTime = new MenopauseTime();
            menopauseTime.setName(detailLemma.getText());
            menses.setMenopauseTime(menopauseTime);
        }
    }
}
