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

import cc.smtweb.framework.core.mvc.controller.IBeanContext;
import cc.smtweb.framework.core.mvc.controller.IEditor;
import lombok.Getter;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * API方法参数访问器
 *
 * @author xkliu
 */
@Getter
public class MethodParamAccess {
    private int bindType;
    private final IEditor editor;
    private final String paramName;
    private final Class<?> collectionType;
    private final Class<?> paramType;

    public MethodParamAccess(IEditor editor, String paramName, Class<?> paramType, int bindType, Class<?> collectionType) {
        this.editor = editor;
        this.paramName = paramName;
        this.paramType = paramType;
        this.bindType = bindType;
        this.collectionType = collectionType;
    }

    public Object getParamValue(IBeanContext context, Map<String, Object> params, String body, HttpServletRequest request) {
        if (editor != null) {
            return editor.getParamValue(paramName, paramType, context, params, body, request);
        }

        return null;
    }

    @Override
    public String toString() {
        return paramName + ":" + paramType.getSimpleName() + "-" + editor.getClass().getSimpleName();
    }
}
