package com.gh.gamecenter.toolbox

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.view.View.OnFocusChangeListener
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.TextView
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.gh.gamecenter.R
import com.gh.gamecenter.WebActivity
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.entity.SuggestType
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.view.CustomLinkMovementMethod
import com.gh.gamecenter.core.utils.SpanBuilder
import com.gh.gamecenter.databinding.ActivityToolboxBlockBinding
import com.gh.gamecenter.help.HelpAndFeedbackBridge
import com.google.android.material.appbar.AppBarLayout
import com.lightgame.utils.Util_System_Keyboard
import com.lightgame.utils.Utils


class ToolBoxBlockActivity : ToolBarActivity() {

    private var mIsEverPause = false
    private val mViewModel: ToolBoxViewModel by viewModels()
    private lateinit var mBinding: ActivityToolboxBlockBinding
    private lateinit var mAdapter: ToolBoxBlockAdapter
    private lateinit var mSearchAdapter: ToolBoxItemAdapter
    private lateinit var mLayoutManager: LinearLayoutManager

    private var mIsSearch = false  // 记录页面状态 搜索页面/普通页面

    override fun getLayoutId() = R.layout.activity_toolbox_block

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityToolboxBlockBinding.bind(mContentView)
        mAdapter = ToolBoxBlockAdapter(this, mViewModel)
        mSearchAdapter = ToolBoxItemAdapter(this, false, mViewModel)
        mLayoutManager = LinearLayoutManager(this@ToolBoxBlockActivity)
        setNavigationTitle("光环工具箱")
        updateStatusBarColor(R.color.background_white, R.color.background_white)

        // 跳转到工具箱 https://gitlab.ghzs.com/pm/halo-app-issues/issues/636
        val gameId = intent.getStringExtra(EntranceConsts.KEY_GAMEID)
        val targetUrl = intent.getStringExtra(EntranceConsts.KEY_URL)
        if (!gameId.isNullOrEmpty() && !targetUrl.isNullOrEmpty()) {
            mViewModel.getGameToolBoxData(gameId) {
                for (toolbox in it) {
                    if (targetUrl == toolbox.url) {
                        mViewModel.addToHistoryList(toolbox)
                        startActivity(WebActivity.getWebByCollectionTools(this, toolbox, false))
                    }
                }
            }
        }

        mBinding.run {
            toolboxRefresh.setColorSchemeResources(R.color.theme)
            toolboxRefresh.setOnRefreshListener {
                refresh()
            }
            feedbackTv.text = SpanBuilder("需要其他工具，点击反馈").click(this@ToolBoxBlockActivity, 7, 11, R.color.theme, false) {
                HelpAndFeedbackBridge.startSuggestionActivity(
                    this@ToolBoxBlockActivity,
                    SuggestType.normal,
                    null,
                    null
                )
            }.build()
            feedbackTv.movementMethod = CustomLinkMovementMethod.getInstance()
            closeIv.setOnClickListener { mBinding.feedbackCv.visibility = View.GONE }
            toolboxRv.adapter = mAdapter
            toolboxRv.layoutManager = mLayoutManager
            (toolboxRv.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
            toolboxAppbar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { _, verticalOffset ->
                toolboxRefresh.isEnabled = verticalOffset == 0
            })
        }
        mViewModel.toolBoxBlockList.observeNonNull(this) { list ->
            mAdapter.setDataList(list)
        }
        mViewModel.searchResultList.observeNonNull(this) { list ->
            mSearchAdapter.setDataList(list, true)
        }
        mViewModel.loadStatus.observeNonNull(this) {
            when (it) {
                LoadStatus.INIT_LOADED -> loadDone()
                LoadStatus.INIT_EMPTY -> loadEmpty()
                LoadStatus.INIT_FAILED -> loadError()
                else -> {
                    //do nothing
                }
            }
        }
        mViewModel.searchStatus.observeNonNull(this) {
            when (it) {
                LoadStatus.INIT_LOADED -> loadDone()
                LoadStatus.INIT_EMPTY -> loadEmpty()
                LoadStatus.INIT_FAILED -> loadError()
                else -> {
                    //do nothing
                }
            }
        }

