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

import cn.stylefeng.team.form.api.FormItemFormatApi;
import cn.stylefeng.team.form.api.entity.FormItem;
import cn.stylefeng.team.form.api.parser.*;
import cn.stylefeng.team.form.api.parser.base.BaseFormFormat;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;

@Service
public class FormItemFormatService implements FormItemFormatApi {

    public static final LinkedHashMap<String, BaseFormFormat> FORM_FORMAT_MAP = new LinkedHashMap<>();

    static {
        FORM_FORMAT_MAP.put("checkbox", new FormCheckBoxFormat());
        FORM_FORMAT_MAP.put("city", new FormCityFormatFormat());
        FORM_FORMAT_MAP.put("date-range", new FormDateRangeFormat());
        FORM_FORMAT_MAP.put("org-user", new FormOrgUserFormat());
        FORM_FORMAT_MAP.put("radio", new FormRadioFormat());
        FORM_FORMAT_MAP.put("select-dict", new FormSelectDictFormat());
        FORM_FORMAT_MAP.put("select", new FormSelectFormat());
        FORM_FORMAT_MAP.put("select-modal", new FormSelectModalFormat());
        FORM_FORMAT_MAP.put("select-org", new FormSelectOrgFormat());
        FORM_FORMAT_MAP.put("select-position", new FormSelectPositionFormat());
        FORM_FORMAT_MAP.put("select-role", new FormSelectRoleFormat());
        FORM_FORMAT_MAP.put("select-user", new FormSelectUserFormat());
        FORM_FORMAT_MAP.put("switch", new FormSwitchFormat());
        FORM_FORMAT_MAP.put("upload", new FormUploadFormat());
        FORM_FORMAT_MAP.put("upload-pic", new FormUploadPicFormat());
        FORM_FORMAT_MAP.put("view-select", new FormViewSelectFormat());
    }

    @Override
    public Object format(String componentCode, FormItem formItem, Object originValue) {
        BaseFormFormat baseFormFormat = FORM_FORMAT_MAP.get(componentCode);
        if (baseFormFormat == null) {
            return originValue;
        }
        return FORM_FORMAT_MAP.get(componentCode).formatProcess(formItem, originValue);
    }

}
