package com.swiftcleaner.chovey.view

import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.swiftcleaner.chovey.business.EventUtils
import com.swiftcleaner.chovey.model.util.ActivityManagerUtils
import com.swiftcleaner.chovey.util.ActivityLauncher

/**
 * 作者：马海钊
 * 时间：2025/2/8 12:18
 * 功能：
 */
abstract class BaseActivityKotlin<T : ViewBinding> : AppCompatActivity() {

    protected abstract val binding: T


    lateinit var launcher: ActivityLauncher


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        launcher = ActivityLauncher(this)
        setContentView(binding.root)

        EventUtils.event("page_${javaClass.simpleName}")
        initView()
        initListener()
    }

    protected abstract fun initView()

    protected open fun initListener() {}


    fun finishToMain() {
        if (this !is MainActivity && !ActivityManagerUtils.getInstance().isActivityInStack(MainActivity::class.java)) {
            startActivity(Intent(this, MainActivity::class.java))
        }
        finish()
    }

    var dialog: Dialog? = null
    override fun onDestroy() {
        super.onDestroy()
        ActivityManagerUtils.getInstance().removeActivity(this)
        if (dialog != null) {
            dialog?.dismiss()
            dialog = null
        }
    }

}