package cn.stylefeng.team.form.api.parser.common;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.dict.api.DictApi;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.sys.api.OrganizationServiceApi;
import cn.stylefeng.roses.kernel.sys.api.PositionServiceApi;
import cn.stylefeng.roses.kernel.sys.api.SysRoleServiceApi;
import cn.stylefeng.roses.kernel.sys.api.SysUserServiceApi;
import cn.stylefeng.team.form.api.entity.FormItem;
import cn.stylefeng.team.form.api.entity.FormItemSelect;
import cn.stylefeng.team.form.api.enums.DictValueEnum;
import com.alibaba.fastjson.JSON;

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

public class FormFormatUtil {

    public static String getSimpleDictListStringReadable(String originValue) {

        List<SimpleDict> simpleDictList = JSON.parseArray(originValue, SimpleDict.class);

        StringBuilder result = new StringBuilder();
        for (SimpleDict simpleDict : simpleDictList) {
            result.append(simpleDict.getName()).append(",");
        }

        return StrUtil.removeSuffix(result.toString(), ",");
    }

    public static String getDictName(FormItem formItem, String originValue) {

        DictApi dictApi = SpringUtil.getBean(DictApi.class);

        if (formItem == null) {
            return originValue;
        }

        FormItemSelect formItemSelect = formItem.getFormItemSelect();
        if (formItemSelect == null) {
            return originValue;
        }

        Integer valueType = formItemSelect.getDictValueType();
        if (valueType == null) {
            return originValue;
        }

        Long dictTypeId = formItemSelect.getDictTypeId();
        if (dictTypeId == null) {
            return originValue;
        }

        if (DictValueEnum.DICT_ID.getValueType().equals(valueType)) {
            return dictApi.getDictNameByDictId(Long.valueOf(originValue));
        } else {
            return dictApi.getDictName(dictTypeId, originValue);
        }
    }

    public static String getDictNameListString(FormItem formItem, Object originValue) {

        DictApi dictApi = SpringUtil.getBean(DictApi.class);

        if (formItem == null) {
            return null;
        }

        FormItemSelect formItemSelect = formItem.getFormItemSelect();
        if (formItemSelect == null) {
            return null;
        }

        Integer valueType = formItemSelect.getDictValueType();
        if (valueType == null) {
            return null;
        }

        Long dictTypeId = formItemSelect.getDictTypeId();
        if (dictTypeId == null) {
            return null;
        }

        List<String> dictValueList = JSON.parseArray(originValue.toString(), String.class);

        ArrayList<String> finalDictNameList = new ArrayList<>();
        for (String dictValue : dictValueList) {
            if (DictValueEnum.DICT_ID.getValueType().equals(valueType)) {
                String dictName = dictApi.getDictNameByDictId(Long.valueOf(dictValue));
                finalDictNameList.add(dictName);
            } else {
                String dictName = dictApi.getDictName(formItem.getFormItemSelect().getDictTypeId(), dictValue);
                finalDictNameList.add(dictName);
            }
        }

        return StrUtil.join(",", finalDictNameList);
    }

    public static String getSingleOrgName(Long orgId) {
        OrganizationServiceApi organizationServiceApi = SpringUtil.getBean(OrganizationServiceApi.class);
        return organizationServiceApi.getOrgNameById(orgId);
    }

    public static String getSinglePositionName(Long orgId) {
        PositionServiceApi positionServiceApi = SpringUtil.getBean(PositionServiceApi.class);
        return positionServiceApi.getPositionName(orgId);
    }

    public static Object getSingleRoleName(Long roleId) {
        SysRoleServiceApi roleServiceApi = SpringUtil.getBean(SysRoleServiceApi.class);
        return roleServiceApi.getRoleNameByRoleId(roleId);
    }

    public static Object getSingleUserName(Long userId) {
        SysUserServiceApi sysUserServiceApi = SpringUtil.getBean(SysUserServiceApi.class);
        return sysUserServiceApi.getUserRealName(userId);
    }

}
