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

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

/**
 * 输血记录处理
 */
public class EntityProcessBlood extends EntityProcess {
    private Logger logger = LoggerFactory.getLogger(EntityProcessBlood.class);

    public ClinicalBloodLabel extractEntity(JSONObject aiOut) {
        ClinicalBloodLabel clinicalBloodLabel = new ClinicalBloodLabel();
        try {
//            Blood blood = new Blood();
//            List<Blood> bloodList = new ArrayList<>();
            //输血指征
            List<Lemma> indicationBlood = createEntityTree(aiOut, EntityEnum.INDICA_BLOOD.toString());
            List<Indication> indicationList = new ArrayList<>();
            for (Lemma lemma : indicationBlood) {
                Indication indication = new Indication();
                indication.setName(lemma.getText());
                indicationList.add(indication);
            }
            clinicalBloodLabel.setIndication(indicationList);

            //输血类型
            List<Lemma> typeBlood = createEntityTree(aiOut, EntityEnum.TYPE_BLOOD.toString());
            for (Lemma lemma : typeBlood) {
                Type type = new Type();
                type.setName(lemma.getText());
                List<Amount> amountList = new ArrayList<>();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemma : lemma.getRelationLemmas()) {
                        Amount amount = new Amount();
                        if (relationLemma.getProperty().equals(EntityEnum.AMOUNT_BLOOD.toString())) {
                            amount.setName(relationLemma.getText());
                            MeasurementUnit measurementUnit = new MeasurementUnit();
                            for (Lemma measureLemma : relationLemma.getRelationLemmas()) {
                                if (measureLemma.getProperty().equals(EntityEnum.MEASURE_BLOOD.toString())) {
                                    measurementUnit.setName(measureLemma.getText());
                                }
                            }
                            amount.setMeasurementUnit(measurementUnit);
                            amountList.add(amount);
                        }
                    }
                }
                type.setAmount(amountList);
                clinicalBloodLabel.setType(type);
            }
            //输血原因
            clinicalBloodLabel.setReason(findFirstT(createEntityTree(aiOut, EntityEnum.REASON_BLOOD.toString()),new Reason()));
            //输血开始时间
            clinicalBloodLabel.setStartTime(findFirstT(createEntityTree(aiOut, EntityEnum.START_TIME_BLOOD.toString()),new StartTime()));
            //输血结束时间
            clinicalBloodLabel.setEndTime(findFirstT(createEntityTree(aiOut, EntityEnum.END_TIME_BLOOD.toString()),new EndTime()));

            //输血反应类型
            List<Lemma> responseTypeBlood = createEntityTree(aiOut, EntityEnum.RESPONSE_TYPE_BLOOD.toString());
            List<ResponseType> responseTypeList = new ArrayList<>();
            for (Lemma lemma : responseTypeBlood) {
                ResponseType responseType = new ResponseType();
                responseType.setName(lemma.getText());
                responseType.setNegative(findT(lemma, new Negative(), EntityEnum.NEGATIVE_BLOOD.toString()));
                responseTypeList.add(responseType);
            }
            clinicalBloodLabel.setResponseType(responseTypeList);
            //输血次数
            clinicalBloodLabel.setFrequency(findFirstT(createEntityTree(aiOut, EntityEnum.FREQUENCY_BLOOD.toString()),new Frequency()));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return clinicalBloodLabel;
    }
}
