package com.gh.gamecenter.video.poster.video

import android.content.Context
import android.net.Uri
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
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.R
import com.gh.gamecenter.databinding.VideoPosterPreviewItemBinding
import com.lightgame.adapter.BaseRecyclerAdapter

class VideoPosterReviewAdapter(
    context: Context,
    val viewModel: VideoPosterViewModel
) : BaseRecyclerAdapter<VideoPosterReviewItemViewHolder>(context) {

    private var mListData: MutableList<VideoPosterViewModel.VideoPreview> = ArrayList()

    private val mSelectMap = HashMap<Int, Boolean>()

    init {
        mSelectMap[0] = true // default select
    }

    fun submitList(listData: List<VideoPosterViewModel.VideoPreview>) {
        mListData = listData.toMutableList()
        notifyDataSetChanged()
    }

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

    override fun getItemCount(): Int {
        return mListData.size
    }

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

        val videoPreview = mListData[position]
        val thumbs = videoPreview.thumbs
        if (thumbs != null) {
            holder.binding.preview.setImageBitmap(thumbs)
        } else if (videoPreview.thumbsUrl != null) {
            ImageUtils.picasso
                .load(Uri.parse(videoPreview.thumbsUrl))
                .into(holder.binding.preview)
        }
        holder.binding.preview.setOnClickListener {
            viewModel.previewLiveData.postValue(videoPreview)

            mSelectMap[position] = true
            for (entry in mSelectMap) {
                if (entry.value && entry.key != position) {
                    mSelectMap[entry.key] = false
                    notifyItemChanged(entry.key)
                }
            }
            notifyItemChanged(position)
        }

        holder.binding.previewContainer.radius = if (mSelectMap[position] == true) {
            3F.dip2px().toFloat()
        } else if (position == 0 || position == itemCount - 1) {
            4F.dip2px().toFloat()
        } else {
            0F
        }
        holder.binding.previewBorder.visibility = if (mSelectMap[position] == true) {
            View.VISIBLE
        } else View.GONE

        val previewContainerLp = holder.binding.previewContainer.layoutParams as ConstraintLayout.LayoutParams
        if (position == 0) {
            previewContainerLp.rightMargin = (-4F).dip2px()
            previewContainerLp.leftMargin = 0
        } else if (position == itemCount - 1) {
            previewContainerLp.leftMargin = (-4F).dip2px()
            previewContainerLp.rightMargin = 0
        } else {
            previewContainerLp.rightMargin = 0
            previewContainerLp.leftMargin = 0
        }
        holder.binding.previewContainer.layoutParams = previewContainerLp
    }
}

class VideoPosterReviewItemViewHolder(val binding: VideoPosterPreviewItemBinding) :
    BaseRecyclerViewHolder<Any>(binding.root) {

}