package com.gh.gamecenter.forum.home.follow.viewholder

import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.common.databinding.RefreshFooterviewBinding

class FollowFooterViewHolder(val binding: RefreshFooterviewBinding) : RecyclerView.ViewHolder(binding.root) {

    val loading: ProgressBar
    val hint: TextView

    init {
        loading = itemView.findViewById(R.id.footerview_loading)
        hint = itemView.findViewById(R.id.footerview_hint)
    }

    fun initItemPadding() {
        itemView.setPadding(0, 0, 0, 0)
    }

    fun initFooterViewHolder(
        isNetworkError: Boolean,
        isOver: Boolean,
        @StringRes loadOverHint: Int
    ) {
        if (isNetworkError) {
            loading.visibility = View.GONE
            hint.setText(R.string.loading_failed_retry)
        } else if (isOver) {
            loading.visibility = View.GONE
            hint.setText(loadOverHint)
        } else {
            loading.visibility = View.VISIBLE
            hint.setText(R.string.loading)
        }
    }

    fun initFooterViewHolder(
        viewModel: ListViewModel<*, *>,
        isLoading: Boolean,
        isNetworkError: Boolean,
        isOver: Boolean
    ) {
        initFooterViewHolder(
            isLoading,
            isNetworkError,
            isOver,
            R.string.load_over_hint
        ) { v: View? -> if (isNetworkError) viewModel.load(LoadType.RETRY) }
    }

    fun bindFooterDefaultEmpty(
        viewModel: ListViewModel<*, *>,
        isLoading: Boolean,
        isNetworkError: Boolean,
        isOver: Boolean
    ) {
        if (isNetworkError) {
            loading.visibility = View.GONE
            hint.visibility = View.VISIBLE
            hint.setText(R.string.loading_failed_retry)
        } else if (isOver) {
            loading.visibility = View.GONE
            hint.visibility = View.VISIBLE
            hint.setText(R.string.load_over_hint)
        } else if (isLoading) {
            loading.visibility = View.VISIBLE
            hint.visibility = View.VISIBLE
            hint.setText(R.string.loading)
        } else {
            loading.visibility = View.GONE
            hint.visibility = View.GONE
        }
        itemView.setOnClickListener { v: View? -> if (isNetworkError) viewModel.load(LoadType.RETRY) }
    }

    @JvmOverloads
    fun initFooterViewHolder(
        isLoading: Boolean,
        isNetworkError: Boolean,
        isOver: Boolean,
        @StringRes loadOverHint: Int = R.string.load_over_hint
    ) {
        BaseActivity.updateStaticView(itemView, ArrayList())
        if (isNetworkError) {
            loading.visibility = View.GONE
            hint.setText(R.string.loading_failed_retry)
        } else if (isOver) {
            loading.visibility = View.GONE
            hint.setText(loadOverHint)
        } else if (isLoading) {
            loading.visibility = View.VISIBLE
            hint.setText(R.string.loading)
        } else {
            loading.visibility = View.GONE
            hint.setText(R.string.loading_more_hint)
        }
    }

    fun initFooterViewHolder(
        isLoading: Boolean,
        mIsNetworkError: Boolean,
        mIsOver: Boolean,
        onClickListener: View.OnClickListener?
    ) {
        initFooterViewHolder(
            isLoading,
            mIsNetworkError,
            mIsOver,
            R.string.load_over_hint,
            onClickListener
        )
    }

    fun initFooterViewHolder(
        isLoading: Boolean,
        mIsNetworkError: Boolean,
        mIsOver: Boolean,
        @StringRes loadOverHint: Int,
        onClickListener: View.OnClickListener?
    ) {
        if (mIsNetworkError) {
            loading.visibility = View.GONE
            hint.setText(R.string.loading_failed_retry)
            itemView.isClickable = true
            itemView.setOnClickListener(onClickListener)
        } else if (mIsOver) {
            loading.visibility = View.GONE
            hint.setText(loadOverHint)
            itemView.isClickable = true
            itemView.setOnClickListener(onClickListener)
        } else if (isLoading) {
            loading.visibility = View.VISIBLE
            hint.setText(R.string.loading)
            itemView.isClickable = false
        } else {
            loading.visibility = View.GONE
            hint.setText(R.string.loading_more_hint)
            itemView.isClickable = false
        }
    }
}