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

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.net.Uri
import android.view.ViewGroup
import com.gh.gamecenter.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.goneIf
import com.gh.gamecenter.databinding.VideoPosterPreviewItemBinding
import com.gh.gamecenter.video.poster.video.VideoPosterViewModel.Companion.SELECTED_IMAGE_POSITION
import com.google.android.material.shape.CornerFamily
import com.google.android.material.shape.ShapeAppearanceModel
import com.lightgame.adapter.BaseRecyclerAdapter

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

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

    private var selectedPosition = 0

    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 builder = ShapeAppearanceModel().toBuilder()
        when (position) {
            0 -> builder.setTopLeftCorner(CornerFamily.ROUNDED, 4F.dip2px().toFloat())
                .setTopRightCorner(CornerFamily.ROUNDED, 0F)
                .setBottomRightCorner(CornerFamily.ROUNDED, 0F)
                .setBottomLeftCorner(CornerFamily.ROUNDED, 4F.dip2px().toFloat())

            itemCount - 1 -> builder.setTopLeftCorner(CornerFamily.ROUNDED, 0F)
                .setTopRightCorner(CornerFamily.ROUNDED, 4F.dip2px().toFloat())
                .setBottomRightCorner(CornerFamily.ROUNDED, 4F.dip2px().toFloat())
                .setBottomLeftCorner(CornerFamily.ROUNDED, 0F)

            else -> builder.setAllCorners(CornerFamily.ROUNDED, 0F)
        }
        holder.binding.preview.shapeAppearanceModel = builder.build()

        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.setPreviewImage(position, mListData[position])
        }
        setSelected(holder, position)

    }

    override fun onBindViewHolder(holder: VideoPosterReviewItemViewHolder, position: Int, payloads: MutableList<Any>) {
        if (payloads.contains(PAYLOADS_IS_SELECTED)) {
            setSelected(holder, position)
        } else {
            super.onBindViewHolder(holder, position, payloads)
        }

    }

    private fun setSelected(holder: VideoPosterReviewItemViewHolder, position: Int) {
        holder.binding.preview.clipToOutline = true
        holder.binding.previewBorderInner.clipToOutline = true
        holder.binding.previewBorderOut.shapeAppearanceModel = ShapeAppearanceModel().toBuilder()
            .setAllCorners(CornerFamily.ROUNDED, 4F.dip2px().toFloat())
            .build()
        holder.binding.previewBorderOut.strokeColor = ColorStateList.valueOf(Color.parseColor("#232323"))
        holder.binding.previewBorderOut.strokeWidth = 6F.dip2px().toFloat()

        holder.binding.previewBorderOut.clipToOutline = true
        holder.binding.previewBorderInner.shapeAppearanceModel = ShapeAppearanceModel().toBuilder()
            .setAllCorners(CornerFamily.ROUNDED, 4F.dip2px().toFloat())
            .build()
        holder.binding.previewBorderInner.strokeColor = ColorStateList.valueOf(Color.parseColor("#2496FF"))
        holder.binding.previewBorderInner.strokeWidth = 4F.dip2px().toFloat()

        holder.binding.gPreviewBorder.goneIf(selectedPosition != position)
        fun getCorner(hasCorner: Boolean) = if (hasCorner) 4F.dip2px().toFloat() else 0F
        val leftCorner = getCorner(position == 0)
        val rightCorner = getCorner(position == itemCount - 1)
        holder.binding.vCover.setCorners(
            leftCorner,
            rightCorner,
            rightCorner,
            leftCorner,
        )
        holder.binding.vCover.setLight(selectedPosition == position)
    }

    fun setPreviewPosition(position: Int) {
        if (selectedPosition != position) {
            val lastPosition = selectedPosition
            selectedPosition = position
            if (lastPosition != SELECTED_IMAGE_POSITION) {
                notifyItemChanged(lastPosition, PAYLOADS_IS_SELECTED)
            }
            if (selectedPosition != SELECTED_IMAGE_POSITION) {
                notifyItemChanged(position, PAYLOADS_IS_SELECTED)
            }
        }
    }

    fun getItem(position: Int) = mListData.getOrNull(position)

    companion object {
        private const val PAYLOADS_IS_SELECTED = "payloads_is_selected"
    }
}

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

}