/*
 * Copyright 2016 jeasonlzy(廖子尧)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.naz.sdkdemo.okgo.callback;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.naz.sdkdemo.utils.SPUtils;
import com.naz.sdkdemo.weight.XToast;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 描    述：对于网络请求是否需要弹出进度对话框
 * eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxMCJ9.Ms561Pcxa9K-p57V1mwNSZ2CiXiLXGo_ximCa85Lzs8
 * eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIyMCJ9.AF5G96sNIUXiwRHIsPko230S8jTgcJF_TEFU78DLjec
 * eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIzMCJ9.SnFn9uooEh8HE8emmYFgi_S8KY35hSwqdZf4tZo9978
 */
public abstract class DialogCallback<T> extends JsonCallback<T> {
    private KProgressHUD hud;
    private Context context;
    private boolean isShow;
    public DialogCallback(Context context) {
        super();
        this.context = context;
        isShow = true;
    }

    public DialogCallback(Context context, boolean isShow) {
        super();
        this.context = context;
        this.isShow = isShow;
    }
    @Override
    public void onStart(Request<T, ? extends Request> request) {
        super.onStart(request);
        if (isShow){
            showLoading();
        }
        HttpHeaders httpHeaders = request.getHeaders();
        httpHeaders.put("Authorization", "Bearer "+SPUtils.getString("TOKEN"));
    }

    @Override
    public void onError(Response<T> response) {
        super.onError(response);
        hideLoading();
        String msg = response.getException().getMessage();
        if (TextUtils.equals(msg, "401")) {
            if (SPUtils.getBoolean("IS_LOGIN")) {
                SPUtils.setBoolean("IS_LOGIN", false);
                SPUtils.setString("TOKEN", "");
                OkGo.getInstance().cancelAll();
                XToast.showToast("token过期，请重新登录！");
            }
        }else{
            XToast.showToast(msg);
        }
    }

    @Override
    public T convertResponse(okhttp3.Response response) throws Throwable {
        hideLoading();
        return super.convertResponse(response);
    }

    @Override
    public void onFinish() {
        super.onFinish();
        hideLoading();
    }

    @Override
    public void onSuccess(Response<T> response) {
        hideLoading();
    }

    @Override
    public void onCacheSuccess(Response<T> response) {
        super.onCacheSuccess(response);
    }

    public void showLoading() {
        if (!((AppCompatActivity)context).isFinishing()){
            //if (hud == null) {
            hud = KProgressHUD.create(context).setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
            //}
            hud.show();
        }
    }

    public void hideLoading() {
        if (hud != null) {
            hud.dismiss();
        }
    }
}
