package cn.superdesk.uniorder.openapicall.superdeskuniorder.security;

import cn.superdesk.uniorder.common.numerical.AnLongUtils;
import cn.superdesk.uniorder.openapicall.superdeskuniorder.service.OctlifeUniOrderCallApiService;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SignInterceptor extends BasePathMatchInterceptor {

	@Autowired
	private OctlifeUniOrderCallApiService octlifeUniOrderCallApiService;

	private String accessKeyId;
	private String accessKeySecret;

	public void setAccessKeyId(String accessKeyId) {
		this.accessKeyId = accessKeyId;
	}

	public void setAccessKeySecret(String accessKeySecret) {
		this.accessKeySecret = accessKeySecret;
	}

	@Override
	public Response doIntercept(Chain chain) throws IOException {

		String accessToken = octlifeUniOrderCallApiService.loginBySupplier(AnLongUtils.getLong(accessKeyId), accessKeySecret);

//		String accessToken = "";

		Request request = chain.request();
		Request newReq = request.newBuilder()
//				.addHeader("accessKeyId", accessKeyId)
//				.addHeader("accessKeySecret", accessKeySecret)
				.addHeader("token", accessToken)
				.build();
		return chain.proceed(newReq);
	}
}