package com.quyunshuo.androidbaseframemvvm.base.mvvm.v

import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import android.os.Looper
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.quyunshuo.androidbaseframemvvm.base.R
import com.quyunshuo.androidbaseframemvvm.base.constant.VersionStatus
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusUtils
import com.quyunshuo.androidbaseframemvvm.base.utils.LanguageUtil
import com.quyunshuo.androidbaseframemvvm.base.utils.RegisterEventBus
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils

import com.quyunshuo.androidbaseframemvvm.base.utils.network.AutoRegisterNetListener
import com.quyunshuo.androidbaseframemvvm.base.utils.network.NetworkStateChangeListener
import com.quyunshuo.androidbaseframemvvm.base.utils.network.NetworkTypeEnum
import com.quyunshuo.androidbaseframemvvm.base.utils.toast
import me.jessyan.autosize.AutoSizeCompat

/**
 * Activity基类
 *
 * @author Qu Yunshuo
 * @since 8/27/20
 */
abstract class BaseFrameActivity<VB : ViewBinding, VM : BaseViewModel> : AppCompatActivity(),
    FrameView<VB>, NetworkStateChangeListener {

    protected abstract val mViewModel: VM

    protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { createVB() }

    /**
     * 是否有 [RegisterEventBus] 注解，避免重复调用 [Class.isAnnotation]
     */
    private var mHaveRegisterEventBus = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)
        // ARouter 依赖注入
        ARouter.getInstance().inject(this)
        var className = localClassName
        Log.d("backinfo", "类名:" + className)
        // 根据子类是否有 RegisterEventBus 注解決定是否进行注册 EventBus
        if (javaClass.isAnnotationPresent(RegisterEventBus::class.java)) {
            mHaveRegisterEventBus = true
            EventBusUtils.register(this)
        }

        val languageType = SpUtils.getInt("LANGUAGE_TYPE", 0)!!
        val lang = LanguageUtil.language[languageType]
        Log.d("backinfo", "语言：" + lang)
        LanguageUtil.setAppLanguage(this, lang)
        setStatusBar()
        mBinding.initView()
        initNetworkListener()
        initObserve()
        initRequestData()
    }


    /*    override fun attachBaseContext(newBase: Context) {
            var languageType: Int = 0
            languageType = SpUtils.getInt("LANGUAGE_TYPE", 0)!!
            Log.d("backinfo", "languageType:$languageType")
            val language: String = LanguageUtil.language.get(languageType)
            Log.d("backinfo", "什么语言：$language")
            val context: Context = LanguageUtil.setAppLanguage(newBase, language)
            super.attachBaseContext(context)
        }*/


    /**
     * 初始化网络状态监听
     * @return Unit
     */
    private fun initNetworkListener() {
        lifecycle.addObserver(AutoRegisterNetListener(this))
    }

    /**
     * 设置状态栏
     * 子类需要自定义时重写该方法即可
     * @return Unit
     */
    open fun setStatusBar() {}

    /**
     * 网络类型更改回调
     * @param type Int 网络类型
     * @return Unit
     */
    override fun networkTypeChange(type: NetworkTypeEnum) {}

    /**
     * 网络连接状态更改回调
     * @param isConnected Boolean 是否已连接
     * @return Unit
     */
    override fun networkConnectChange(isConnected: Boolean) {
        toast(if (isConnected) getString(R.string.base_network_connected) else getString(R.string.base_network_disconnected))
        Log.d("2", "networkConnectChange: " + isConnected)
        VersionStatus.networkState = isConnected
    }

    override fun onDestroy() {
        // 根据子类是否有 RegisterEventBus 注解决定是否进行注册 EventBus
        if (mHaveRegisterEventBus) {
            EventBusUtils.unRegister(this)
        }

        super.onDestroy()
    }

    /*  override fun attachBaseContext(newBase: Context) {
          // 在 Android 10 上需要手动应用语言
          super.attachBaseContext(MultiLanguages.attach(newBase))
      }*/
    override fun attachBaseContext(newBase: Context) {
        val languageType = SpUtils.getInt("LANGUAGE_TYPE", 0)!!
        val language = LanguageUtil.language[languageType]
        val context = LanguageUtil.setAppLanguage(newBase, language)
        // 在 Android 10 上需要手动应用语言
        super.attachBaseContext(context)
    }

    override fun getResources(): Resources {
        // 主要是为了解决 AndroidAutoSize 在横屏切换时导致适配失效的问题
        // 但是 AutoSizeCompat.autoConvertDensity() 对线程做了判断 导致Coil等图片加载框架在子线程访问的时候会异常
        // 所以在这里加了线程的判断 如果是非主线程 就取消单独的适配
        if (Looper.myLooper() == Looper.getMainLooper()) {
            AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources()))
        }
        return super.getResources()
    }


}