package com.lantone.structure.ai.process;

import com.alibaba.fastjson.JSONObject;
import com.lantone.structure.ai.model.EntityEnum;
import com.lantone.structure.model.entity.Lis;
import com.lantone.structure.model.entity.LisValue;
import com.lantone.structure.model.entity.PD;
import org.apache.commons.lang3.StringUtils;

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

public class EntityProcessLis extends EntityProcess {
    public List<Lis> extractEntity(JSONObject outputs) {
        List<Lis> lises = new ArrayList<>();
        Lis lis = null;
        List<Map<String, String>> lisEntityList = processJson(outputs, EntityEnum.LABORATORY.toString());
        for (Map<String, String> lisEntityMap : lisEntityList) {
            if (StringUtils.isEmpty(lisEntityMap.get(EntityEnum.LABORATORY.toString()))) {
                continue;
            }
            lis = new Lis();
            for (String key : lisEntityMap.keySet()) {
                String entity = StringUtils.isEmpty(lisEntityMap.get(key)) ? "" : lisEntityMap.get(key);
                switch (EntityEnum.parseOfValue(key)) {
                    case LABORATORY:
                        lis.setName(entity);
                        break;
                    case LABORATORY_VALUE:
                        LisValue lisValue = new LisValue();
                        lisValue.setName(entity);
                        lis.setLisValue(lisValue);
                        PD pd = new PD();
                        String value = entity;
                        String[] val_unit = new String[2];
                        if (value.trim().length() > 0) {
                            val_unit = extract_digit(value);
                        }
                        pd.setValue(val_unit[0]);
                        pd.setUnit(val_unit[1]);
                        lis.setPd(pd);
                        break;
                }
            }
            lises.add(lis);
        }
        return lises;
    }
}
