package com.gh.gamecenter.forum.search

import android.os.Bundle
import android.view.View
import androidx.appcompat.content.res.AppCompatResources
import androidx.constraintlayout.widget.ConstraintLayout
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.FragmentSearchDefaultBinding
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 ForumOrUserSearchDefaultFragment : SearchDefaultFragment() {

    private lateinit var mViewModel: ForumOrUserSearchDefaultViewModel

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

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

        mViewModel.hotContentLiveData.observe(viewLifecycleOwner) {
            mRankList = it
            defaultViewModel?.isExistRankList = mRankList?.isNotEmpty() == true
            updateView()
            initRankViewPager()
        }
    }

    private fun initHotSearch() {
        defaultViewModel?.isExistHotSearch = false
        updateView()
    }

    override fun initView() {
        mBinding = FragmentSearchDefaultBinding.bind(mCachedView)
        mBinding.hotAndDiscoveryTagHeadContainer.root.visibility = View.GONE
        mBinding.hotAndDiscoveryTagFlexContainer.visibility = View.GONE
        if (mEntrance == "论坛首页" || mEntrance == "搜索栏") {
            mBinding.hotHeadContainer.headTitle.text = "热门论坛"
            mViewModel.getForumSearchHotContent()
        } else {
            mBinding.hotHeadContainer.root.visibility = View.GONE
            mBinding.hotList.visibility = View.GONE
        }
        val params = mBinding.historyHeadContainer.root.layoutParams as ConstraintLayout.LayoutParams
        params.topMargin = 0.5f.dip2px()
        mBinding.historyHeadContainer.root.layoutParams = params
        mBinding.historyFlexContainer.setLimitHeight(mFlexMaxHeight)

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

        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()
                    defaultViewModel?.isExistHistory = false
                    updateView()
                })
            }
        }
    }

    override fun updateHistorySearchView(historyList: List<String>?) {
        defaultViewModel?.isExistHistory = historyList?.isNotEmpty() == true
        updateView()
        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
                )
            })
        }
    }
}