package com.seeyon.utils.form;

import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormFieldBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumBean;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import com.seeyon.ctp.util.JDBCAgent;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class EnumMapUtils {

    public static String getEnumItemValueByDisplayValue(FormBean cap4FormBean,String fieldDisplay, String targetValue) {
        if(targetValue == null || "null".equals(targetValue) || "".equals(targetValue)){
            return null;
        }
        try {
            FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
            FormFieldBean beanByDisplay = masterTableBean.getFieldBeanByDisplay(fieldDisplay);
            if(beanByDisplay == null || beanByDisplay.getEnumId() == 0l) {
                return null;
            }
            return getEnumItemValueByEnumId(targetValue, beanByDisplay.getEnumId());
        } catch (Exception e) {
            return null;
        }
    }

    public static String getEnumItemIdByGroupNameAndItemShowValue(String enumGroupName, String targetValue) {
        String sql = "SELECT * FROM ctp_enum ce let join ctp_enum_item cei on ce.`ID` = cei.REF_ENUMID where ce.`ENUMNAME` = ? and CEI.`SHOWVALUE` = ?";
        if(StringUtils.isAnyBlank(enumGroupName, targetValue)){
            return null;
        }
        Long enumId = null;
        JDBCAgent agent = new JDBCAgent();
        try {
            agent.execute(sql, Arrays.asList(enumGroupName,targetValue));
            List<Map<String, Object>> list = (List<Map<String, Object>>) agent.resultSetToList();
            if(list == null || list.size() == 0) {
                return null;
            }
            Map map = list.get(0);
            enumId = (Long)map.get("ID");
            return enumId == null ? "" : enumId + "";
        } catch (Exception e) {

        }finally {
            agent.close();
        }
        return null;
    }


    public static String getEnumItemValueByRootPCode(String rootPCode, String groupValue, String targetValue) {
        if(targetValue == null || "null".equals(targetValue) || "".equals(targetValue)){
            return null;
        }
        String queryIdSql = "SELECT ce.ID FROM ctp_enum ce inner join ctp_enum cei on ce.`PARENT_ID` = cei.ID where CEI.`PROGRAM_CODE` = ? and ce.`ENUMNAME` = ?";
        Long enumId = null;
        JDBCAgent agent = new JDBCAgent();
        try {
            agent.execute(queryIdSql, Arrays.asList(rootPCode,groupValue));
            List<Map<String, Object>> list = (List<Map<String, Object>>) agent.resultSetToList();
            if(list == null || list.size() == 0) {
                return null;
            }
            Map map = list.get(0);
            enumId = (Long)map.get("ID");
        } catch (Exception e) {
            return null;
        }finally {
            agent.close();
        }
        EnumManager enumManagerNew = (EnumManager) AppContext.getBean("enumManagerNew");
        CtpEnumBean ctpEnumBean = enumManagerNew.getEnum(enumId);
        if(ctpEnumBean == null) {
            return null;
        }
        List<CtpEnumItem> ctpEnumItems =  ctpEnumBean.getItems();
        if(ctpEnumBean.getItems() == null) {
            return null;
        }
        for (CtpEnumItem enumItem : ctpEnumItems) {
            if(enumItem.getShowvalue().equals(targetValue)) {
                return enumItem.getId() + "";
            }
        }
        return null;
    }

    public static Set<String> getEnumItemValues(FormBean cap4FormBean, String fieldDisplay) {
        Set<String> set = new HashSet<>();
        try {
            FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
            FormFieldBean beanByDisplay = masterTableBean.getFieldBeanByDisplay(fieldDisplay);
            if(beanByDisplay == null || beanByDisplay.getEnumId() == 0l) {
                return set;
            }
            EnumManager enumManagerNew = (EnumManager) AppContext.getBean("enumManagerNew");
            CtpEnumBean ctpEnumBean = enumManagerNew.getEnum(beanByDisplay.getEnumId());

            if(ctpEnumBean == null) {
                return set;
            }
            List<CtpEnumItem> ctpEnumItems =  ctpEnumBean.getItems();
            if(ctpEnumBean.getItems() == null) {
                return set;
            }
            for (CtpEnumItem enumItem : ctpEnumItems) {
                set.add(enumItem.getShowvalue());
            }
        } catch (Exception e) {

        }
        return set;
    }

    public static String getEnumItemValueByEnumId(String showValue,long enumId) {
        EnumManager enumManagerNew = (EnumManager) AppContext.getBean("enumManagerNew");
        CtpEnumBean ctpEnumBean = enumManagerNew.getEnum(enumId);
        if(ctpEnumBean == null) {
            return null;
        }
        List<CtpEnumItem> ctpEnumItems =  ctpEnumBean.getItems();
        if(ctpEnumBean.getItems() == null) {
            return null;
        }
        for (CtpEnumItem enumItem : ctpEnumItems) {
            if(enumItem.getShowvalue().equals(showValue)) {
                return enumItem.getId() + "";
            }
        }
        return null;
    }

    public static String getEnumShowValue(Object enumItemId) throws BusinessException {
        if(enumItemId == null) {
            return null;
        }
        Long temp = enumItemId instanceof Long ? (Long)enumItemId : (enumItemId instanceof String ? Long.parseLong((String)enumItemId) : null);
        if(temp == null) {
            return null;
        }
        EnumManager enumManagerNew = (EnumManager) AppContext.getBean("enumManagerNew");
        CtpEnumItem ctpEnumItem = enumManagerNew.getCtpEnumItem(temp);
        if(ctpEnumItem == null) {
            return null;
        }
        return ctpEnumItem.getShowvalue();
    }
}
