package com.common.lib_base.common.http.rx;

import android.content.Context;

import com.common.lib_base.utils.LogUtils;
import com.common.lib_base.utils.ui.ToastUtils;
import com.google.gson.JsonParseException;
import com.common.lib_base.common.http.exception.ApiException;
import com.common.lib_base.common.http.exception.BaseException;
import com.common.lib_base.common.http.exception.ErrorMessageFactory;

import java.net.SocketException;
import java.net.SocketTimeoutException;

import retrofit2.HttpException;

/**
 * Created by admin on 2018/3/16.
 *
 * @version $Rev$
 * @time 2018/3/16 12:27
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDate $Date$
 * @updateDes ${TODO}
 */

public class RxErrorHandler {

    // 当前类名
    public String mTag = getClass().getSimpleName()+"--";

    private Context mContext;

    public RxErrorHandler(Context context) {
        this.mContext = context;
    }

    public BaseException handleError(Throwable e){

        BaseException exception = new BaseException();

        LogUtils.e(mTag+"BaseException-->Code:"+exception.getCode());
        LogUtils.e(mTag+"BaseException-->Message:"+exception.getMessage());
        LogUtils.e(mTag+"BaseException-->DispalyMessage:"+exception.getDispalyMessage());

//        if(exception.getCode() == null){
//            LogUtils.e(mTag+"code == null  ERROR_TOKEN ");
//            exception.setCode(BaseException.ERROR_TOKEN);
//        }else

        if(e instanceof ApiException){
//            Log.e("Test",mTag+"ApiException");
            LogUtils.e(mTag+"ApiException");
            exception.setCode(BaseException.JSON_ERROR);
            exception.setDispalyMessage(((ApiException)e).getDispalyMessage());
            LogUtils.e(mTag+"ApiException--getCode:"+((ApiException)e).getCode());
            LogUtils.e(mTag+"ApiException--getMessage:"+((ApiException)e).getDispalyMessage());
            /**
             * 此处，因为服务端，已经在返回结果中，给了msg，不需要本地写错误code对应的msg，所以可以直接返回
             * 如果服务端未给msg，则需要在BaseException类中，定义错误提示
             */
            return exception;
        }
        else if(e instanceof JsonParseException){
//            Log.e("Test",mTag+"JsonParseException");
            LogUtils.e(mTag+"JsonParseException");
            exception.setCode(BaseException.JSON_ERROR);
        }
        else if(e instanceof SocketException){
//            Log.e("Test",mTag+"SocketException");
            LogUtils.e(mTag+"SocketException");
            exception.setCode(BaseException.SOCKET_ERROR);
        }
        else if(e instanceof SocketTimeoutException){
//            Log.e("Test",mTag+"SocketTimeoutException");
            LogUtils.e(mTag+"SocketTimeoutException");
            exception.setCode(BaseException.SOCKET_TIMEOUT_ERROR);
        }
        else if(e instanceof HttpException){
//            Log.e("Test",mTag+"HttpException");
            LogUtils.e(mTag+"HttpException");
            exception.setCode(((HttpException)e).code()+"");
        }
        else{
//            Log.e("Test",mTag+"UNKNOWN_ERROR");
            LogUtils.e(mTag+"UNKNOWN_ERROR");
            exception.setCode(BaseException.UNKNOWN_ERROR);
        }

        LogUtils.e(mTag+"exception.getCode():"+exception.getCode());
        exception.setDispalyMessage(ErrorMessageFactory.create(mContext,exception.getCode()));

        return exception;

    }


    public void showErrorMessage(BaseException e){
        ToastUtils.showLongToast(mContext,e.getDispalyMessage());
    }




}
