package com.adealink.weparty.image.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewParent
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.media3.common.util.UnstableApi
import androidx.media3.ui.TimeBar
import com.adealink.frame.log.Log
import com.adealink.frame.util.DisplayUtil
import com.adealink.weparty.image.databinding.LayoutVideoTimeBarBinding
import java.util.concurrent.CopyOnWriteArraySet

@UnstableApi
class VideoTimeBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs), TimeBar {

    private val binding = LayoutVideoTimeBarBinding.inflate(LayoutInflater.from(context), this)

    private val listeners: CopyOnWriteArraySet<TimeBar.OnScrubListener> = CopyOnWriteArraySet()
    private var position: Long = 0
    private var bufferedPosition: Long = 0
    private var duration: Long = 0

    init {
        binding.seekBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                if (fromUser) {

                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
//                seekBar?.progress?.let {
//                    startScrubbing(it.toLong())
//                }
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                seekBar?.progress?.let {
                    stopScrubbing(it.toLong())
                }
            }
        })
    }

    private fun stopScrubbing(position: Long) {
        val parent: ViewParent? = parent
        parent?.requestDisallowInterceptTouchEvent(false)
        invalidate()
        for (listener: TimeBar.OnScrubListener in listeners) {
            listener.onScrubStop(this, position, false)
        }
    }

    override fun addListener(listener: TimeBar.OnScrubListener) {
        listeners.add(listener)
    }

    override fun removeListener(listener: TimeBar.OnScrubListener) {
        listeners.remove(listener)
    }

    override fun setKeyTimeIncrement(time: Long) {
        //Ntd.
    }

    override fun setKeyCountIncrement(count: Int) {
        //Ntd.
    }

    override fun setPosition(position: Long) {
        Log.d(TAG, "setPosition:$position")
        if (this.position == position) {
            return
        }
        this.position = position
        update()
    }

    override fun setBufferedPosition(bufferedPosition: Long) {
        Log.d(TAG, "setBufferedPosition:$bufferedPosition")
        if (this.bufferedPosition == bufferedPosition) {
            return
        }
        this.bufferedPosition = bufferedPosition
        update()
    }

    override fun setDuration(duration: Long) {
        Log.d(TAG, "setDuration:$duration")
        if (this.duration == duration) {
            return
        }
        this.duration = duration
        update()
    }

    override fun getPreferredUpdateDelay(): Long {
        val timeBarWidthDp: Int = DisplayUtil.px2dp(binding.seekBar.width.toFloat())
        return if ((timeBarWidthDp == 0) || (duration == 0L) || (duration == androidx.media3.common.C.TIME_UNSET)) {
            Long.Companion.MAX_VALUE
        } else {
            duration / timeBarWidthDp
        }
    }

    override fun setAdGroupTimesMs(adGroupTimesMs: LongArray?, playedAdGroups: BooleanArray?, adGroupCount: Int) {
        //Ntd.
    }

    private fun update() {
        binding.seekBar.max = duration.toInt()
        binding.seekBar.progress = position.toInt()
        binding.seekBar.secondaryProgress = bufferedPosition.toInt()
    }

    companion object {
        const val TAG = "VideoTimeBar"
    }
}