package com.tenlionsoft.baselib.core.network.callback;

import android.os.Handler;
import android.util.Log;

import com.google.gson.Gson;
import com.tenlionsoft.baselib.core.network.response.GsonResponseHandler;
import com.tenlionsoft.baselib.core.network.response.JsonResponseHandler;
import com.tenlionsoft.baselib.core.network.response.RawResponseHandler;
import com.tenlionsoft.baselib.core.network.response.ResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by Xuer on 2017/3/8.
 */
public class RequestCallBack implements Callback {
    private Handler handler;
    private ResponseHandler responseHandler;

    public RequestCallBack(ResponseHandler responseHandler) {
        handler = new Handler();
        this.responseHandler = responseHandler;
    }

    @Override
    public void onFailure(Call call, final IOException e) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                responseHandler.onFailure(0, e.toString());
            }
        });
    }

    @Override
    public void onResponse(Call call, final Response response) throws IOException {
        if (response.isSuccessful()) {
            final String response_body = response.body().string();
            if (responseHandler instanceof JsonResponseHandler) {       //json回调
                try {
                    final JSONObject jsonBody = new JSONObject(response_body);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                ((JsonResponseHandler) responseHandler).onSuccess(response.code(), jsonBody);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (JSONException e) {
//                    LogUtils.e("onResponse fail parse jsonobject, body=" + response_body);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            responseHandler.onFailure(response.code(), "fail parse jsonobject, body=" + response_body);
                        }
                    });
                }
            } else if (responseHandler instanceof GsonResponseHandler) {    //gson回调
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Gson gson = new Gson();
                            ((GsonResponseHandler) responseHandler).onSuccess(response.code(),
                                    gson.fromJson(response_body, ((GsonResponseHandler) responseHandler).getType()));
                        } catch (Exception e) {
                            Log.e("Gson Error",e.getMessage());
//                            LogUtils.e("onResponse fail parse gson, body=" + response_body, e);
                            responseHandler.onFailure(response.code(), "fail parse gson, body=" + response_body);
                        }

                    }
                });
            } else if (responseHandler instanceof RawResponseHandler) {     //raw字符串回调
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ((RawResponseHandler) responseHandler).onSuccess(response.code(), response_body);
                    }
                });
            }
        } else {
//            LogUtils.e("onResponse fail status=" + response.code());

            handler.post(new Runnable() {
                @Override
                public void run() {
                    responseHandler.onFailure(0, "fail status=" + response.code());
                }
            });
        }
    }
}
