package com.lantone.qc.kernel.structure.ai.process;

import com.alibaba.fastjson.JSONObject;
import com.lantone.qc.kernel.structure.ai.model.EntityEnum;
import com.lantone.qc.kernel.structure.ai.model.Lemma;
import com.lantone.qc.pub.model.entity.*;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

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

@Component
public class EntityProcessDrug extends EntityProcess {
    private Logger logger = LoggerFactory.getLogger(EntityProcessDrug.class);

    public List<Drug> extractEntity(JSONObject aiOut) {
        String content = aiOut.getString("content");
        //药物
        List<Drug> drugs = new ArrayList<>();
        try {
            List<Lemma> drugLemmas = createEntityTree(aiOut, EntityEnum.DRUG.toString());
            List<Lemma> consumptionLemmas = createEntityTree(aiOut, EntityEnum.CONSUMPTION.toString().split("-")[0]);
            List<Lemma> usageWardRoundLemmas = createEntityTree(aiOut, EntityEnum.USAGE_WARD_ROUND.toString());
            List<Lemma> frequencyLemmas = createEntityTree(aiOut, EntityEnum.FREQUENCY.toString());
            List<Lemma> stopLemmas = createEntityTree(aiOut, EntityEnum.STOP.toString());
            List<Lemma> reasonsForAntibioticLemmas = createEntityTree(aiOut, EntityEnum.REASONS_FOR_ANTIBIOTIC.toString());
            for (Lemma lemma : drugLemmas) {
                int lemmaPosition = Integer.parseInt(lemma.getPosition());
                if (content.substring(Math.max(0, lemmaPosition - 10), lemmaPosition).contains("暂停")) {
                    continue;
                }
                Drug drug = new Drug();
                drug.setName(lemma.getText());
                drug.setConsumption(findTAfter(lemma, new Consumption(), EntityEnum.CONSUMPTION.toString().split("-")[0]));
                drug.setUsageWardRound(findTAfter(lemma, new UsageWardRound(), EntityEnum.USAGE_WARD_ROUND.toString()));
                drug.setFrequency(findTAfter(lemma, new Frequency(), EntityEnum.FREQUENCY.toString()));
                drug.setStop(findTAfter(lemma, new Stop(), EntityEnum.STOP.toString()));
                drug.setReasonsForAntibiotic(findTAfter(lemma, new ReasonsForAntibiotic(), EntityEnum.REASONS_FOR_ANTIBIOTIC.toString()));
                //用量
                if (drug.getConsumption() == null) {
                    drug.setConsumption(setDrugs(consumptionLemmas, lemma, new Consumption()));
                }
                //用法
                if (drug.getUsageWardRound() == null) {
                    drug.setUsageWardRound(setDrugs(usageWardRoundLemmas, lemma, new UsageWardRound()));
                }
                //频率
                if (drug.getFrequency() == null) {
                    drug.setFrequency(setDrugs(frequencyLemmas, lemma, new Frequency()));
                }
                //停用
                if (drug.getStop() == null) {
                    drug.setStop(setDrugs(stopLemmas, lemma, new Stop()));
                }
                //抗生素使用原因
                if (drug.getReasonsForAntibiotic() == null) {
                    drug.setReasonsForAntibiotic(setDrugs(reasonsForAntibioticLemmas, lemma, new ReasonsForAntibiotic()));
                }
                drugs.add(drug);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return drugs;
    }

    /**
     * 模型抓取抗生素信息为空的时候判断是否在20个字节之内
     *
     * @param Lemmas
     * @param lemma
     * @param t
     * @return
     * @throws Exception
     */
    private <T> T setDrugs(List<Lemma> Lemmas, Lemma lemma, T t) throws Exception {
        for (Lemma lem : Lemmas) {
            if (Integer.parseInt(lem.getPosition()) - Integer.parseInt(lemma.getPosition()) <= 20) {
                BeanUtils.copyProperty(t, "name", lemma.getText());
                return t;
            }
        }
        return null;
    }
}
