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

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.dict.api.DictApi;
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 cn.stylefeng.team.form.api.parser.base.BaseFormFormat;

public class FormRadioFormat implements BaseFormFormat {

    @Override
    public Object formatProcess(FormItem formItem, Object originValue) {

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

        if (ObjectUtil.isEmpty(originValue)) {
            return null;
        }

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


        if (DictValueEnum.DICT_ID.getValueType().equals(valueType)) {
            return dictApi.getDictNameByDictId(Long.valueOf(originValue.toString()));
        }

        if (DictValueEnum.DICT_CODE.getValueType().equals(valueType)) {
            return dictApi.getDictName(formItemSelect.getDictTypeId(), originValue.toString());
        }

        return null;
    }

}
