package cc.smtweb.framework.core.mvc.controller.binder;

import cc.smtweb.framework.core.mvc.controller.IEditor;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

public class ParamEditor {
    public static final int TYPE_UNKNOWN = 0;
    public static final int TYPE_FIELD = 1;
    public static final int TYPE_PARAM = 2;
    public static final int TYPE_BODY = 3;
    public static final int TYPE_PATH = 4;
    public static final int TYPE_HEADER = 5;
    public static final int TYPE_COOKIE = 6;
    public static final int TYPE_ATTR = 7;

    private final Map<Class<?>, IEditor> editors = new HashMap<>();

    @Setter
    private IEditor defaultEditor;

    ParamEditor(IEditor defaultEditor) {
        this.defaultEditor = defaultEditor;
    }

    public void add(Class<?> clazz, IEditor editor) {
        editors.put(clazz, editor);
    }

    public IEditor find(Class<?> paramType) {
        IEditor result = editors.get(paramType);
        if (result == null) {
            result = defaultEditor;
        }

        return result;
    }
}
