package com.gh.gamecenter.home.amway

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.gh.common.util.DialogUtils
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.entity.AmwayCommentEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class HomeAmwayAdapter(
    context: Context,
    var dataList: List<AmwayCommentEntity>,
    private val mItemClick: (View, position: Int, AmwayCommentEntity) -> Unit
) : BaseRecyclerAdapter<HomeAmwayItemViewHolder>(context) {

    var mMaxWidth = context.resources.displayMetrics.widthPixels

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = HomeAmwayItemViewHolder(parent.toBinding())

    override fun getItemCount() = dataList.size

    override fun onBindViewHolder(holder: HomeAmwayItemViewHolder, position: Int) {
        holder.itemView.setPadding(16F.dip2px(), 0, 0, 12F.dip2px())
        holder.itemView.layoutParams?.width =
            if (position == itemCount - 1) mMaxWidth - 4F.dip2px() else mMaxWidth - 60F.dip2px()

        holder.binding.run {
            val amway = dataList[position]

            holder.bindAmway(amway)

            all.goneIf(position != itemCount - 1) {
                all.setOnClickListener {
                    DirectUtils.directToAmway(mContext, null, "(游戏-专题:安利墙-全部)", "")
                    NewLogUtils.logHomeShareWallButtonClick("卡片末尾")
                }
            }

            sdvUserBadge.goneIf(amway.comment.user.badge == null) {
                ImageUtils.display(sdvUserBadge, amway.comment.user.badge!!.icon)
                sdvUserBadge.setOnClickListener {
                    DialogUtils.showViewBadgeDialog(mContext, amway.comment.user.badge) {
                        DirectUtils.directToBadgeWall(
                            mContext,
                            amway.comment.user.id,
                            amway.comment.user.name,
                            amway.comment.user.icon
                        )
                    }
                }
            }

            userIconContainer.setOnClickListener { mItemClick.invoke(it, position, amway) }
            userName.setOnClickListener { userIconContainer.performClick() }

            amwayBackground.setOnClickListener { mItemClick.invoke(it, position, amway) }
            ratingBlock.setOnClickListener { mItemClick.invoke(it, position, amway) }
        }
    }

    fun checkResetData(update: List<AmwayCommentEntity>) {
        if (dataList != update) {
            dataList = update
            notifyDataSetChanged()
        } else {
            notifyItemRangeChanged(0, itemCount)
        }
    }
}