package com.rc.core.ui.fragment;

import android.text.TextUtils;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.kongzue.dialogx.DialogX;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.rc.httpcore.exception.NetException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.disposables.Disposable;
import retrofit2.HttpException;

public class BaseFragmentHelp {
    private BaseFragment baseFragment;
    private ThreadUtils.SimpleTask<Object> cdDismissTask;

    public BaseFragmentHelp(BaseFragment baseFragment) {
        this.baseFragment = baseFragment;
    }

    public void onDestroy() {

    }

    public void showLoading(String msg) {
        ThreadUtils.cancel(cdDismissTask);
        WaitDialog.show(msg).setEnterAnimDuration(0).setExitAnimDuration(0).setCancelable(DialogX.cancelable);
        ThreadUtils.executeByCachedWithDelay(cdDismissTask = new ThreadUtils.SimpleTask<Object>() {
            @Override
            public Object doInBackground() throws Throwable {
                return null;
            }

            @Override
            public void onSuccess(Object result) {
                WaitDialog.dismiss();
            }
        }, 30, TimeUnit.SECONDS);
    }

    public void dismissLoading() {
        ThreadUtils.cancel(cdDismissTask);
        WaitDialog.dismiss();
    }

    public void showToast(String msg) {
        Toast.makeText(ActivityUtils.getTopActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    public void showNetError(Throwable throwable) {
        String message = "";
        if (throwable instanceof NetException) {
            message = throwable.getMessage();
            if (!TextUtils.isEmpty(message)) {
                message = "接口请求失败(" + ((NetException) throwable).getCode() + ")";
            }
        } else if (throwable instanceof SocketTimeoutException) {
            message = "请求超时，请稍后重试";
        } else if (throwable instanceof ConnectException) {
            message = "无法连接服务器，请检查网络";
        } else if (throwable instanceof HttpException) {
            message = "服务器繁忙，请稍后重试";
        } else {
            if (!TextUtils.isEmpty(message)) {
                message = throwable.getMessage();
            }
        }
        showToast(message);
    }

    public void addDisposable(Disposable disposable) {
        if (null != baseFragment.compositeDisposable && disposable != null && !baseFragment.compositeDisposable.isDisposed()) {
            baseFragment.compositeDisposable.add(disposable);
        }
    }

    public void delDisposable(Disposable disposable) {
        if (null != baseFragment.compositeDisposable && disposable != null) {
            baseFragment.compositeDisposable.delete(disposable);
        }
    }

    public void clearDisposable() {
        if (null != baseFragment.compositeDisposable) {
            baseFragment.compositeDisposable.dispose();
        }
    }
}
