package com.cheng.blzb.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.window.OnBackInvokedDispatcher
import androidx.activity.addCallback
import androidx.core.os.BuildCompat
import com.cheng.blzb.R
import com.cheng.blzb.ui.fragment.guide.GuideFragment
import com.example.base.common.ActivityManager
import com.example.base.extensions.toast
import com.example.base.ui.BaseActivity

class GuideActivity :BaseActivity(){

    private var lastBackClickTime = 0L
    override fun getFragment() = GuideFragment()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        backApp()
    }

    @SuppressLint("UnsafeOptInUsageError")
    private fun backApp() {
        if (BuildCompat.isAtLeastT()) {
            onBackInvokedDispatcher.registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_DEFAULT) {
                exitApp()
            }
        } else {
            onBackPressedDispatcher.addCallback(this) {
                exitApp()
            }
        }
    }

    private fun exitApp() {
        if (System.currentTimeMillis() - lastBackClickTime > 1500) {
            toast(getString(R.string.exit_app))
            lastBackClickTime = System.currentTimeMillis()
        } else {
            ActivityManager.exitApp()
        }
    }
}