package com.gh.gamecenter.search.viewholder

import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.databinding.RefreshFooterviewBinding
import com.gh.gamecenter.common.utils.goneIf

class SearchGameListFooterViewHolder(
    val binding: RefreshFooterviewBinding,
    private val listener: FooterListener
) : ViewHolder(binding.root) {

    private var _loadStatus = LoadStatus.INIT

    fun bind(loadStatus: LoadStatus) {
        if (_loadStatus == loadStatus) return
        _loadStatus = loadStatus
        when (loadStatus) {
            LoadStatus.LIST_LOADING -> {
                binding.footerviewLoading.goneIf(false)
                binding.footerviewHint.goneIf(false)
                binding.footerviewHint.setText(R.string.loading)
            }

            LoadStatus.LIST_FAILED -> {
                binding.footerviewLoading.goneIf(true)
                binding.footerviewHint.goneIf(false)
                binding.footerviewHint.setText(R.string.loading_failed_retry)
            }

            LoadStatus.LIST_OVER -> {
                binding.footerviewLoading.goneIf(true)
                binding.footerviewHint.goneIf(false)
                binding.footerviewHint.setText(R.string.load_over_hint)
            }

            LoadStatus.LIST_LOADED, LoadStatus.INIT_LOADED -> listener.loadMore()

            else -> Unit
        }
        itemView.setOnClickListener {
            when (loadStatus) {
                LoadStatus.LIST_FAILED -> listener.retry()
                LoadStatus.LIST_OVER -> listener.toTop()

                else -> Unit
            }
        }
    }

    interface FooterListener {

        fun loadMore()

        fun retry()

        fun toTop()
    }
}