import android.content.res.Resources
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.hjq.toast.Toaster
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.xhly.corelib.base.ui.CommonBaseActivity
import com.xhly.corelib.base.viewmodel.CommonBaseViewModel
import com.xhly.corelib.eventbus.UIEvent
import com.xhly.corelib.utils.LogShow
import com.xhly.corelib.utils.SharedPreferencesUtils
import com.xhly.corelib.utils.StatusBarUtil
import com.xhly.corelib.utils.getClass
import me.jessyan.autosize.AutoSizeCompat
import me.jessyan.autosize.AutoSizeConfig
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseActivity<VM : CommonBaseViewModel, VB : ViewBinding> : CommonBaseActivity() {
    lateinit var viewModel: VM
    lateinit var mBinding: VB
    val WRITECODE = 10086
    val spUtils by lazy { SharedPreferencesUtils.getInstance(this) }



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = getBinding()
        setContentView(mBinding.root)
        viewModel = ViewModelProvider(this).get(getClass(this))
        StatusBarUtil.StatusBarLightMode(this, true)
        EventBus.getDefault().register(this)
        startObserveError()
        try {
            initData()
            initView()
        } catch (e: Exception) {
            LogShow(e.toString())
        }
    }

    abstract fun getBinding(): VB
    abstract fun initData()
    abstract fun initView()

    private fun startObserveError() {
        viewModel.getError().observe(this) {
            Log.d("错误日志", it.toString())
        }
        viewModel.netMessage.observe(this) {
            //处理一些自定义的信息
            useNetMessage(it)
        }
    }

    open fun useNetMessage(s: String) {
        Toast(s)
    }


    public fun Toast(s: String) {
        try {
            Toaster.show(s)
            Toaster.setGravity(Gravity.CENTER)
        } catch (e: java.lang.Exception) {
            android.widget.Toast.makeText(this, s, android.widget.Toast.LENGTH_SHORT).show()
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onUiEvent(uiEvent: UIEvent) {
    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        /*  if (requestCode == WRITECODE) {
              if (grantResults[0] == -1) {
                  Toast("没有权限")
              } else {
                  Toast("权限申请成功，请点击保存")
              }
          }*/
    }

    open fun initBaseRefreshLayout(smart: SmartRefreshLayout) {
        smart.setRefreshFooter(ClassicsFooter(this))
        smart.setEnableRefresh(false)
        smart.setEnableAutoLoadMore(false)
    }

    override fun getResources(): Resources {
        val width = AutoSizeConfig.getInstance().screenWidth
        val height = AutoSizeConfig.getInstance().screenHeight
        runOnUiThread {
            AutoSizeCompat.autoConvertDensity(super.getResources(), 853f, width > height)
        }
        return super.getResources()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    fun hiddenSoft(editText: EditText){
        try {
            val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            // 隐藏输入法键盘
            inputMethodManager.hideSoftInputFromWindow(editText.windowToken, 0)
        }catch (e:Exception){

        }
    }
}