package com.gh.gamecenter.personalhome.background

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.visibleIf
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.AvatarBackgroundItemBinding
import com.gh.gamecenter.feature.entity.BackgroundImageEntity
import com.gh.gamecenter.login.user.UserManager
import com.lightgame.adapter.BaseRecyclerAdapter
import java.util.ArrayList

class PersonalityBackgroundAdapter(context: Context, val mViewModel: PersonalityBackgroundViewModel) :
    BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    var mEntityList: ArrayList<BackgroundImageEntity> = arrayListOf()

    fun setListData(datas: ArrayList<BackgroundImageEntity>) {
        mEntityList.addAll(datas)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return PendantBackgroundViewHolder(
            AvatarBackgroundItemBinding.bind(
                mLayoutInflater.inflate(
                    R.layout.avatar_background_item,
                    parent,
                    false
                )
            )
        )
    }

    override fun getItemCount(): Int = mEntityList.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is PendantBackgroundViewHolder) {
            val entity = mEntityList[position]
            ImageUtils.display(holder.binding.imageSdv, entity.url)
            holder.binding.nameTv.text = entity.name
            holder.binding.checkBorderView.visibleIf(UserManager.getInstance().userInfoEntity?.background?.id == entity.id)
            holder.binding.checkIv.visibleIf(UserManager.getInstance().userInfoEntity?.background?.id == entity.id)
            holder.itemView.setOnClickListener {
                mViewModel.downLoadImage(mContext, entity)
            }
        }
    }

    class PendantBackgroundViewHolder(val binding: AvatarBackgroundItemBinding) :
        BaseRecyclerViewHolder<String>(binding.root)
}