package com.sheep.gamegroup.model.util;

import android.text.TextUtils;

import com.sheep.gamegroup.model.api.BaseMessageConverter;
import com.sheep.gamegroup.util.DeviceUtil;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;

/**
 * http 请求添加公共的参数
 * Created by ljy on 2018/3/18.
 */

public class AddPublicParamsIntercept implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request oldRequest = chain.request();
        //新的请求，添加参数
        Request newRequest = addParam(oldRequest);

        return chain.proceed(newRequest);
    }

    /**
     * 添加公共参数
     * 增加同步锁，防止多个HTTP同时请求时，加密出现异常bug
     *
     .setQueryParameter("test3","test3")
     .addQueryParameter("test","test")
     .setEncodedQueryParameter("test2","test2")
     * @param oldRequest
     * @return
     */
    synchronized private Request addParam(Request oldRequest) {
        HttpUrl.Builder b = oldRequest.url()
                .newBuilder();
        String method = oldRequest.method();
        RequestBody body = oldRequest.body();
        if (BuildConfig.XXTEA_ENCRYPT && (method.equals("POST") || method.equals("PUT"))) {
            RequestBody requestBody = oldRequest.body();
            if (requestBody instanceof FormBody) {
            } else if (requestBody != null){
                //buffer流
                Buffer buffer = new Buffer();
                try {
                    requestBody.writeTo(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String oldParamsJson = buffer.readUtf8();
                String newJsonParams = BaseMessageConverter.encrypt(oldParamsJson);
                if(!TextUtils.isEmpty(newJsonParams))
                    body = RequestBody.create(requestBody.contentType(), newJsonParams);
            }
        }
        String id = SpUtils.getToken(SheepApp.getInstance());
        String deviceId = DeviceUtil.getDeviceId(SheepApp.getInstance());
        Request.Builder requestBuilder = oldRequest.newBuilder();
        String tokenKey = "Authorization";
        if (oldRequest.header(tokenKey) == null) {
            requestBuilder.addHeader("Authorization", TextUtils.isEmpty(id) ? "" : id);//header 会覆盖以前的  header
        }

        return requestBuilder
                .addHeader("X-Device-Id", TextUtils.isEmpty(deviceId) ? "" : deviceId)//header 会覆盖以前的  header
                .addHeader("platform", "andriod")//header 会覆盖以前的  header
                .method(method, body)
                .url(b.build())
                .build();
    }
}
