package com.audio.record.screen.test.activity

import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.OptIn
import androidx.core.view.isVisible
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.DefaultTimeBar
import com.audio.record.screen.test.R
import com.audio.record.screen.test.base.BaseActivity
import com.audio.record.screen.test.databinding.ActivityPlayBinding
import com.audio.record.screen.test.tool.Common


/**
 * 视频播放页面
 */
class PlayActivity : BaseActivity<ActivityPlayBinding>() {

    companion object {
        val KEY_URI = "uri_key"
        val KEY_name = "name"
    }

    private var exoPlayer: ExoPlayer? = null


    private var mUri: Uri? = null

    private var displayName: String? = null
    private lateinit var timeBar:DefaultTimeBar

    private lateinit var btnPlay:ImageButton
    override fun initBinding(): ActivityPlayBinding = ActivityPlayBinding.inflate(layoutInflater)
    override fun getFullColor(): Boolean? = null
    override fun onInitPadding(): Boolean = false
    override fun onSetViewBefore() {
    }

    override fun onCreateInit() {
        Common.enableImmersiveMode(this)
        mUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getParcelableExtra(KEY_URI, Uri::class.java)
        } else {
            intent.getParcelableExtra(KEY_URI)
        }

        displayName = intent.getStringExtra(KEY_name)
        initControllerView()
        initPlay()


    }


    private fun initControllerView() {
        val controller = binding.playerView.findViewById<ViewGroup>(R.id.exo_controller)
        controller.findViewById<ImageView>(R.id.back).setOnClickListener {
            finish()
        }
        controller.findViewById<TextView>(R.id.text_name).text = displayName
        btnPlay = controller.findViewById<ImageButton>(R.id.exo_play_pause)
        val layoutProgress = controller.findViewById<RelativeLayout>(R.id.layout_progress)
          timeBar = controller.findViewById<DefaultTimeBar>(R.id.exo_progress)
    }

    @OptIn(UnstableApi::class)
    private fun initPlay() {
        btnPlay.isVisible = false
        exoPlayer = ExoPlayer.Builder(this).build()
        binding.playerView.run {
            player = exoPlayer
            controllerShowTimeoutMs = 3000 // 设置控制器显示时间 3 秒后自动隐藏
            controllerAutoShow = true // 播放时点击可以自动显示控制器
        }
        exoPlayer?.run {
            val mediaItem = MediaItem.fromUri(mUri!!)
            setMediaItem(mediaItem)
            prepare()
            playWhenReady = true
            addListener(object : Player.Listener {
                override fun onIsPlayingChanged(isPlaying: Boolean) {
                    if(isPlaying){
                        timeBar.isVisible = true
                    }
                }

                override fun onPlaybackStateChanged(playbackState: Int) {
                    if (playbackState == Player.STATE_ENDED) {
                        // 播放完成
                        btnPlay.isVisible = true
                    }

                }
            })
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        binding.playerView.player = null
        releasePlayer()
    }

    private fun releasePlayer() {
        exoPlayer?.run {
            stop()          // 可选：停止播放
            clearMediaItems()
            release()
        }
        exoPlayer = null
    }

}