package com.hh.lib_base.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.ImmersionBar
import com.hh.lib_base.manager.ActivityManager
import com.hh.lib_base.utils.LogUtils
import java.lang.ref.WeakReference


/**
 *
 * Description:
 * @author xwh
 * Time 2024/8/19
 *
 **/
open abstract class BaseActivity : AppCompatActivity() {
    var TAG: String? = null


    protected abstract fun getLayoutId(): Int
    protected abstract fun initView()
    protected abstract fun initData()
    protected abstract fun initEvent()

    protected open fun setHideTopBar() {
        // 全屏展示
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
    }


    open fun init() {
        ActivityManager.add(WeakReference<Activity>(this) )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        if (needBreakCreate()) {
            super.onCreate(savedInstanceState)
            finish()
            return
        }
        super.onCreate(savedInstanceState)
        setContentView(getLayoutId())
        TAG = javaClass.name
        init()
      //  setStatusBar()
        setHideTopBar()
        initView()
        initData()
        initEvent()

    }

    protected open fun needBreakCreate(): Boolean {
        return false
    }

    open fun setStatusBar() {
        if (ImmersionBar.isSupportStatusBarDarkFont()) {
            LogUtils.i("setStatusBar---isSupportStatusBarDarkFont")
            // 状态栏字体颜色为深色(android6.0以上或者miuiv6以上或者flymeOS4+)
            ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .init()
        } else {
            LogUtils.i("setStatusBar---默认")
            ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColorInt(Color.BLACK)
                .navigationBarColorInt(Color.BLACK)
                .autoDarkModeEnable(true)
                .init()
        }
    }


    protected open fun startActivity(c: Class<*>?) {
        this.startActivity(Intent(this, c))
    }

    protected open fun startActivityAndFinish(c: Class<*>?) {
        val intent = Intent(this, c)
        intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK)
        startActivity(intent)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityManager.removeExitsActivity(this)
        System.gc()
    }
}