package core.ui.activity;

import android.os.Bundle;
import android.os.CountDownTimer;

import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

import xn.xxp.HomeActivity;
import xn.xxp.app.LabApp;
import xn.xxp.home.notice.NoticeListActivity;
import xn.xxp.room.RoomTool;
import xn.xxp.room.bean.NoticeSummary;
import xn.xxp.room.dao.NoticeSummaryDao;
import xn.xxp.widget.ITitleBar;
import xn.xxp.widget.NavViewCompat;

public abstract class BaseCountDownActivity<VB extends ViewBinding> extends BaseActivity<VB> {
    private final int MAX_TIME = 120;

    private int finishCd = 0;

    private CountDownTimer countDownTimer;

    @Override
    protected void initViews(Bundle savedInstanceState) {
        super.initViews(savedInstanceState);
        ITitleBar iTitleBar = getMTitleBar();
        if (null != iTitleBar) {
            iTitleBar.setTitleInfoFromSp();
            NoticeSummaryDao noticeSummaryDao = RoomTool.getInstance().noticeSummaryDao();
            List<NoticeSummary> noticeSummaryList = noticeSummaryDao.getAll();
            if (null == noticeSummaryList) {
                noticeSummaryList = new ArrayList<>();
            }
            iTitleBar.updateNotice(noticeSummaryList);
            iTitleBar.setTitleListener(() -> ActivityUtils.startActivity(NoticeListActivity.class));
        }
        NavViewCompat navViewCompat = getMNavView();
        if (null != navViewCompat) {
            navViewCompat.setNavListener(new NavViewCompat.NavListener() {
                @Override
                public void onHomeViewClicked() {
                    logoutCountDownFinish();
                }

                @Override
                public void onBackViewClicked() {
                    finish();
                }

                @Override
                public void onFingerEnrollViewClicked() {

                }
            });
        }


        if (enabledBackCountDown()) {
            countDownTimer = new CountDownTimer(1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {

                }

                @Override
                public void onFinish() {
                    if (finishCd >= MAX_TIME) {
                        logoutCountDownFinish();
                    } else {
                        finishCd++;
                        countDownTimer.start();
                    }
                }
            };
        }
    }

    protected void logoutCountDownFinish() {
        LabApp.userVo = null;
        ActivityUtils.finishToActivity(HomeActivity.class, false);
        LogUtils.d(ActivityUtils.getActivityList());
    }

    public abstract ITitleBar getMTitleBar();

    public abstract NavViewCompat getMNavView();

    @Override
    protected void onResume() {
        super.onResume();
        if (null != countDownTimer) {
            finishCd = 0;
            countDownTimer.start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != countDownTimer) {
            countDownTimer.cancel();
        }
    }

    @Override
    public void onUserInteraction() {
        super.onUserInteraction();
        finishCd = 0;
    }

    protected boolean enabledBackCountDown() {
        return false;
    }
}
