package com.hw.nativeapp.httpnet;

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

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hw.nativeapp.MApplication;
import com.hw.nativeapp.R;
import com.hw.nativeapp.httpnet.entity.ResponseData;
import com.hw.nativeapp.ui.dialogs.LoadingDialog;

import java.text.ParseException;
import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * 通用返回处理
 *
 * @param <E>
 */
public abstract class ResponseConsumer<E> implements Consumer<ResponseData<E>> {

    protected ResponseData<E> responseData;

    @Override
    public void accept(ResponseData<E> responseData) throws Exception {
        this.responseData = responseData;
        if (responseData != null) {
            int code = responseData.getCode();
            String msg = responseData.getMsg();
            if (code != 0 && code != 200) {
                LogUtils.e("code : " + code + ", Error : " + msg);
//                LoadingDialogUtils.cancelLoadingDialog();
                LoadingDialog.getInstance(MApplication.getAppContext()).hide();
                ToastUtils.showShort(msg);
//                if (code == 1)
//                    ToastUtils.showShort(msg);
//                if ("该证件已经被其他手机号绑定".equals(msg)){
//                    hasBandPhone();
//                }
            }
            if (code == 0 || code == 200) {
                LoadingDialog.getInstance(MApplication.getAppContext()).hide();
                if (responseData.getData() != null)
                    onSuccess(responseData.getData());
                else if (responseData.getList() != null)
                    onListSuccess(responseData.getList());
                else

                    onSuccess(responseData.getData());
            } else if (code == 401) {
                /**
                 * token 过期
                 */
//                MApplication.loginout(false, "登录过期", null);
//                LoadingDialogUtils.cancelLoadingDialog();
                LoadingDialog.getInstance(MApplication.getAppContext()).hide();

            } else {
                onFailed(code, msg);
            }
        } else {
            ToastUtils.showShort(MApplication.getInstance().getResources().getString(R.string.request_failure));
//            LoadingDialogUtils.cancelLoadingDialog();
            LoadingDialog.getInstance(MApplication.getAppContext()).hide();
        }
    }

    public abstract void onSuccess(E data) throws ParseException, RemoteException;

    public void onListSuccess(List<E> list) {

    }

    protected void hasBandPhone() {

    }


    public void onFailed(int code, String msg) {
//        LoadingDialogUtils.cancelLoadingDialog();
        LoadingDialog.getInstance(MApplication.getAppContext()).hide();
        showErrorMsg(msg);
    }

    public static void showErrorMsg(String msg) {
        if (TextUtils.isEmpty(msg)) {
            ToastUtils.showShort("请求错误");
            return;
        }
        ToastUtils.showShort(msg);
    }

}
