package com.gh.gamecenter.home.custom.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.gh.common.util.DialogUtils
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.gh.gamecenter.home.custom.eventlistener.OtherItemEventHelper
import com.gh.gamecenter.home.custom.viewholder.CustomHomeAmwayItemViewHolder

class CustomHomeAmwayAdapter(
    context: Context,
    private val childEventHelper: OtherItemEventHelper,
    private val mItemClick: (View, position: Int, AmwayCommentEntity) -> Unit
) : CustomBaseChildAdapter<AmwayCommentEntity, CustomHomeAmwayItemViewHolder>(context) {

    private val  mMaxWidth by lazy {
        context.resources.displayMetrics.widthPixels
    }

    override fun getKey(t: AmwayCommentEntity): String {
        return t.id
    }

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

    override fun onBindViewHolder(holder: CustomHomeAmwayItemViewHolder, 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 {
                    NewLogUtils.logHomeShareWallButtonClick("卡片末尾")
                    childEventHelper.navigateToAmway()
                }
            }

            sdvUserBadge.goneIf(amway.comment.user.badge == null) {
                ImageUtils.display(sdvUserBadge, amway.comment.user.badge!!.icon)
                sdvUserBadge.setOnClickListener {
                    DialogUtils.showViewBadgeDialog(context, amway.comment.user.badge) {
                        childEventHelper.navigateToBadgeWall(amway.comment)
                    }
                }
            }

            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) }
        }
    }
}