package com.rc.core.ui.activity;

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 java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.disposables.Disposable;

public class BaseActivityHelp {
    private BaseActivity baseActivity;

    private ThreadUtils.SimpleTask<Object> cdDismissTask;

    public BaseActivityHelp(BaseActivity baseActivity) {
        this.baseActivity = baseActivity;
    }

    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();
            }
        }, 20, TimeUnit.SECONDS);
    }

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

    public void showToast(String msg) {
        ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), msg, Toast.LENGTH_LONG).show());
    }

    public void showNetError(Throwable throwable) {
        showToast(throwable.getMessage());
    }

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

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

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


}
