package com.jady.retrofitclient.subscriber;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.jady.retrofitclient.callback.HttpCallback;
import com.jady.retrofitclient.callback.LoadingCallback;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by jady on 2016/12/8.
 */

public class CommonResultSubscriber<T extends ResponseBody> extends Subscriber<T> {

    public static final String TAG = "CommonResultSubscriber";

    private Context mContext;
    private boolean showLoadingDailog = false;
    private String loadingMsg;

    private LoadingCallback loadingCallback;
    private HttpCallback httpCallback;

    /**
     * 不带加载框回调
     *
     * @param mContext
     * @param callback
     */
    public CommonResultSubscriber(Context mContext, HttpCallback callback) {
        this.mContext = mContext;
        this.httpCallback = callback;
    }

    /**
     * 带加载框回调
     *
     * @param mContext
     * @param callback
     * @param loadingMsg
     */
    public CommonResultSubscriber(Context mContext, HttpCallback callback, String loadingMsg) {
        this.mContext = mContext;
        this.httpCallback = callback;
        this.loadingMsg = loadingMsg;

        if (!TextUtils.isEmpty(loadingMsg)) {
            try {
                loadingCallback = (LoadingCallback) mContext;
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (loadingCallback != null) {
                showLoadingDailog = true;
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (showLoadingDailog && loadingCallback != null) {
            loadingCallback.onStartLoading(loadingMsg);
        }
    }

    @Override
    public void onCompleted() {
        if (showLoadingDailog) {
            loadingCallback.onFinishLoading();
        }
    }

    @Override
    public void onError(Throwable e) {
        if (showLoadingDailog && loadingCallback != null) {
            loadingCallback.onFinishLoading();
        }
        e.printStackTrace();
        if (httpCallback != null) {
            httpCallback.onFailure("failure", e.getMessage());
        }
    }

    @Override
    public void onNext(T t) {
        if (t.contentLength() == 0) {
            return;
        }
        if (httpCallback != null) {
            Type genericityType = httpCallback.getGenericityType();
            switch (((Class) genericityType).getSimpleName()) {
                case "Object":
                case "String":
                    try {
                        httpCallback.onResolve(t.string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    try {
                        this.httpCallback.onResolve((new Gson()).fromJson(t.string(), genericityType));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }
}

@@ -2423,32 +2423,72 @@
 back != null) %7B%0A
+            boolean returnJson = false;%0A
             Type
@@ -2535,24 +2535,79 @@
 cityType();%0A
+            if (genericityType instanceof Class) %7B%0A    
             
@@ -2654,24 +2654,28 @@
 leName()) %7B%0A
+    
             
@@ -2705,24 +2705,28 @@
             
+    
 case %22String
@@ -2744,34 +2744,223 @@
             
+    returnJson = true;%0A                        break;%0A                    default:%0A                        break;%0A                %7D%0A            %7D%0A%0A            if (returnJson) %7B%0A                
 try %7B%0A
-    
             
@@ -3011,36 +3011,32 @@
                 
-  
-  
 %7D catch (IOExcep
@@ -3037,36 +3037,32 @@
 OException e) %7B%0A
-    
                 
@@ -3094,36 +3094,32 @@
                 
-    
 %7D%0A              
@@ -3120,52 +3120,17 @@
     
-        break;%0A                default:%0A    
+%7D else %7B%0A
     
@@ -3139,36 +3139,32 @@
           try %7B%0A
-    
                 
@@ -3255,36 +3255,32 @@
                 
-   
- 
 %7D catch (Excepti
@@ -3279,36 +3279,32 @@
 (Exception e) %7B%0A
-    
                 
@@ -3348,40 +3348,9 @@
     
-    %7D%0A                    break;
+%7D
 %0A   

fix some bugs