package com.rc.core.ui.activity;

import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.rc.core.util.ScreenAdapter;
import com.rc.httpcore.bean.ConfigBean;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import xn.hxp.app.ChemicalApp;
import xn.hxp.comm.Constants;

public abstract class BaseActivity extends AppCompatActivity {

    private boolean isCountCdTime = true;
    private int cdTime = 60;
    private BaseActivityHelp baseActivityHelp;
    protected CompositeDisposable compositeDisposable = new CompositeDisposable();
    private final ThreadUtils.SimpleTask<Object> cdTask = new ThreadUtils.SimpleTask<Object>() {
        @Override
        public Object doInBackground() throws Throwable {
            return null;
        }

        @Override
        public void onSuccess(Object result) {
            if (isCountCdTime) {
                cdTime(cdTime);
                cdTime--;
                if (cdTime <= 0) {
                    cdFinish();
                    ThreadUtils.cancel(cdTask);
                }
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cdTime = 90;
        ConfigBean configBean = ChemicalApp.confs;
        if (null != configBean) {
            cdTime = configBean.getBackTime();
            Constants.INSTANCE.setOFFTIME_TIME(configBean.getOffTime());
        }

        ScreenAdapter.INSTANCE.setCustomDensity(this);

        ViewBinding binding = setViewBinding();
//        ViewGroup mainVG = (ViewGroup) binding.getRoot();
//        mainVG.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        setContentView(binding.getRoot());
        ThreadUtils.executeByCachedAtFixRate(cdTask, 1, TimeUnit.SECONDS);
        baseActivityHelp = new BaseActivityHelp(this);
        onInit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.d("activity生命周期", ActivityUtils.getActivityList());
        ScreenAdapter.INSTANCE.setCustomDensity(this);
        isCountCdTime = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isCountCdTime = false;
    }

    @Override
    protected void onStop() {
        super.onStop();
        isCountCdTime = false;
    }

    @Override
    public void onUserInteraction() {
        super.onUserInteraction();
        ConfigBean configBean = ChemicalApp.confs;
        if (null != configBean) {
            cdTime = configBean.getBackTime();
            Constants.INSTANCE.setOFFTIME_TIME(configBean.getOffTime());
        }
    }

    protected abstract ViewBinding setViewBinding();

    protected abstract void onInit();

    protected abstract void cdFinish();

    protected abstract void cdTime(int cd);

    protected void setCdTime(int cdTime) {
        this.cdTime = cdTime;
    }

    public void showLoading(String msg) {
        if (null != baseActivityHelp) {
            baseActivityHelp.showLoading(msg);
        }
    }

    public void dismissLoading() {
        if (null != baseActivityHelp) {
            baseActivityHelp.dismissLoading();
        }

    }

    public void showToast(String msg) {
        if (null != baseActivityHelp) {
            baseActivityHelp.showToast(msg);
        }

    }

    public void showNetError(Throwable throwable) {
        if (null != baseActivityHelp) {
            baseActivityHelp.showNetError(throwable);
        }

    }

    public void addDisposable(Disposable disposable) {
        if (null != baseActivityHelp) {
            baseActivityHelp.addDisposable(disposable);
        }

    }

    public void delDisposable(Disposable disposable) {
        if (null != baseActivityHelp) {
            baseActivityHelp.delDisposable(disposable);
        }
    }

    @Override
    protected void onDestroy() {
        if (null != baseActivityHelp) {
            baseActivityHelp.onDestroy();
        }
        super.onDestroy();
        isCountCdTime = false;
        ThreadUtils.cancel(cdTask);
    }
}
