package com.dahe.mylibrary.net;

import android.text.TextUtils;
import android.util.Log;


import com.google.zxing.common.StringUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;

/**
 * json的Interceptor
 */

public class JsonInterceptor implements Interceptor {
    private static final String TAG = "JsonInterceptor";

    public JsonInterceptor() {
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        ResponseBody responseBody = response.body();
        //为了不消耗buffer，我们这里使用source先获得buffer对象，然后clone()后使用
        BufferedSource source = responseBody.source();
        source.request(Long.MAX_VALUE); // Buffer the entire body.
        //获得返回的数据
        Buffer buffer = source.buffer();
        //使用前clone()下，避免直接消耗
        String responseBodyStr = buffer.clone().readString(Charset.forName("UTF-8"));
        Log.i(TAG, "result-body= " + responseBodyStr);
        try {
            JSONObject jsonObject = new JSONObject(responseBodyStr);
            String str = jsonObject.optString("data");
            int code = jsonObject.optInt("code");
            if (-1 == code) {
                jsonObject.put("data", new JSONObject());
                throw new ResultException();
            } else {
                if (TextUtils.isEmpty(str)) {
                    jsonObject.put("data", new JSONObject());
                }
            }

            String data = jsonObject.toString();
            responseBody = ResponseBody.create(MediaType.parse("application/json; charset=utf-8"), data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response = response.newBuilder().body(responseBody).build();
        return response;
    }
}
