package com.gh.gamecenter.qa.questions.invite

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.callback.OnListClickListener
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.databinding.QuestioninviteHeaderItemBinding
import com.gh.gamecenter.databinding.QuestionsinviteItemBinding
import com.gh.gamecenter.entity.MeEntity
import com.gh.gamecenter.qa.entity.InviteEntity
import java.util.*

open class QuestionsInviteAdapter(
    context: Context,
    private val mListClickListener: OnListClickListener,
    private val mEntrance: String,
    private val mPath: String
) : ListAdapter<InviteEntity>(context) {

    override fun setListData(listData: List<InviteEntity>?) {
        if (listData != null && listData.isNotEmpty()) {
            mEntityList = ArrayList(listData)
            notifyDataSetChanged()
            // notifyItemRangeInserted(mEntityList.size() - listData.size() + TOP_ITEM_COUNT, listData.size());
        }
    }

    override fun getItemViewType(position: Int): Int {
        return when {
            position == itemCount - 1 -> ItemViewType.ITEM_FOOTER
            mEntityList[position].isHeaderItem!! -> ItemViewType.ITEM_HEADER
            else -> ItemViewType.ITEM_BODY
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View
        when (viewType) {
            ItemViewType.ITEM_FOOTER -> {
                view = mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false)
                return FooterViewHolder(view, mListClickListener)
            }

            ItemViewType.ITEM_BODY -> {
                view = mLayoutInflater.inflate(R.layout.questionsinvite_item, parent, false)
                return QuestionsInviteViewHolder(QuestionsinviteItemBinding.bind(view), mListClickListener)
            }

            ItemViewType.ITEM_HEADER -> {
                return HeaderHolder(QuestioninviteHeaderItemBinding.inflate(mLayoutInflater, parent, false))
            }

            else -> {
                view = mLayoutInflater.inflate(R.layout.questionsinvite_item, parent, false)
                return QuestionsInviteViewHolder(QuestionsinviteItemBinding.bind(view), mListClickListener)
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            ItemViewType.ITEM_FOOTER -> {
                val footerViewHolder = holder as FooterViewHolder
                footerViewHolder.initFooterViewHolder(
                    mIsLoading,
                    mIsNetworkError,
                    mIsOver,
                    R.string.invite_more_players
                )
            }
            ItemViewType.ITEM_BODY -> {
                val entity = mEntityList[position]
                (holder as QuestionsInviteViewHolder).initQuestionsInviteViewHolder(mContext, entity, mPath)
                holder.itemView.setOnClickListener {
                    DirectUtils.directToHomeActivity(
                        mContext,
                        entity.id,
                        mEntrance,
                        mPath
                    )
                }
            }
            ItemViewType.ITEM_HEADER -> {
                val headerEntity = mEntityList[position]
                (holder as HeaderHolder).binding.questionInviteHeaderTv.text = headerEntity.headerName
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList != null && mEntityList.size != 0) mEntityList.size + FOOTER_ITEM_COUNT else 0
    }

    fun inviteSuccess(id: String) {
        for (inviteEntity in mEntityList) {
            if (id == inviteEntity.id) {
                var me = inviteEntity.me
                if (me == null) me = MeEntity()
                me.isUserInvite = true
                inviteEntity.me = me
                notifyDataSetChanged()
                break
            }
        }
    }

    class HeaderHolder(var binding: QuestioninviteHeaderItemBinding) : RecyclerView.ViewHolder(binding.root)
}
