package com.njzscloud.common.http.resolver;

import cn.hutool.core.util.ReflectUtil;
import com.njzscloud.common.core.tuple.Tuple2;
import com.njzscloud.common.core.utils.Mime;
import com.njzscloud.common.http.annotation.XmlBodyParam;
import com.njzscloud.common.http.constant.HttpMethod;
import com.njzscloud.common.http.processor.XmlBodyParamProcessor;

public class XmlBodyParamResolver extends ParamResolver<XmlBodyParam, Tuple2<String, byte[]>> {
    byte[] result = null;

    public XmlBodyParamResolver() {
        super(XmlBodyParam.class);
    }

    @Override
    public Tuple2<String, byte[]> resolve(HttpMethod httpMethod, String urlTpl) {
        if (result == null) return null;

        return Tuple2.of(Mime.XML, result);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected boolean resolveParameter(XmlBodyParam anno, String paramName, Class<?> paramClazz, Object paramValue) {
        Class<? extends XmlBodyParamProcessor> processorClazz = anno.processor();
        result = ReflectUtil.newInstance(processorClazz).process(anno, paramName, paramClazz, paramValue);
        return true;
    }

    @Override
    @SuppressWarnings("unchecked")
    protected boolean resolveField(boolean hasParameterAnno, XmlBodyParam anno, String paramName, Class<?> paramClazz, Object paramValue) {
        if (hasParameterAnno) return false;

        Class<? extends XmlBodyParamProcessor> processorClazz = anno.processor();
        result = ReflectUtil.newInstance(processorClazz).process(anno, paramName, paramClazz, paramValue);
        return true;
    }
}
