package com.gh.gamecenter.amway.search

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
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 mSearchDao: AmwaySearchDao
    private lateinit var mViewModel: AmwaySearchViewModel

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

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

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

        mViewModel = viewModelProviderFromParent()
        mViewModel.playedGames.observeNonNull(this) {
            defaultViewModel?.isExistHotSearch = it.isNotEmpty()
            updateView()
            mBinding.hotList.run {
                layoutManager = LinearLayoutManager(context)
                adapter = AmwaySearchAdapter(context, mViewModel).apply { setData(it) }
            }

            updateNoPlayedGameHint()
        }
    }

    override fun initDao() {
        mSearchDao = AmwaySearchDao()
        mHistoryList = mSearchDao.getAll()
    }

    override fun initView() {
        mBinding = mAmwayBinding.searchContent
        defaultViewModel?.isExistHistory = mHistoryList?.isNotEmpty() == true
        updateView()
        mBinding.hotHeadContainer.headTitle.text = "最近玩过"
        mBinding.historyFlexContainer.setLimitHeight(mFlexMaxHeight)
        createFlexContent(mBinding.historyFlex, mHistoryList, clickListener = {
            val key = mHistoryList!![it]
            mSearchDao.add(key)
            EventBus.getDefault().post(EBSearch("history", key))
            Util_System_Keyboard.hideSoftKeyboardByIBinder(context, mBinding.historyFlex.windowToken)
        })

        initHistoryHeadView()
    }

    private fun initHistoryHeadView() {
        mBinding.historyHeadContainer.run {
            headTitle.text = getString(R.string.search_history)
            headTitle.textSize = 16F
            headActionTv.text = "清空"
            headActionTv.setTextColor(R.color.text_subtitleDesc.toColor(requireContext()))
            headActionTv.setDrawableStart(VectorDrawableCompat.create(
                resources,
                R.drawable.search_history_delete,
                null
            ))
            headActionTv.setOnClickListener {
                DialogHelper.showCenterWarningDialog(requireContext(), "清空记录", "确定清空历史搜索记录？", confirmClickCallback = {
                    mSearchDao.deleteAll()
                    defaultViewModel?.isExistHistory = false
                    updateView()
                    updateNoPlayedGameHint()
                })
            }
        }
    }

    private fun updateNoPlayedGameHint() {
        if (mSearchDao.getAll() != null && mSearchDao.getAll()?.size != 0) {
            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
        }
    }

}