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

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

/**
 * 剖宫产手术处理
 */
public class EntityProcessCaesareanSection extends EntityProcess {
    private Logger logger = LoggerFactory.getLogger(EntityProcessCaesareanSection.class);

    public CaesareanSectionLabel extractEntity(JSONObject aiOut) {
        CaesareanSectionLabel caesareanSectionLabel = new CaesareanSectionLabel();
        try {
            //诊断名称
            List<Lemma> diagnosisLemma = createEntityTree(aiOut, EntityEnum.OPE_DIAGNOSIS_NAME.toString());
            List<Diagnosis> diagnosisList = new ArrayList<>();
            for (Lemma lemma : diagnosisLemma) {
                Diagnosis diagnosis = new Diagnosis();
                diagnosis.setName(lemma.getText());
                diagnosisList.add(diagnosis);
            }
            caesareanSectionLabel.setDiagnosis(diagnosisList);

            //麻醉方法
            caesareanSectionLabel.setAnesthesiaMethod(findFirstT(createEntityTree(aiOut, EntityEnum.ANESTHESIA_METHOD.toString()),new AnesthesiaMethod()));

            //麻醉效果
            caesareanSectionLabel.setAnestheticEffect(findFirstT(createEntityTree(aiOut, EntityEnum.ANESTHETIC_EFFECT.toString()),new AnestheticEffect()));

            //子宫情况
            List<Lemma> uterusConditionLemma = createEntityTree(aiOut, EntityEnum.UTERUS_CONDITION.toString());
            List<UterusCondition> uterusConditionList = new ArrayList<>();
            for (Lemma lemma : uterusConditionLemma) {
                UterusCondition uterusCondition = new UterusCondition();
                uterusCondition.setName(lemma.getText());
                uterusConditionList.add(uterusCondition);
            }
            caesareanSectionLabel.setUterusConditions(uterusConditionList);

            //胎儿娩出方式
            caesareanSectionLabel.setChildbirthWay(findFirstT(createEntityTree(aiOut, EntityEnum.CHILDBIRTH_WAY.toString()),new ChildbirthWay()));

            //羊水
            List<Lemma> amnioticFluidLemma = createEntityTree(aiOut, EntityEnum.AMNIOTIC_FLUID.toString());
            AmnioticFluid amnioticFluid = new AmnioticFluid();
            for (Lemma lemma : amnioticFluidLemma) {
                amnioticFluid.setName(lemma.getText());
                amnioticFluid.setAmnioticFluidCharacter(findT(lemma, new AmnioticFluidCharacter(), EntityEnum.AMNIOTIC_FLUID_CHARACTER.toString()));
                amnioticFluid.setQuantity(findT(lemma, new Quantity(), EntityEnum.OPE_QUANTITY.toString()));
            }
            caesareanSectionLabel.setAmnioticFluid(amnioticFluid);

            //胎盘娩出情况
            List<Lemma> childbirthConditionLemma = createEntityTree(aiOut, EntityEnum.CHILDBIRTH_CONDITION.toString());
            List<ChildbirthCondition> ChildbirthConditionList = new ArrayList<>();
            for (Lemma lemma : childbirthConditionLemma) {
                ChildbirthCondition childbirthCondition = new ChildbirthCondition();
                childbirthCondition.setName(lemma.getText());
                ChildbirthConditionList.add(childbirthCondition);
            }
            caesareanSectionLabel.setChildbirthConditions(ChildbirthConditionList);

            //胎盘
            List<Lemma> PlacentaLemma = createEntityTree(aiOut, EntityEnum.PLACENTA.toString());
            Placenta placenta = new Placenta();
            for (Lemma lemma : PlacentaLemma) {
                placenta.setName(lemma.getText());
                placenta.setVolume(findT(lemma, new Volume(), EntityEnum.VOLUME.toString()));
                placenta.setWeight(findT(lemma, new Weight(), EntityEnum.WEIGHT.toString()));
            }
            caesareanSectionLabel.setPlacenta(placenta);

            //脐带
            List<Lemma> umbilicalCordLemma = createEntityTree(aiOut, EntityEnum.UMBILICALCORD.toString());
            UmbilicalCord umbilicalCord = new UmbilicalCord();
            for (Lemma lemma : umbilicalCordLemma) {
                umbilicalCord.setName(lemma.getText());
                umbilicalCord.setExtent(findT(lemma, new Extent(), EntityEnum.EXTENT.toString()));
            }
            caesareanSectionLabel.setUmbilicalCord(umbilicalCord);

            //脐带绕颈
            List<Lemma> CordAroundNeckLemma = createEntityTree(aiOut, EntityEnum.CORD_AROUND_NECK.toString());
            CordAroundNeck cordAroundNeck = new CordAroundNeck();
            for (Lemma lemma : CordAroundNeckLemma) {
                cordAroundNeck.setName(lemma.getText());
                cordAroundNeck.setNegative(findT(lemma, new Negative(), EntityEnum.NEGATIVE.toString()));
                cordAroundNeck.setCylinderNumber(findT(lemma, new CylinderNumber(), EntityEnum.CYLINDER_NUMBER.toString()));
            }
            caesareanSectionLabel.setCordAroundNeck(cordAroundNeck);

            //脐带绕身
            List<Lemma> cordAroundBodyLemma = createEntityTree(aiOut, EntityEnum.CORD_AROUND_BODY.toString());
            CordAroundBody cordAroundBody = new CordAroundBody();
            for (Lemma lemma : cordAroundBodyLemma) {
                cordAroundBody.setName(lemma.getText());
                cordAroundBody.setNegative(findT(lemma, new Negative(), EntityEnum.NEGATIVE.toString()));
                cordAroundBody.setCylinderNumber(findT(lemma, new CylinderNumber(), EntityEnum.CYLINDER_NUMBER.toString()));
            }
            caesareanSectionLabel.setCordAroundBody(cordAroundBody);

            //子宫壁缝合情况
            List<Lemma> uterineWallSutureLemma = createEntityTree(aiOut, EntityEnum.UTERINE_WALL_SUTURE.toString());
            List<UterineWallSuture> uterineWallSutureList = new ArrayList<>();
            for (Lemma lemma : uterineWallSutureLemma) {
                UterineWallSuture uterineWallSuture = new UterineWallSuture();
                uterineWallSuture.setName(lemma.getText());
                uterineWallSutureList.add(uterineWallSuture);
            }
            caesareanSectionLabel.setUterineWallSutures(uterineWallSutureList);

            //宫缩剂名称
            List<Lemma> uCAgentLemma = createEntityTree(aiOut, EntityEnum.UC_AGENT.toString());
            List<UCAgent> uCAgentList = new ArrayList<>();
            for (Lemma lemma : uCAgentLemma) {
                UCAgent uCAgent = new UCAgent();
                uCAgent.setName(lemma.getText());
                uCAgent.setConsumption(findT(lemma, new Consumption(), EntityEnum.UC_CONSUMPTION.toString()));
                uCAgent.setUsageWardRound(findT(lemma, new UsageWardRound(), EntityEnum.UC_USAGE_WARD_ROUND.toString()));
                uCAgentList.add(uCAgent);
            }
            caesareanSectionLabel.setUCAgents(uCAgentList);

            //宫腔探查异常情况描述
            List<Lemma> uterineCavityAbnormityLemma = createEntityTree(aiOut, EntityEnum.UTERINE_CAVITY_ABNORMITY.toString());
            List<UterineCavityAbnormity> uterineCavityAbnormityList = new ArrayList<>();
            for (Lemma lemma : uterineCavityAbnormityLemma) {
                UterineCavityAbnormity uterineCavityAbnormity = new UterineCavityAbnormity();
                uterineCavityAbnormity.setName(lemma.getText());
                uterineCavityAbnormityList.add(uterineCavityAbnormity);
            }
            caesareanSectionLabel.setUterineCavityAbnormity(uterineCavityAbnormityList);

            //宫腔探查处理情况
            List<Lemma> uterineCavityDisposeLemma = createEntityTree(aiOut, EntityEnum.UTERINE_CAVITY_DISPOSE.toString());
            List<UterineCavityDispose> uterineCavityDisposeList = new ArrayList<>();
            for (Lemma lemma : uterineCavityDisposeLemma) {
                UterineCavityDispose uterineCavityDispose = new UterineCavityDispose();
                uterineCavityDispose.setName(lemma.getText());
                uterineCavityDisposeList.add(uterineCavityDispose);
            }
            caesareanSectionLabel.setUterineCavityDispose(uterineCavityDisposeList);

            //手术时产妇情况
            List<Lemma> puerperaConditionLemma = createEntityTree(aiOut, EntityEnum.PUERPERA_CONDITION.toString());
            List<PuerperaCondition> puerperaConditionList = new ArrayList<>();
            for (Lemma lemma : puerperaConditionLemma) {
                PuerperaCondition puerperaCondition = new PuerperaCondition();
                puerperaCondition.setName(lemma.getText());
                puerperaConditionList.add(puerperaCondition);
            }
            caesareanSectionLabel.setPuerperaCondition(puerperaConditionList);

            //术中出血
            List<Lemma> hemorrhageLemma = createEntityTree(aiOut, EntityEnum.HEMORRHAGE.toString());
            Hemorrhage hemorrhage = new Hemorrhage();
            for (Lemma lemma : hemorrhageLemma) {
                hemorrhage.setName(lemma.getText());
                hemorrhage.setQuantity(findT(lemma, new Quantity(), EntityEnum.OPE_QUANTITY.toString()));
            }
            caesareanSectionLabel.setHemorrhage(hemorrhage);

            //补液
            List<Lemma> fluidInfusionLemma = createEntityTree(aiOut, EntityEnum.FLUID_INFUSION.toString());
            FluidInfusion fluidInfusion = new FluidInfusion();
            for (Lemma lemma : fluidInfusionLemma) {
                fluidInfusion.setName(lemma.getText());
                fluidInfusion.setQuantity(findT(lemma, new Quantity(), EntityEnum.OPE_QUANTITY.toString()));
            }
            caesareanSectionLabel.setFluidInfusion(fluidInfusion);

            //血压情况
            caesareanSectionLabel.setBloodPressure(findFirstT(createEntityTree(aiOut, EntityEnum.BLOOD_PRESSURE.toString()),new BloodPressure()));

            //新生儿
            List<Lemma> newbornLemma = createEntityTree(aiOut, EntityEnum.NEWBORN.toString());
            Newborn newborn = new Newborn();
            for (Lemma lemma : newbornLemma) {
                newborn.setName(lemma.getText());
                newborn.setWeight(findT(lemma, new Weight(), EntityEnum.WEIGHT.toString()));
                newborn.setExtent(findT(lemma, new Extent(), EntityEnum.EXTENT.toString()));
            }
            caesareanSectionLabel.setNewborn(newborn);

            //Apgar评分
            List<Lemma> apgarScoreLemma = createEntityTree(aiOut, EntityEnum.APGAR_SCORE.toString());
            List<ApgarScore> apgarScoreList = new ArrayList<>();
            for (Lemma lemma : apgarScoreLemma) {
                ApgarScore apgarScore = new ApgarScore();
                apgarScore.setName(lemma.getText());
                apgarScore.setScore(findT(lemma, new Score(), EntityEnum.SCORE.toString()));
                apgarScoreList.add(apgarScore);
            }
            caesareanSectionLabel.setApgarScores(apgarScoreList);

            //产瘤
            List<Lemma> caputSuccedaneumLemma = createEntityTree(aiOut, EntityEnum.CAPUT_SUCCEDANEUM.toString());
            CaputSuccedaneum caputSuccedaneum = new CaputSuccedaneum();
            for (Lemma lemma : caputSuccedaneumLemma) {
                caputSuccedaneum.setName(lemma.getText());
                caputSuccedaneum.setVolume(findT(lemma, new Volume(), EntityEnum.VOLUME.toString()));
                caputSuccedaneum.setCaputSuccedaneumPart(findT(lemma, new CaputSuccedaneumPart(), EntityEnum.CAPUT_SUCCEDANEUM_PART.toString()));
            }
            caesareanSectionLabel.setCaputSuccedaneum(caputSuccedaneum);

            //手术用药名称
            List<Lemma> drugLemma = createEntityTree(aiOut, EntityEnum.OPE_DRUG.toString());
            List<Drug> drugList = new ArrayList<>();
            for (Lemma lemma : drugLemma) {
                Drug drug = new Drug();
                drug.setName(lemma.getText());
                drug.setConsumption(findT(lemma, new Consumption(), EntityEnum.OPE_CONSUMPTION.toString())); //(手术用药量)
                drug.setUsageWardRound(findT(lemma, new UsageWardRound(), EntityEnum.OPE_USAGE_WARD_ROUND.toString())); //(手术用药方法)
                drugList.add(drug);
            }
            caesareanSectionLabel.setDrugs(drugList);

            //输血成分
            List<Lemma> transfusionLemma = createEntityTree(aiOut, EntityEnum.TRANSFUSION.toString());
            Transfusion transfusion = new Transfusion();
            for (Lemma lemma : transfusionLemma) {
                transfusion.setName(lemma.getText());
                transfusion.setQuantity(findT(lemma, new Quantity(), EntityEnum.OPE_QUANTITY.toString()));
            }
            caesareanSectionLabel.setTransfusion(transfusion);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return caesareanSectionLabel;
    }
}
