package com.gh.gamecenter.game.imageslide

import android.graphics.Color
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.core.iinterface.IOffsetable
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.px2dip
import com.gh.gamecenter.common.view.DrawableView
import com.gh.gamecenter.databinding.GameImageSlideItemBinding
import com.gh.gamecenter.entity.GameEntity

class GameImageSlideViewHolder(val binding: GameImageSlideItemBinding) : BaseRecyclerViewHolder<Any>(binding.root) {

    private var mScrollListener: RecyclerView.OnScrollListener? = null
    private var mLastScrolledPosition = 0

    fun bindImageSlide(
        entity: GameEntity,
        imageClickListener: View.OnClickListener,
        entrance: String,
        offsetable: IOffsetable
    ) {
        val context = binding.root.context

        ImageUtils.display(binding.columnIcon, entity.image)
        binding.columnTitle.text = entity.name
        binding.columnIcon.setOnClickListener(imageClickListener)
        binding.columnList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                updateBackground()
            }
        })
        binding.columnList.isNestedScrollingEnabled = false

        if (!entity.games.isNullOrEmpty()) {
            binding.columnBackground.visibility = View.VISIBLE
            binding.columnList.visibility = View.VISIBLE
            var slideAdapter = binding.columnList.adapter
            if (slideAdapter == null) {
                slideAdapter = ImageSlideAdapter(context, entity, imageClickListener, entrance)
                (binding.columnList.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
                binding.columnList.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                binding.columnList.adapter = slideAdapter
            } else {
                (slideAdapter as ImageSlideAdapter).checkResetData(entity)
                slideAdapter.notifyItemRangeChanged(0, slideAdapter.itemCount)

                // 处理复用时的 recyclerView 偏移问题
                if (mLastScrolledPosition != adapterPosition) {
                    val offset = offsetable.getOffset(adapterPosition)
                    if (offset == 0) {
                        binding.columnList.scrollToPosition(0)
                    } else {
                        binding.columnList.scrollBy(offset, offset)
                    }
                }
            }
        } else {
            binding.columnList.visibility = View.GONE
            binding.columnBackground.visibility = View.GONE
        }

        if (mScrollListener == null) {
            mScrollListener = object : RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)

                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        mLastScrolledPosition = adapterPosition
                        offsetable.updateOffset(adapterPosition, recyclerView.computeHorizontalScrollOffset())
                    }
                }
            }

            binding.columnList.addOnScrollListener(mScrollListener!!)
        }

//        binding.columnList.layoutManager?.scrollToPosition(0)
        updateBackground()
    }

    private fun updateBackground() {
        val columnIconLp = binding.columnIcon.layoutParams
        val offset = binding.columnList.computeHorizontalScrollOffset().toFloat().px2dip() // 计算返回可能出错

        var firstVisiblePosition = 0
        val layoutManager = binding.columnList.layoutManager
        if (layoutManager is LinearLayoutManager) {
            firstVisiblePosition = layoutManager.findFirstVisibleItemPosition()
        }

        val relativeOffset = if (offset > 180 || firstVisiblePosition > 0) {
            180
        } else if (offset < 0) {
            30
        } else {
            if (offset + 30 > 180) {
                180
            } else {
                offset + 30
            }
        }

        if (columnIconLp is ConstraintLayout.LayoutParams) {
            columnIconLp.leftMargin = -(relativeOffset / 4).toFloat().dip2px()
            binding.columnIcon.layoutParams = columnIconLp
        }

        val startAlpha = relativeOffset.toFloat() / 180 * 90
        val startColor = "#" + DrawableView.convertAlphaKey(startAlpha.toInt()) + "0C1926"
        val endColor = "#" + DrawableView.convertAlphaKey(100) + "0C1926"
        binding.columnBackground.background = DrawableView.getGradientDrawable(
            binding.columnBackground.width,
            Color.parseColor(startColor),
            Color.parseColor(endColor)
        )
    }
}