package com.gh.gamecenter.amway.search

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.entity.SimpleGameEntity
import com.gh.gamecenter.common.entity.SuggestType
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.utils.viewModelProviderFromParent
import com.gh.gamecenter.databinding.FragmentAmwaySearchBinding
import com.gh.gamecenter.help.HelpAndFeedbackBridge

class AmwaySearchListFragment : ToolbarFragment() {

    private lateinit var mAdapter: AmwaySearchAdapter
    private lateinit var mViewModel: AmwaySearchViewModel

    private val mBinding by lazy { FragmentAmwaySearchBinding.inflate(layoutInflater) }

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

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

        mViewModel = viewModelProviderFromParent()

        mAdapter = AmwaySearchAdapter(requireContext(), mViewModel, "安利墙搜索-搜索列表")

        mBinding.recyclerView.adapter = mAdapter
        mBinding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
        mBinding.noDataContainer.reuseResetLoadTv.text = "求游戏"
        mBinding.noDataContainer.reuseNoneDataTv.text = "没有找到相关游戏"
        mBinding.noDataContainer.reuseNoneDataDescTv.text = "换个搜索词试试~"
        mBinding.noDataContainer.reuseResetLoadTv.visibility = View.VISIBLE
        mBinding.noDataContainer.reuseResetLoadTv.setOnClickListener {
            HelpAndFeedbackBridge.startSuggestionActivity(
                requireContext(),
                SuggestType.UPDATE,
                null,
                null,
                SimpleGameEntity(name = mViewModel.currentSearchKey)
            )
        }

        mBinding.errorContainer.connectionReloadTv.setOnClickListener {
            mViewModel.retrySearch()
        }

        mViewModel.searchGames.observeNonNull(this) {
            mAdapter.setData(it)
        }

        mViewModel.loadStatus.observeNonNull(this) {
            when (it) {
                LoadStatus.INIT_LOADING -> {
                    hideError()
                    hideRvList()
                    showLoading()
                    hideNoDataHint()
                }
                LoadStatus.INIT_LOADED -> {
                    hideError()
                    hideLoading()
                    hideNoDataHint()
                    showRvList()
                }
                LoadStatus.INIT_FAILED -> {
                    hideLoading()
                    hideNoDataHint()
                    showError()
                    hideRvList()
                }
                LoadStatus.INIT_EMPTY -> {
                    showNoDataHint()
                    hideError()
                    hideLoading()
                    hideRvList()
                }
                else -> {
                    // do nothing
                }
            }
        }
    }

    private fun hideRvList() {
        mBinding.recyclerView.visibility = View.GONE
    }

    private fun showRvList() {
        mBinding.recyclerView.visibility = View.VISIBLE
    }

    private fun showLoading() {
        mBinding.loadingContainer.visibility = View.VISIBLE
    }

    private fun hideLoading() {
        mBinding.loadingContainer.visibility = View.GONE
    }

    private fun showNoDataHint() {
        mBinding.noDataContainer.reuseNoneData.visibility = View.VISIBLE
    }

    private fun hideNoDataHint() {
        mBinding.noDataContainer.reuseNoneData.visibility = View.GONE
    }

    private fun showError() {
        mBinding.errorContainer.reuseNoConnection.visibility = View.VISIBLE
    }

    private fun hideError() {
        mBinding.errorContainer.reuseNoConnection.visibility = View.GONE
    }

}