package com.jwipc.api.interceptor;

import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.interceptor.Interceptor;
import com.dtflys.forest.reflection.ForestMethod;
import com.jwipc.api.dto.SapBaseParamDTO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Base64;


/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class SapInterceptor implements Interceptor<String> {
    @Value("${sys.sap-user}")
    private String name;
    @Value("${sys.sap-pw}")
    private String password;
    @Value("${sys.sap-sndprn}")
    private String SNDPRN;
    @Value("${sys.sap-rcvprn}")
    private String RCVPRN;


    @Override
    public boolean beforeExecute(ForestRequest request) {
        final String encodeToString = Base64.getEncoder().encodeToString((name + ":" + password).getBytes());
        String authHeader = "Basic " + encodeToString;
        request.addHeader("Authorization", authHeader);
        return true;
    }

    @Override
    public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) {
        if (args[0] instanceof SapBaseParamDTO) {
            final SapBaseParamDTO arg = (SapBaseParamDTO) args[0];
            arg.getIS_REQ().setSNDPRN(SNDPRN);
            arg.getIS_REQ().setRCVPRN(RCVPRN);
        }
    }
}
