package com.gh.gamecenter.gamecollection.choose

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.constant.Config
import com.gh.gamecenter.BuildConfig
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.entity.SuggestType
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.databinding.FragmentNewSearchGameBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.help.HelpAndFeedbackBridge
import com.gh.gamecenter.qa.editor.GameAdapter
import com.gh.gamecenter.qa.editor.GameFragment
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Observable

class NewAddSearchGameFragment : GameFragment() {
    private val binding by lazy { FragmentNewSearchGameBinding.inflate(layoutInflater, null, false) }
    private val chooseGamesViewModel: ChooseGamesViewModel by lazy { viewModelProvider(ChooseGamesViewModel.Factory()) }

    override fun getLayoutId(): Int = 0

    override fun getInflatedLayout(): View {
        return binding.root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        mSearchKey = arguments?.getString(EntranceConsts.KEY_SEARCHKEY, "") ?: ""
        super.onCreate(savedInstanceState)
        chooseGamesViewModel.deleteOrAddGameLiveData.observe(this) {
            mAdapter?.entityList?.forEachIndexed { index, gameEntity ->
                if (gameEntity.id == it.id) {
                    mAdapter?.notifyItemChanged(index)
                }
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        noneText?.text = "没有找到相关游戏，换个搜索词试试？"

        binding.reuseNoneData.reuseResetLoadTv.setOnClickListener {
            HelpAndFeedbackBridge.startSuggestionActivity(requireContext(), SuggestType.UPDATE, "【游戏单添加游戏】")
        }
    }

    override fun provideDataObservable(page: Int): Observable<MutableList<GameEntity>>? {
        return RetrofitManager
            .getInstance().api
            .getSearchGame(Config.API_HOST + "games:search?keyword=" + mSearchKey + "&view=digest" + "&from=game_list" + "&page=" + page + "&channel=" + HaloApp.getInstance().channel + "&version=" + BuildConfig.VERSION_NAME)
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration? {
        return null
    }

    override fun provideListAdapter(): GameAdapter? {
        if (mAdapter == null) {
            mAdapter = AddSearchAndPlayedGameAdapter(requireContext(), chooseGamesViewModel)
        }
        return mAdapter
    }

    override fun isAutoShowKeyboard(): Boolean = false

    override fun shouldLoadMore(): Boolean = true

    override fun isAutoSearch(): Boolean = false

    override fun isAutomaticLoad(): Boolean = true

    fun updateSearchKey(searchKey: String) {
        mSearchKey = searchKey
        if (mLastSearchKey != mSearchKey) {
            mLastSearchKey = mSearchKey
            clearPage()
            onLoadRefresh()
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mAdapter?.let { it.notifyItemRangeChanged(0, it.itemCount) }
        binding.listRv.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_background.toColor(requireContext()))
    }
}