        initSearch()
        mViewModel.getToolBoxList()
    }

    override fun onPause() {
        super.onPause()
        mIsEverPause = true
    }

    override fun onResume() {
        super.onResume()
        if (!mIsSearch && mIsEverPause) mAdapter.setHistoryList(mViewModel.getHistoryList())
    }

    private fun loadDone() {
        mBinding.toolboxRv.visibility = View.VISIBLE
        mBinding.toolboxRefresh.isRefreshing = false
        mBinding.reuseNoneData.root.visibility = View.GONE
        mBinding.reuseNoConnection.root.visibility = View.GONE
        mBinding.reuseLoading.root.visibility = View.GONE
    }

    private fun loadError() {
        mBinding.toolboxRv.visibility = View.GONE
        mBinding.toolboxRefresh.isRefreshing = false
        mBinding.reuseNoneData.root.visibility = View.GONE
        mBinding.reuseNoConnection.root.visibility = View.VISIBLE
        mBinding.reuseLoading.root.visibility = View.GONE
        mBinding.reuseNoConnection.root.setOnClickListener {
            refresh()
            mBinding.reuseNoConnection.root.visibility = View.GONE
            mBinding.reuseLoading.root.visibility = View.VISIBLE
        }
    }

    private fun loadEmpty() {
        mBinding.toolboxRv.visibility = View.GONE
        mBinding.toolboxRefresh.isRefreshing = false
        mBinding.reuseNoneData.root.visibility = View.VISIBLE
        mBinding.reuseNoConnection.root.visibility = View.GONE
        mBinding.reuseLoading.root.visibility = View.GONE
        mBinding.reuseNoneData.reuseNoneDataTv.text =
            if (mIsSearch) "这儿还没有内容噢~" else resources.getString(R.string.game_empty)
        mBinding.reuseNoneData.root.setOnClickListener {
            if (mIsSearch) HelpAndFeedbackBridge.startSuggestionActivity(
                this,
                SuggestType.functionSuggest,
                null,
                null
            )
        }
    }

    private fun refresh() {
        if (mIsSearch) {
            mViewModel.getSearchResultList()
        } else {
            mViewModel.getToolBoxList()
        }
    }

    private fun search(isSearch: Boolean, searchKey: String) {
        if (mBinding.reuseNoneData.root.visibility == View.VISIBLE) {
            mBinding.reuseNoneData.root.visibility = View.GONE
        }
        mIsSearch = isSearch
        mViewModel.mSearchKey = searchKey
        changeAdapter(isSearch)
        if (isSearch) {
            mBinding.reuseLoading.root.visibility = View.VISIBLE
            mBinding.toolboxRv.visibility = View.GONE
            mViewModel.getSearchResultList()
        }
    }

    private fun initSearch() {
        val backTv = mBinding.reuseSearchBar.tvBack
        val searchTv = mBinding.reuseSearchBar.tvSearch
        val searchEt = mBinding.reuseSearchBar.etSearch
        mBinding.reuseSearchBar.root.setPadding(16F.dip2px(), 8F.dip2px(), 16F.dip2px(), 8F.dip2px())
        backTv.setOnClickListener { backToBlock() }
        TextHelper.limitTheLengthOfEditText(searchEt, 20,
            object : TextHelper.ExceedTextLengthLimitCallback {
                override fun onExceed() {
                    toast("最多输入20字")
                }
            })
        searchTv.setOnClickListener {
            if (searchEt.text.toString().isEmpty()) {
                Utils.toast(this, R.string.search_hint)
                return@setOnClickListener
            }
            Util_System_Keyboard.hideSoftKeyboard(this, searchEt)
            search(true, searchEt.text.toString())
        }
        searchEt.onFocusChangeListener = OnFocusChangeListener { _: View?, hasFocus: Boolean ->
            if (!hasFocus) {
                Util_System_Keyboard.hideSoftKeyboard(this, searchEt)
            }
        }
        searchEt.setOnEditorActionListener { _: TextView?, actionId: Int, _: KeyEvent? ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                searchTv.performClick()
            }
            false
        }
    }

    private fun changeAdapter(isSearch: Boolean) {
        mBinding.toolboxRv.adapter = if (isSearch) mSearchAdapter else mAdapter
        mBinding.reuseSearchBar.tvBack.visibility = if (mIsSearch) View.VISIBLE else View.GONE
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_DOWN) {
            val v = currentFocus
            if (isShouldHideKeyboard(v, ev)) {
                Util_System_Keyboard.hideSoftKeyboard(this)
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    private fun isShouldHideKeyboard(v: View?, event: MotionEvent): Boolean {
        if (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 backToBlock() {
        search(false, "")
        mAdapter.setHistoryList(mViewModel.getHistoryList())
        mBinding.reuseSearchBar.etSearch.text.clear()
        loadDone()
    }

    override fun onBackPressed() {
        if (mIsSearch) {
            backToBlock()
        } else {
            super.onBackPressed()
        }
    }

    override fun isAutoResetViewBackgroundEnabled(): Boolean = true

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        updateStatusBarColor(R.color.background_white, R.color.background_white)
        if (::mBinding.isInitialized) {
            mBinding.run {
                toolboxRv.adapter?.run { notifyItemRangeChanged(0, itemCount) }
                feedbackCv.setCardBackgroundColor(R.color.background_white.toColor(this@ToolBoxBlockActivity))
            }
        }
    }

    companion object {
        @JvmStatic
        fun getIntent(context: Context, entrance: String?): Intent {
            val intent = Intent(context, ToolBoxBlockActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, entrance)
            return intent
        }
    }
}