package com.gh.gamecenter.gamedetail.fuli.kaifu

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.feature.entity.GameEntity

/**
 * 开服订阅-开服表-游戏订阅
 */
class ServersSubscribedGameListFragment : ListFragment<GameEntity, ServersSubscribedGameListViewModel>(),
    ServersSubscribedGameListAdapter.OnUnsubscribeClickListener {

    private var adapter: ServersSubscribedGameListAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setNavigationTitle(getString(R.string.servers_calendar_subscribed_game_title))
        mCachedView.background = ContextCompat.getDrawable(requireContext(), com.gh.gamecenter.common.R.color.ui_surface)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mListViewModel.error.observe(viewLifecycleOwner) {
            ToastUtils.showToast(getString(R.string.network_error_hint))
        }
    }

    override fun provideListViewModel(): ServersSubscribedGameListViewModel {
        return viewModelProvider()
    }

    override fun provideListAdapter(): ListAdapter<GameEntity> {
        return adapter ?: ServersSubscribedGameListAdapter(
            requireContext()
        ).apply {
            setOnUnsubscribeClickListener(this@ServersSubscribedGameListFragment)
            adapter = this
        }
    }

    override fun onUnsubscribeClick(gameEntity: GameEntity, position: Int) {
        NewLogUtils.logLaunchServerSubscribeCancel(
            gameId = gameEntity.id,
            gameName = gameEntity.name ?: ""
        )
        SensorsBridge.trackLaunchServerSubscribeCancelClick(
            gameId = gameEntity.id,
            gameName = gameEntity.name ?: "",
            gameType = gameEntity.categoryChinese ?: ""
        )

        DialogHelper.showDialog(
            context = requireContext(),
            title = getString(R.string.servers_subscribed_game_unsubscribe_dialog_title),
            content = getString(R.string.servers_subscribed_game_unsubscribe_dialog_content),
            confirmText = getString(R.string.servers_subscribed_game_unsubscribe_dialog_confirm),
            cancelText = getString(R.string.servers_subscribed_game_unsubscribe_dialog_cancel),
            confirmClickCallback = {
                mListViewModel.unsubscribeServer(gameEntity.id)
            }
        )
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration? = null

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mCachedView.background = ContextCompat.getDrawable(requireContext(), com.gh.gamecenter.common.R.color.ui_surface)
    }
}