package com.gh.gamecenter.amway.search

import android.os.Bundle
import android.view.View
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.LinearLayoutManager
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.FragmentAmwaySearchDefaultBinding
import com.gh.gamecenter.eventbus.EBSearch
import com.gh.gamecenter.search.SearchDefaultFragment
import com.lightgame.utils.Util_System_Keyboard
import org.greenrobot.eventbus.EventBus

class AmwaySearchDefaultFragment : SearchDefaultFragment() {

    private lateinit var mViewModel: AmwaySearchViewModel

    private val mAmwayBinding by lazy { FragmentAmwaySearchDefaultBinding.inflate(layoutInflater) }

    override fun getLayoutId() = 0
    override fun getInflatedLayout() = mAmwayBinding.root

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewModel = viewModelProviderFromParent()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mViewModel.playedGames.observeNonNull(viewLifecycleOwner) {
            defaultViewModel?.isExistHotSearch = it.isNotEmpty()
            updateView()
            mBinding.hotList.run {
                layoutManager = LinearLayoutManager(context)
                adapter = AmwaySearchAdapter(context, mViewModel, "安利墙搜索-最近玩过").apply { setData(it) }
            }

            updateNoPlayedGameHint()
        }
    }

    override fun initView() {
        mBinding = mAmwayBinding.searchContent
        mBinding.hotHeadContainer.headTitle.text = "最近玩过"
        mBinding.historyFlexContainer.setLimitHeight(mFlexMaxHeight)

        updateHistorySearchView(null)
        defaultViewModel?.historySearchLiveData?.observe(this) {
            updateHistorySearchView(it)
        }

        initHeadView()
    }

    override fun initHeadView() {
        mBinding.historyHeadContainer.run {
            headTitle.text = getString(R.string.search_history)
            headTitle.textSize = 16F
            headActionTv.text = "清空"
            headActionTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(requireContext()))
            headActionTv.setDrawableStart(
                AppCompatResources.getDrawable(
                    requireContext(),
                    R.drawable.search_history_delete
                )
            )
            headActionTv.setOnClickListener {
                DialogHelper.showCenterWarningDialog(requireContext(), "清空记录", "确定清空历史搜索记录？", confirmClickCallback = {
                    defaultViewModel?.deleteAll()
                })
            }
        }
    }

    private fun updateNoPlayedGameHint() {
        val historyList = defaultViewModel?.historySearchLiveData?.value
        if (!historyList.isNullOrEmpty()) {
            mAmwayBinding.noDataContainer.visibility = View.GONE
            return
        }

        if (mViewModel.playedGames.value == null || mViewModel.playedGames.value?.size == 0) {
            mAmwayBinding.noDataContainer.visibility = View.VISIBLE
        } else {
            mAmwayBinding.noDataContainer.visibility = View.GONE
        }
    }

    override fun updateHistorySearchView(historyList: List<String>?) {
        defaultViewModel?.isExistHistory = historyList?.isNotEmpty() == true
        updateView()
        updateNoPlayedGameHint()
        historyList?.let {
            createFlexContent(mBinding.historyFlex, historyList, clickListener = { id ->
                val key = it[id]
                defaultViewModel?.add(key)
                EventBus.getDefault().post(EBSearch("history", key))
                Util_System_Keyboard.hideSoftKeyboardByIBinder(
                    context,
                    mBinding.historyFlex.windowToken
                )
            })
        }
    }

}