package com.base.appzxhy.ui.play

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.util.AttributeSet
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.graphics.ColorUtils
import androidx.interpolator.view.animation.FastOutLinearInInterpolator
import com.base.appzxhy.R
import com.base.appzxhy.databinding.LayoutOverlayForyouBinding
import com.base.appzxhy.databinding.LayoutOverlayPlayerBinding
import com.base.appzxhy.ui.play.effect.AnimationEffect
import com.base.appzxhy.utils.KotlinExt.formatSeconds
import com.base.appzxhy.utils.LogEx
import com.base.appzxhy.utils.Utils.dp
import com.bytedance.sdk.shortplay.api.PSSDK
import com.bytedance.sdk.shortplay.api.ShortPlay
import com.bytedance.sdk.shortplay.api.ShortPlayFragment
import com.litao.slider.NiftySlider

/**
 * 自定义播放页面悬浮成
 */
class CustomOverlayView : FrameLayout, PSSDK.IControlView {

    private val TAG = "CustomOverlayView"

    private var layout: Int = 0
    private var niftySlider: NiftySlider? = null
    private var tvPlayNow: TextView? = null
    private var tvPlayTotal: TextView? = null


    var bindItemDataCallBack: ((shortPlay: ShortPlay, index: Int, customOverlayView: CustomOverlayView) -> Unit)? = null

    constructor(context: Context, layout: Int) : super(context) {
        this.layout = layout
        inflate(context, layout, this)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)


    override fun getControlViewType(): PSSDK.ControlViewType {
        return PSSDK.ControlViewType.CUSTOM
    }

    override fun bindItemData(shortPlayFragment: ShortPlayFragment?, shortPlay: ShortPlay, index: Int) {
        when (layout) {
            R.layout.layout_overlay_foryou -> {
                val binding = LayoutOverlayForyouBinding.bind(this)
                binding.tvTitle.text = shortPlay.title
                binding.tvDesc.text = shortPlay.desc
                bindItemDataCallBack?.invoke(shortPlay, index, this)
            }

            R.layout.layout_overlay_player -> {
                val binding = LayoutOverlayPlayerBinding.bind(this)

                niftySlider = binding.niftySlider
                tvPlayNow = binding.tvPlayNow
                tvPlayTotal = binding.tvPlayTotal

                val thumbColor = ColorUtils.compositeColors(ColorUtils.setAlphaComponent(Color.WHITE, 0x55), Color.BLACK)
                val trackColor = ColorUtils.setAlphaComponent(Color.WHITE, 0x33)
                val inactiveColor = ColorUtils.setAlphaComponent(Color.WHITE, 0x11)

                val animEffect = AnimationEffect(binding.niftySlider).apply {
                    srcTrackHeight = 3.dp
                    srcThumbHeight = 6.dp
                    srcThumbWidth = 6.dp
                    srcThumbRadius = 3.dp
                    srcThumbColor = thumbColor
                    srcTrackColor = trackColor
                    srcInactiveTrackColor = inactiveColor

                    targetTrackHeight = 12.dp
                    targetThumbHeight = 16.dp
                    targetThumbWidth = 8.dp
                    targetThumbRadius = 5.dp
                    targetThumbColor = Color.WHITE
                    targetTrackColor = ColorUtils.setAlphaComponent(Color.WHITE, 0xDD)
                    targetInactiveTrackColor = ColorUtils.setAlphaComponent(Color.WHITE, 0x33)

                    animationListener = object : AnimationEffect.OnAnimationChangeListener {
                        override fun onEnd(slider: NiftySlider) {
//                        Toast.makeText(requireContext(), "do something on animation end", Toast.LENGTH_SHORT).show()
                        }
                    }

                    setInterpolator(FastOutLinearInInterpolator())
                }
                binding.niftySlider.apply {
                    effect = animEffect
                    setTrackTintList(ColorStateList.valueOf(trackColor))
                    setTrackInactiveTintList(ColorStateList.valueOf(inactiveColor))
                    setThumbTintList(ColorStateList.valueOf(thumbColor))
                    setThumbShadowColor(Color.BLACK)
                }
                binding.tvTitle.text = "${shortPlay.title} $index/${shortPlay.total}"
                bindItemDataCallBack?.invoke(shortPlay, index, this)
            }

        }
    }

    @SuppressLint("SetTextI18n")
    fun onProgressChange(currentPlayTimeInSeconds: Int, durationInSeconds: Int) {
//        LogEx.logDebug(TAG, "currentPlayTimeInSeconds=$currentPlayTimeInSeconds durationInSeconds=$durationInSeconds")
        if (niftySlider?.valueTo != durationInSeconds.toFloat()) {
            LogEx.logDebug(TAG, "change Progress max")
            niftySlider?.valueFrom = 0f
            niftySlider?.valueTo = durationInSeconds.toFloat()
            niftySlider?.setValue(0f, false)
            tvPlayNow?.text = 0L.formatSeconds() + " / "
            tvPlayTotal?.text = durationInSeconds.toLong().formatSeconds()
        } else {
            niftySlider?.setValue(currentPlayTimeInSeconds.toFloat(), false)
            tvPlayNow?.text = currentPlayTimeInSeconds.toLong().formatSeconds() + " / "
        }
    }

}