package com.halo.assistant.fragment.user.avatar

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.ItemDefaultAvatarBinding
import com.gh.gamecenter.entity.DefaultAvatar
import com.lightgame.adapter.BaseRecyclerAdapter

class DefaultAvatarAdapter(
    context: Context,
    private val mViewModel: ChooseDefaultAvatarViewModel,
    val callback: (DefaultAvatar) -> Unit
) : BaseRecyclerAdapter<DefaultAvatarAdapter.DefaultAvatarViewHolder>(context) {

    private var mDataList = ArrayList<DefaultAvatar>()

    fun submitList(updateData: List<DefaultAvatar>?) {
        mDataList.clear()
        if (updateData != null) {
            mDataList.addAll(updateData)
        }
        notifyItemRangeChanged(0, mDataList.size)
    }

    override fun getItemCount(): Int = mDataList.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DefaultAvatarViewHolder {
        return DefaultAvatarViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(holder: DefaultAvatarViewHolder, position: Int) {
        val entity = mDataList[position]
        ImageUtils.display(holder.binding.iconIv, entity.url)
        holder.binding.selectedIv.isVisible = mViewModel.defaultAvatar == entity
        holder.binding.iconBorderView.run {
            val drawable = GradientDrawable().apply {
                setColor(R.color.transparent.toColor())
                shape = GradientDrawable.OVAL
                setStroke(0.5F.dip2px(), Color.parseColor("#1A000000"))
            }
            background = drawable
        }
        holder.binding.root.setOnClickListener {
            callback.invoke(entity)
        }
    }

    class DefaultAvatarViewHolder(val binding: ItemDefaultAvatarBinding) :
        BaseRecyclerViewHolder<DefaultAvatar>(binding.root)
}