package com.gh.gamecenter.forum.search

import android.os.Bundle
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
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 mSearchDao: ForumSearchDao
    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 initDao() {
        mSearchDao = ForumSearchDao()
        mHistoryList = mSearchDao.getAll()
    }

    override fun initView() {
        mBinding = FragmentSearchDefaultBinding.bind(mCachedView)
        mBinding.hotTagHeadContainer.root.visibility = View.GONE
        mBinding.hotTagFlexContainer.visibility = View.GONE
        if (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
        defaultViewModel?.isExistHistory = mHistoryList?.isNotEmpty() == true
        updateView()
        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
            )
        })

        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()
                })
            }
        }
    }
}