package com.gh.gamecenter.game.columncollection

import android.content.Context
import android.view.ViewGroup
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.view.WrapContentDraweeView
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.viewholder.GameColumnCollectionItemViewHolder
import com.gh.gamecenter.databinding.GameColumnCollectionItemBinding
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.SubjectEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class GameColumnCollectionAdapter(
    context: Context,
    private var mSubjectEntity: SubjectEntity,
    private var mClickClosure: (position: Int, gameEntity: GameEntity) -> Unit
) : BaseRecyclerAdapter<GameColumnCollectionItemViewHolder>(context) {

    var columnCollectionHeight = 0

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GameColumnCollectionItemViewHolder {
        val view = mLayoutInflater.inflate(R.layout.game_column_collection_item, parent, false)
        return GameColumnCollectionItemViewHolder(GameColumnCollectionItemBinding.bind(view))
    }

    override fun getItemCount(): Int {
        return mSubjectEntity.data!!.size
    }

    override fun onBindViewHolder(holder: GameColumnCollectionItemViewHolder, position: Int) {

        if (columnCollectionHeight == 0) {
            columnCollectionHeight = mContext.resources.getDimension(R.dimen.home_column_collection_height).toInt()
        }

        val data = mSubjectEntity.data!![position]
        ImageUtils.display(holder.binding.columnCollectionImage, data.image)
        holder.binding.columnCollectionImage.loadingCallback = object : WrapContentDraweeView.LoadingCallback {
            override fun loaded() {
                holder.binding.columnCollectionImage.layoutParams =
                    holder.binding.columnCollectionImage.layoutParams.apply {
                        height = columnCollectionHeight
                        width = (holder.binding.columnCollectionImage.aspectRatio * columnCollectionHeight).toInt()
                    }
            }
        }

        holder.binding.columnCollectionImage.setOnClickListener {
            mClickClosure(position, data)
        }
    }

    fun checkResetData(subjectEntity: SubjectEntity) {
        if (subjectEntity != mSubjectEntity) mSubjectEntity = subjectEntity
    }
}