package com.gh.gamecenter.home.skip

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.common.util.*
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.adapter.viewholder.GameViewHolder
import com.gh.gamecenter.core.utils.EmptyCallback
import com.gh.gamecenter.core.utils.StringUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.feature.databinding.GameItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.game.GameItemViewHolder
import com.lightgame.adapter.BaseRecyclerAdapter

class PackageSkipAdapter(
    val context: Context,
    val games: ArrayList<GameEntity>,
    private val itemClickCallback: () -> Unit
) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        return if (viewType == ItemViewType.ITEM_BODY) {
            GameItemViewHolder(GameItemBinding.bind(mLayoutInflater.inflate(com.gh.gamecenter.feature.R.layout.game_item, parent, false)))
        } else {
            FooterViewHolder(mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false))
        }

    }

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

    override fun getItemCount(): Int = games.size + 1

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GameItemViewHolder) {
            val gameEntity = games[position]
            val subjectData = gameEntity.subjectData
            holder.bindGameItem(gameEntity, subjectData?.briefStyle ?: "")
            holder.initServerType(gameEntity)

            if (games.size == 1) {
                holder.itemView.background =
                    ContextCompat.getDrawable(context, com.gh.gamecenter.common.R.drawable.background_shape_white_radius_8)
            } else if (games.size == 2) {
                if (position == 0) {
                    holder.itemView.setPadding(16f.dip2px(), 16f.dip2px(), 16f.dip2px(), 8f.dip2px())
                    holder.itemView.background =
                        ContextCompat.getDrawable(context, com.gh.gamecenter.common.R.drawable.background_shape_white_radius_8_top_only)
                } else {
                    holder.itemView.setPadding(16f.dip2px(), 8f.dip2px(), 16f.dip2px(), 16f.dip2px())
                    holder.itemView.background =
                        ContextCompat.getDrawable(context, com.gh.gamecenter.common.R.drawable.background_shape_white_radius_8_bottom_only)
                }
            } else {
                when (position) {
                    0 -> {
                        holder.itemView.setPadding(16f.dip2px(), 16f.dip2px(), 16f.dip2px(), 8f.dip2px())
                        holder.itemView.background =
                            ContextCompat.getDrawable(context, com.gh.gamecenter.common.R.drawable.background_shape_white_radius_8_top_only)
                    }
                    itemCount - 2 -> {
                        holder.itemView.setPadding(16f.dip2px(), 8f.dip2px(), 16f.dip2px(), 16f.dip2px())
                        holder.itemView.background =
                            ContextCompat.getDrawable(context, com.gh.gamecenter.common.R.drawable.background_shape_white_radius_8_bottom_only)
                    }
                    else -> {
                        holder.itemView.setPadding(16f.dip2px(), 8f.dip2px(), 16f.dip2px(), 8f.dip2px())
                        holder.itemView.background =
                            ContextCompat.getDrawable(context, com.gh.gamecenter.common.R.drawable.background_shape_white)
                    }
                }
            }

            DownloadItemUtils.setOnClickListener(mContext, holder.binding.downloadBtn, gameEntity, position,
                this,
                StringUtils.buildString("应用跳转:", subjectData?.name, "-列表[", (position + 1).toString(), "])"),
                location = StringUtils.buildString("应用跳转-", subjectData?.name, ":", gameEntity.name),
                traceEvent = null
            ) { LogUtils.uploadPackageSkip("external_show", "下载游戏", gameEntity.id, gameEntity.name) }
            DownloadItemUtils.updateItem(
                mContext,
                gameEntity,
                GameViewHolder(holder.binding),
                subjectData?.briefStyle
            )
            holder.itemView.setOnClickListener {
                LogUtils.uploadPackageSkip("external_show", "点击游戏", gameEntity.id, gameEntity.name)
                GameDetailActivity.startGameDetailActivity(context, gameEntity, "应用跳转")
                itemClickCallback.invoke()
            }
        } else if (holder is FooterViewHolder) {
            holder.initFooterViewHolder(false, false, true)
            holder.hint.setTextColor(ContextCompat.getColor(context, com.gh.gamecenter.common.R.color.text_tertiary))
            holder.hint.textSize = 12f
        }
    }
}