package com.ux.video.file.filerecovery.video

import android.content.Context
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.widget.ImageView
import android.widget.SeekBar
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import com.ux.video.file.filerecovery.utils.Common
import java.io.File

class PlayMediaManager(var context: Context, var mediaFile: File, var playView: PlayerView? = null, var seekBar: SeekBar, var playBtn: ImageView, var onUpdateProgress:(currentStr: String, totalStr: String)-> Unit) {

    private lateinit var player: ExoPlayer
    private val updateHandler = Handler(Looper.getMainLooper())

    init {
        initPlayer()
        playBtn.setOnClickListener {
            if (player.playbackState == Player.STATE_ENDED) {
                player.seekTo(0)
            }
            if (!player.isPlaying) {
                player.play()
                it.isSelected = true
            } else {
                player.pause()
                it.isSelected = false
            }
        }

        seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(
                seekBar: SeekBar?,
                progress: Int,
                fromUser: Boolean
            ) {
                if (fromUser) {
                    val newPosition = progress * player.duration / 100
                    player.seekTo(newPosition)
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {

            }

        })

    }
    private fun initPlayer() {
        player = ExoPlayer.Builder(context).build()
        playView?.player = player
        val mediaItem = MediaItem.fromUri(Uri.fromFile(mediaFile))
        player.addListener(object : Player.Listener {
            override fun onPlaybackStateChanged(playbackState: Int) {
                super.onPlaybackStateChanged(playbackState)
                when (playbackState) {
                    Player.STATE_IDLE -> {

                    }

                    Player.STATE_BUFFERING -> {

                    }

                    Player.STATE_READY -> {

                    }

                    Player.STATE_ENDED -> {
                        playBtn.isSelected = false
//                        onPlayEnd()
                    }
                }

            }
        })
        player.setMediaItem(mediaItem)
        player.prepare()

        startProgressUpdater()
    }

    private fun startProgressUpdater() {
        updateHandler.post(object : Runnable {
            override fun run() {
                if (player.isPlaying || player.isLoading) {
                    val pos = player.currentPosition
                    val dur = player.duration.takeIf { it > 0 } ?: 1L
                    val progress = (pos * 100 / dur).toInt()
                    onUpdateProgress(Common.formatDuration(pos),Common.formatDuration(dur))
                    seekBar.progress = progress
//
//                    binding.textTimeCurrent.text = Common.formatDuration(pos)
//                    binding.textTimeTotal.text = Common.formatDuration(dur)
                }
                updateHandler.postDelayed(this, 500)
            }
        })
    }

    fun stopPlayAudio(){
        player.stop()

    }
}