package aicare.net.cn.sdk.ailinksdkdemoandroid.base

import aicare.net.cn.sdk.ailinksdkdemoandroid.dialog.LoadingIosDialogFragment
import android.R
import android.app.Activity
import android.content.Context
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import androidx.annotation.ColorRes
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.viewbinding.ViewBinding

abstract class KBleBaseNewActivity<T : ViewBinding> : BleNewBaseActivity() {

    val TAG = this.javaClass.name
    lateinit var mContext: Context
    @ColorRes
    var mStatusBarColor: Int = 0
    lateinit var binding: T

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = getLayoutView()
//        initWindow()
        setContentView(binding.root)
        mContext = this
        init()

    }

    /**
     * 初始化
     */
    private fun init() {
        findTopView()
        initData()
        initListener()
    }


    private fun findTopView() {
        binding.root.findViewWithTag<View>("tbPublicTitle")?.let {
            if (it is Toolbar) {
                it.title = ""
                setSupportActionBar(it)
            }
        }
    }

    /**
     * 获取当前activity的布局
     *
     * @return int
     */
    protected abstract fun getLayoutView(): T


    open fun initWindow() {
        //禁止横屏
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        //设置状态栏文字为黑色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
        setStatusBarColor(mStatusBarColor)
    }

    fun setStatusBarColor(color: Int) {
        if (color != 0) {
            this.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            this.window.statusBarColor = ContextCompat.getColor(this, color)
        }
    }

    /**
     * 返回键
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.repeatCount == 0) {
            myFinish()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    /**
     * 返回
     */
    protected open fun myFinish() {
        finish()
    }


    /**
     * 初始化事件
     */
    protected abstract fun initListener()

    /**
     * 初始化数据
     */
    protected abstract fun initData()


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        if (id == R.id.home) {
            myFinish()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    //--------------------------start Loading--------------------------
    private var mDialogFragment: LoadingIosDialogFragment? = null

    /**
     * 显示加载
     */
    fun showLoading() {
        if (mDialogFragment == null) {
            mDialogFragment = LoadingIosDialogFragment()
        }
        mDialogFragment!!.show(supportFragmentManager)
    }

    /**
     * 关闭加载
     */
    fun dismissLoading() {
        if (mDialogFragment != null) {
            mDialogFragment!!.dismiss()
        }
    }

    //--------------------------end Loading--------------------------


}