package com.all.pdfreader.pdf.reader.ui.act

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import com.all.pdfreader.pdf.reader.room.repository.PdfRepository
import com.all.pdfreader.pdf.reader.sp.AppStore
import com.all.pdfreader.pdf.reader.util.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.gyf.immersionbar.OnInsetsChangeListener

abstract class BaseActivity : AppCompatActivity() {
    protected var backPressedCallback: OnBackPressedCallback? = null
    /** 子类是否需要拦截返回 */
    protected open fun shouldInterceptBackPress(): Boolean = false

    /** 子类定义拦截后的操作（例如弹窗） */
    protected open fun onInterceptBackPressed() {}

    protected open val rootBottomView: View? = null
    protected abstract val TAG: String
    protected val appStore by lazy { AppStore(this) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setupBackPressedCallback()//初始化back事件
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    protected fun logDebug(message: String) {
        Log.d("ocean", "$TAG: $message")
    }

    protected fun logError(message: String, throwable: Throwable? = null) {
        Log.e("ocean", "$TAG: $message", throwable)
    }

    protected fun logInfo(message: String) {
        Log.i("ocean", "$TAG: $message")
    }

    protected fun logWarning(message: String) {
        Log.w("ocean", "$TAG: $message")
    }

    protected fun showToast(message: String) {
        ToastUtils.show(this, message)
    }

    //获取数据库实例
    protected fun getRepository(): PdfRepository {
        return PdfRepository.getInstance()
    }

    protected fun setupImmersionBar(block: ImmersionBar.() -> Unit) {
        val bar = ImmersionBar.with(this)

        // 通用逻辑：监听 insets 改变
        bar.setOnInsetsChangeListener(object : OnInsetsChangeListener {
            override fun onInsetsChanged(top: Int, bottom: Int, left: Int, right: Int) {
                rootBottomView?.setPadding(left, 0, right, bottom)
            }
        })
        bar.block()

        bar.init()
    }

    private fun setupBackPressedCallback() {
        backPressedCallback = object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                if (shouldInterceptBackPress()) {
                    // 由子类处理拦截动作
                    onInterceptBackPressed()
                } else {
                    // 不拦截：关闭自己
                    isEnabled = false
                    onBackPressedDispatcher.onBackPressed()
                }
            }
        }

        onBackPressedDispatcher.addCallback(this, backPressedCallback!!)

    }
}