package com.swago.baseswago.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.viewbinding.ViewBinding
import com.gyf.immersionbar.ImmersionBar
import java.lang.reflect.ParameterizedType

/**
 *@date 2021/8/1 10:15
 *description:
 */
abstract class BaseActivity<T:ViewBinding> : AppCompatActivity() {


    var mToolbar: Toolbar? = null
    private var centerText: TextView? = null

    lateinit var binding:T


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setImmersionBar(darkFont = false, fitSystemWindow = false)
        binding = getViewBindingForActivity(layoutInflater)
        setContentView(binding.root)

        initView()

    }


    abstract fun initView()

    fun setBackView(ivBack: ImageView?){
        ivBack?.setOnClickListener {
            onBackPressed()
        }
    }

    open fun setImmersionBar(darkFont: Boolean, fitSystemWindow: Boolean) {
        ImmersionBar.with(this)
            .statusBarDarkFont(darkFont)
            .keyboardEnable(false)
            .fitsSystemWindows(fitSystemWindow)
            .init()
    }


    @Suppress("UNCHECKED_CAST")
    private fun getViewBindingForActivity(layoutInflater: LayoutInflater): T {
        val type = javaClass.genericSuperclass as ParameterizedType
        val aClass = type.actualTypeArguments[0] as Class<*>
        val method = aClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        return method.invoke(null, layoutInflater) as T
    }

}