package com.example.base.ui

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import android.os.IBinder
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.example.base.R
import com.example.base.common.ActivityManager
import com.example.base.utils.DisplayUtil
import com.example.base.utils.DisplayUtil.recreate
import com.gyf.immersionbar.ImmersionBar


abstract class BaseActivity : AppCompatActivity() {
    private var fontScale = 1f

    @SuppressLint("UnsafeOptInUsageError")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ActivityManager.addActivity(this)
        setContentView(R.layout.mvvm_activity_base)
        var fragment = supportFragmentManager.findFragmentById(R.id.fcvContainer)
        if (fragment == null) {
            fragment = getFragment()
            fragment.arguments = intent.extras
            supportFragmentManager.beginTransaction()
                .replace(R.id.fcvContainer, getFragment())
                .commitNow()
        }
        if (immersionBarEnabled()) {
            initImmersionBar()
        }
    }

    abstract fun getFragment(): Fragment

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (ev.action == MotionEvent.ACTION_DOWN) {
            if (isShouldHideKeyboard(currentFocus, ev)) {
                hideKeyboard(currentFocus!!.windowToken)
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    private fun isShouldHideKeyboard(v: View?, event: MotionEvent): Boolean {
        if (v != null && v is EditText) {
            val l = intArrayOf(0, 0)
            v.getLocationInWindow(l)
            val left = l[0]
            val top = l[1]
            val bottom = top + v.getHeight()
            val right = left + v.getWidth()
            return !(event.x > left && event.x < right && event.y > top && event.y < bottom)
        }
        return false
    }

    private fun hideKeyboard(token: IBinder?) {
        if (token != null) {
            val im = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS)
        }
    }

    private fun initImmersionBar() {
        ImmersionBar.with(this)
            .keyboardEnable(false)
            .statusBarColor(android.R.color.transparent)
            .navigationBarColor(android.R.color.white)
            .statusBarDarkFont(true)
            .navigationBarDarkIcon(true)
            .keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
            .init()
    }

    private fun immersionBarEnabled(): Boolean {
        return true
    }

    override fun onDestroy() {
        ActivityManager.removeActivity(this)
        super.onDestroy()
    }

    override fun getResources(): Resources {
        val resources = super.getResources()
        return DisplayUtil.getResources(this, resources, fontScale)
    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(DisplayUtil.attachBaseContext(newBase!!, fontScale))
    }
}