package com.quyunshuo.androidbaseframemvvm.common.ui

import ZtlApi.ZtlManager
import android.R
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.appcompat.view.ContextThemeWrapper
import androidx.viewbinding.ViewBinding

import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameActivity
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.quyunshuo.androidbaseframemvvm.base.utils.ActivityStackManager
import com.quyunshuo.androidbaseframemvvm.base.utils.AndroidBugFixUtils
import com.quyunshuo.androidbaseframemvvm.base.utils.BarUtils
import com.quyunshuo.androidbaseframemvvm.base.utils.LanguageUtilsObject
import com.quyunshuo.androidbaseframemvvm.base.utils.XzjhSystemManagerUtlis
import com.quyunshuo.androidbaseframemvvm.base.utils.multilanguages.MultiLanguages
import com.quyunshuo.androidbaseframemvvm.common.keyboard.KeyboardView
import com.quyunshuo.androidbaseframemvvm.common.util.AppManager
import java.util.Locale


/**
 * Activity基类
 *
 * @author Qu Yunshuo
 * @since 8/27/20
 */
private const val TAG = "BaseActivity"

abstract class BaseActivity<VB : ViewBinding, VM : BaseViewModel> : BaseFrameActivity<VB, VM>() {
    private var keyboardView: KeyboardView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        AppManager.getAppManager().addActivity(this)
        //  if (SpUtils.getBoolean(MMKVName.LONG_CLICK_SHOW_STATUS_BAR, true) == true) {

            isShowSar(true)


        // }



    }

  /*  override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
        if (overrideConfiguration != null) {
            var uiMode = overrideConfiguration.uiMode;
            overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
            overrideConfiguration.uiMode = uiMode;
        }

        super.applyOverrideConfiguration(overrideConfiguration)
    }*/


  /*  override fun attachBaseContext(newBase: Context?) {
        val context: Context = MultiLanguages.attach(newBase)
        val configuration: Configuration = context.resources.configuration
        val wrappedContext: ContextThemeWrapper = object : ContextThemeWrapper(
            context,
            R.style.Theme_Light
        ) {
            override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
                if (overrideConfiguration != null) {
                    overrideConfiguration.setTo(configuration)
                }
                super.applyOverrideConfiguration(overrideConfiguration)
            }
        }
        super.attachBaseContext(wrappedContext)
       // super.attachBaseContext(MultiLanguages.attach(newBase))
    }*/

    fun isShowSar(flag: Boolean) {

        if (flag) {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {  // Build.VERSION_CODES.R corresponds to Android 11
                ZtlManager.GetInstance().openSystemBar(flag)
                XzjhSystemManagerUtlis.getInstance().xzjhSetSystemBarEnable(flag)
            } else {
                ZtlManager.GetInstance().openSystemBar(flag)
                XzjhSystemManagerUtlis.getInstance().xzjhSetSystemBarEnable(flag)
            }
        } else {
            ZtlManager.GetInstance().openSystemBar(flag)
            XzjhSystemManagerUtlis.getInstance().xzjhSetSystemBarEnable(flag)
            // ToastUtil.showToast(UiUtil.getStringRes(R.string.modified_failed))
        }
    }

    /**
     * 设置状态栏
     * 子类需要自定义时重写该方法即可
     * @return Unit
     */
    override fun setStatusBar() {
        BarUtils.transparentStatusBar(this)
        BarUtils.setStatusBarLightMode(this, true)
    }

    override fun onResume() {
        super.onResume()
        Log.d("ActivityLifecycle", "ActivityStack: ${ActivityStackManager.activityStack}")
    }

    override fun onDestroy() {
        super.onDestroy()
        // 解决某些特定机型会触发的Android本身的Bug
        AndroidBugFixUtils().fixSoftInputLeaks(this)
        AppManager.getAppManager().finishActivity(this)
    }


}