package com.rc.core.ui.activity

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.rc.core.ui.ActivityCollector
import com.rc.core.ui.common.AbsUIDelegate
import com.rc.core.ui.common.IUIListener
import com.rc.core.util.ScreenAdapter
import io.reactivex.disposables.Disposable


/**
 * info
 *
 * @author ReiChin_
 */
abstract class RcBaseActivity<VB : ViewBinding> : AppCompatActivity(), IUIListener {

    private lateinit var _viewBinding: VB

    protected val viewBinding get() = _viewBinding

    private lateinit var mUIDelegate: AbsUIDelegate

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ActivityCollector.addActivity(this)
        ScreenAdapter.setCustomDensity(this)
        mUIDelegate = AbsUIDelegate.create()
        configImmersiveMode()
        beforeSetContentView()
        _viewBinding = createViewBinding()
        setContentView(_viewBinding.root)
        initViews(savedInstanceState)
        initListener()
        initData()
    }

    override fun onResume() {
        super.onResume()
        ScreenAdapter.setCustomDensity(this)
    }

    protected abstract fun createViewBinding(): VB

    protected open fun configImmersiveMode() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
            window.statusBarColor = statusBarColor()
        }
    }

    @ColorInt
    protected open fun statusBarColor(): Int = Color.WHITE

    protected open fun beforeSetContentView() {
    }

    protected open fun initViews(savedInstanceState: Bundle?) {
    }

    protected open fun initListener() {
    }

    protected open fun initData() {
    }

    override fun onDestroy() {
        super.onDestroy()
        mUIDelegate.clearDisposable()
        ActivityCollector.removeActivity(this)
    }

    override fun showLoading(message: String?, cancelable: Boolean) {
        if (!isDestroyed) {
            mUIDelegate.showLoading(this, message, cancelable)
        }
    }

    override fun dismissLoading() {
        mUIDelegate.dismissLoading()
    }

    override fun showToast(message: String) {
        mUIDelegate.showToast(this, message)
    }

    override fun showNetError(throwable: Throwable) {
        mUIDelegate.showNetError(this, throwable)
    }

    override fun addDisposable(disposable: Disposable) {
        mUIDelegate.addDisposable(disposable)
    }

    override fun createItemDecoration(): RecyclerView.ItemDecoration? {
        return mUIDelegate.createItemDecoration(this)
    }

    /**
     * 关闭软键盘
     */
    open fun hintKeyBoard() {
        //拿到InputMethodManager
        val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        //如果window上view获取焦点 && view不为空
        if (imm.isActive && currentFocus != null) {
            //拿到view的token 不为空
            if (currentFocus!!.windowToken != null) {
                //表示软键盘窗口总是隐藏，除非开始时以SHOW_FORCED显示。
                imm.hideSoftInputFromWindow(
                    currentFocus!!.windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS
                )
            }
        }
    }
}