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 com.lantone.qc.pub.model.label.MaritalLabel;
import com.lantone.qc.pub.model.label.PersonalLabel;

import java.util.List;

import static com.lantone.qc.pub.Content.family;

/**
 * @ClassName : EntityProcessPersonal
 * @Description : 个人史
 * @Author : 楼辉荣
 * @Date: 2020-03-10 10:20
 */
public class EntityProcessPersonal extends EntityProcess {
    public PersonalLabel extractEntity(JSONObject aiOut) {
        PersonalLabel personalLabel = new PersonalLabel();
        //地点
        List<Lemma> addressLemmas = createEntityTree(aiOut, EntityEnum.LOCATION.toString());
        for (Lemma lemma :addressLemmas) {
            Address address = new Address();
            address.setName(lemma.getText());
            personalLabel.addAddress(address);
        }
        //职业
        List<Lemma> occupationLemmas = createEntityTree(aiOut, EntityEnum.OCCUPATION.toString());
        for (Lemma lemma :occupationLemmas) {
            Occupation occupation = new Occupation();
            occupation.setName(lemma.getText());
            personalLabel.setOccupation(occupation);
        }
        //疫区史
        List<Lemma> fertilityLemmas = createEntityTree(aiOut, EntityEnum.EPIDEMIC_AREA_HISTORY.toString());
        for (Lemma lemma :fertilityLemmas) {
            EpidemicArea epidemicArea = new EpidemicArea();
            epidemicArea.setName(lemma.getText());
            personalLabel.setEpidemicArea(epidemicArea);
        }
        //接触史
        List<Lemma> contactLemmas = createEntityTree(aiOut, EntityEnum.CONTACT_HISTORY.toString());
        for (Lemma lemma :contactLemmas) {
            Contact contact = new Contact();
            contact.setName(lemma.getText());
            personalLabel.setContact(contact);
        }
        //特殊嗜好
        List<Lemma> specialHobbyLemmas = createEntityTree(aiOut, EntityEnum.SPECIAL_HOBBY.toString());
        for (Lemma lemma :specialHobbyLemmas) {
            SpecialHobby specialHobby = new SpecialHobby();
            specialHobby.setName(lemma.getText());
            //阴性
            specialHobby.setNegative(findNegative(lemma));
            personalLabel.setSpecialHobby(specialHobby);
        }
        //吸烟史
        List<Lemma> smokingLemmas = createEntityTree(aiOut, EntityEnum.SMOKING_HISTORY.toString());
        for (Lemma lemma :smokingLemmas) {
            Smoking smoking = new Smoking();
            smoking.setName(lemma.getText());
            //阴性
            smoking.setNegative(findNegative(lemma));
            if (lemma.isHaveChildren()) {
                for (Lemma relationLemma : lemma.getRelationLemmas()) {
                    if (relationLemma.getProperty().equals(EntityEnum.TIME.toString())) {
                        smoking.setPd(addPD(relationLemma));
                    } else if (relationLemma.getProperty().equals(EntityEnum.USAGE.toString())) {
                        smoking.setUsage(addUsage(relationLemma));
                    }
                }
            }
            personalLabel.setSmoking(smoking);
        }

        //饮酒史
        List<Lemma> drinkingLemmas = createEntityTree(aiOut, EntityEnum.HISTORY_OF_ALCOHOL_INTAKE.toString());
        for (Lemma lemma :drinkingLemmas) {
            Drinking drinking = new Drinking();
            drinking.setName(lemma.getText());
            //阴性
            drinking.setNegative(findNegative(lemma));
            if (lemma.isHaveChildren()) {
                for (Lemma relationLemma : lemma.getRelationLemmas()) {
                    if (relationLemma.getProperty().equals(EntityEnum.TIME.toString())) {
                        drinking.setPd(addPD(relationLemma));
                    } else if (relationLemma.getProperty().equals(EntityEnum.USAGE.toString())) {
                        drinking.setUsage(addUsage(relationLemma));
                    }
                }
            }
            personalLabel.setDrinking(drinking);
        }

        //用量 用于判断吸烟 和 饮酒符合规范
        List<Lemma> usageLemmas = createEntityTree(aiOut, EntityEnum.USAGE.toString());
        for (Lemma lemma :usageLemmas) {
            Usage usage = new Usage();
            usage.setName(lemma.getText());
            personalLabel.setUsage(usage);
        }

        //冶游史
        List<Lemma> maritalHistoryLemmas = createEntityTree(aiOut, EntityEnum.MARITAL_HISTORY.toString());
        for (Lemma lemma :maritalHistoryLemmas) {
            MaritalHistory maritalHistory = new MaritalHistory();
            maritalHistory.setName(lemma.getText());
            if (lemma.isHaveChildren()) {
                maritalHistory.setNegative(findNegative(lemma));
            }
            personalLabel.setMaritalHistory(maritalHistory);
        }
        return personalLabel;
    }

    /**
     * 时间
     * @param timeLemma
     * @return
     */
    private PD addPD(Lemma timeLemma) {
        PD pd = new PD();
        pd.setName(timeLemma.getText());
        pd.setValue(timeLemma.getText());
        return pd;
    }

    /**
     * 用量
     * @param usageLemma
     * @return
     */
    private Usage addUsage(Lemma usageLemma) {
        Usage usage = new Usage();
        usage.setName(usageLemma.getText());
        return usage;
    }
}