package com.gh.gamecenter.gamecollection.choose

import android.os.Bundle
import android.view.View
import androidx.core.widget.doOnTextChanged
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.constant.Config
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.BuildConfig
import com.gh.gamecenter.R
import com.gh.gamecenter.SuggestionActivity
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.databinding.FragmentSearchGameBinding
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.qa.editor.GameAdapter
import com.gh.gamecenter.qa.editor.GameFragment
import com.gh.gamecenter.retrofit.RetrofitManager
import com.gh.gamecenter.suggest.SuggestType
import com.halo.assistant.HaloApp
import io.reactivex.Observable

class AddSearchGameFragment : GameFragment() {

    private lateinit var mBinding: FragmentSearchGameBinding
    private lateinit var mChooseGamesViewModel: ChooseGamesViewModel

    override fun getLayoutId(): Int = 0

    override fun getInflatedLayout(): View {
        return FragmentSearchGameBinding.inflate(layoutInflater, null, false).apply {
            mBinding = this
        }.root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mChooseGamesViewModel = viewModelProvider(ChooseGamesViewModel.Factory())
        mChooseGamesViewModel.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)
        setNavigationTitle("添加游戏")
        noneText.text = "没有找到相关游戏，换个搜索词试试？"
        searchEt.doOnTextChanged { text, _, _, _ ->
            if (text.isNullOrEmpty()) {
                mBinding.promptTv.visibility = View.VISIBLE
            }
        }

        mBinding.reuseNoneData.reuseResetLoadTv.setOnClickListener {
            SuggestionActivity.startSuggestionActivity(requireContext(), SuggestType.gameCollect, "【游戏单添加游戏】")
        }
    }

    override fun provideDataObservable(page: Int): Observable<MutableList<GameEntity>>? {
        return RetrofitManager
            .getInstance().api
            .getSearchGame(Config.API_HOST + "games:search?keyword=" + searchEt.text + "&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(), mChooseGamesViewModel)
        }
        return mAdapter
    }

    override fun onLoadDone() {
        super.onLoadDone()
        mBinding.promptTv.visibility = View.GONE
    }

    override fun onLoadError() {
        super.onLoadError()
        mBinding.promptTv.visibility = View.GONE
    }

    override fun onLoadEmpty() {
        super.onLoadEmpty()
        mBinding.promptTv.visibility = View.GONE
    }

    override fun isAutoShowKeyboard(): Boolean = false

    override fun shouldLoadMore(): Boolean = true

    override fun isAutoSearch(): Boolean = false

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mAdapter?.let { it.notifyItemRangeChanged(0, it.itemCount) }
        if (::mBinding.isInitialized) {
            mBinding.searchContainer.setBackgroundColor(R.color.background_white.toColor(requireContext()))
            mBinding.listRv.setBackgroundColor(R.color.background.toColor(requireContext()))
            mBinding.divider.setBackgroundColor(R.color.divider.toColor(requireContext()))
            mBinding.promptTv.setBackgroundColor(R.color.background_white.toColor(requireContext()))
//            mBinding.reuseNoneData.reuseResetLoadTv.background = R.drawable.game_detail_btn_receive.toDrawable(requireContext())
            mBinding.searchInput.background =
                R.drawable.community_editor_insert_search_background.toDrawable(requireContext())
            mBinding.searchInput.setTextColor(R.color.text_title.toColor(requireContext()))
            mBinding.searchButton.setTextColor(R.color.theme_font.toColor(requireContext()))
            mBinding.promptTv.setTextColor(R.color.text_body.toColor(requireContext()))
//            mBinding.reuseNoneData.reuseNoneDataTv.setTextColor(R.color.text_body.toColor(requireContext()))
        }
    }
}