package com.yidian.common.base

import android.os.Bundle
import android.view.View
import androidx.viewbinding.ViewBinding
import com.yidian.common.R
import com.yidian.news.ui.SystemBarUtils
import com.yidian.nightmode.base.NightAppCompatActivity
import com.yidian.nightmode.preference.NightModeConfig
import com.yidian.xpage.XPageViewProtocol


abstract class BaseActivity<VB :ViewBinding> : NightAppCompatActivity(), XPageViewProtocol {
    protected lateinit var viewBind:VB

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBind = createViewBinding()
        extraViewBinding()
        setContentView(viewBind.root)
        init(savedInstanceState)
    }

    /**
     * 如果有多个viewBinding需要初始化时
     */
    open fun extraViewBinding() {

    }

    override fun updateCover(isNight: Boolean) {}

    override fun getCurrentTheme(isNight: Boolean): Int {
        return if (isNight) R.style.subway_theme_night else R.style.subway_theme_day
    }

    override fun setContentView(view:View){
        SystemBarUtils.setStatusBarTransparent(this.window)
        if (allowChangeStatusBarTextColor() ) {
            setStatusBarTextColor(NightModeConfig.getInstance().nightMode)
        }
        super.setContentView(view)
    }


    private  fun allowChangeStatusBarTextColor(): Boolean {
        return SystemBarUtils.supportTranslucentStatusBar() && SystemBarUtils.supportChangeStatusBarTextColor()
    }
    abstract fun createViewBinding(): VB

    override fun onNightModeChanged(isNight: Boolean) {
        super.onNightModeChanged(isNight)
        if (allowChangeStatusBarTextColor()) {
            setStatusBarTextColor(isNight)
        }
    }

     open fun setStatusBarTextColor(isNight: Boolean) {
        if (isNight) {
            if (this.useWhiteStatusBarTextColorInNightMode()) {
                SystemBarUtils.setStatusBarTextColorWhite(this)
            } else {
                SystemBarUtils.setStatusBarTextColorBlack(this)
            }
        } else if (this.useBlackStatusBarTextColorInDayMode()) {
            SystemBarUtils.setStatusBarTextColorBlack(this)
        } else {
            SystemBarUtils.setStatusBarTextColorWhite(this)
        }
    }

    private fun useBlackStatusBarTextColorInDayMode(): Boolean {

        return true
    }

    private fun useWhiteStatusBarTextColorInNightMode(): Boolean {

        return true
    }

    protected open fun init(savedInstanceState: Bundle?) {}

}
