package com.gh.gamecenter.gamecollection.choose

import android.content.Context
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.databinding.GameItemBinding
import com.gh.gamecenter.game.GameItemViewHolder
import com.gh.gamecenter.qa.editor.GameAdapter
import com.lightgame.utils.Utils

class AddSearchAndPlayedGameAdapter(context: Context, val mChooseGamesViewModel: ChooseGamesViewModel) :
    GameAdapter(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == ItemViewType.ITEM_BODY) {
            GameItemViewHolder(GameItemBinding.bind(mLayoutInflater.inflate(R.layout.game_item, parent, false)))
        } else {
            FooterViewHolder(mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false))
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList.isNullOrEmpty()) 0 else mEntityList.size + 1
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) ItemViewType.ITEM_FOOTER else ItemViewType.ITEM_BODY
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GameItemViewHolder) {
            val entity = mEntityList[position]
            val isSelected = mChooseGamesViewModel.chooseGamesLiveData.value?.find { it.id == entity.id } != null
            holder.bindGameItem(entity)
            holder.binding.downloadBtn.text = if (isSelected) "删除" else "添加"
            holder.binding.downloadBtn.background =
                if (isSelected) R.drawable.button_round_gray_light.toDrawable(mContext) else R.drawable.download_button_normal_style.toDrawable(
                    mContext
                )
            holder.binding.downloadBtn.setTextColor(
                if (isSelected) R.color.text_subtitleDesc.toColor(mContext) else R.color.white.toColor(
                    mContext
                )
            )

            holder.binding.downloadBtn.setOnClickListener {
                val chooseGameList = mChooseGamesViewModel.chooseGamesLiveData.value ?: arrayListOf()
                if (isSelected) {
                    chooseGameList.remove(chooseGameList.find { it.id == entity.id })
                    mChooseGamesViewModel.deleteOrAddGameLiveData.postValue(entity)
                    Utils.toast(mContext, "游戏已移除")
                } else {
                    if (chooseGameList.size >= 100) {
                        Utils.toast(mContext, "已添加游戏到达上限")
                        return@setOnClickListener
                    }
                    chooseGameList.add(entity)
                    mChooseGamesViewModel.deleteOrAddGameLiveData.postValue(entity)
                    Utils.toast(mContext, "游戏已添加")
                }
                notifyItemChanged(position)
                mChooseGamesViewModel.chooseGamesLiveData.postValue(chooseGameList)
            }
        } else if (holder is FooterViewHolder) {
            holder.initItemPadding()
            holder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver)
            holder.hint.setTextColor(ContextCompat.getColor(mContext, R.color.text_B3B3B3))
            val lp = holder.itemView.layoutParams as RecyclerView.LayoutParams
            lp.height = DisplayUtils.dip2px(48F)
            lp.width = ViewGroup.LayoutParams.MATCH_PARENT
            holder.itemView.layoutParams = lp
        }
    }
}