package com.live.mylivewallpaper.help

import android.content.Context
import android.net.Uri
import android.service.wallpaper.WallpaperService
import android.view.SurfaceHolder
import androidx.annotation.OptIn
import androidx.media3.common.C
import androidx.media3.common.MediaItem
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.DefaultDataSourceFactory
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.source.ProgressiveMediaSource
import java.io.File


class LiveWallpaperService : WallpaperService() {


    override fun onCreateEngine(): Engine {
        return VideoWallpaperEngine()
    }

    inner class VideoWallpaperEngine : Engine() {
        private var exoPlayer: ExoPlayer? = null


        @OptIn(UnstableApi::class)
        private fun initExoPlay() {
            exoPlayer = ExoPlayer.Builder(this@LiveWallpaperService).build()
            exoPlayer?.repeatMode = ExoPlayer.REPEAT_MODE_ONE
            update()
        }

        override fun onCreate(surfaceHolder: SurfaceHolder?) {
            super.onCreate(surfaceHolder)
            Common.logMsg("-----Engine-----onCreate")
            initExoPlay()

        }

        @OptIn(UnstableApi::class)
        override fun onSurfaceCreated(holder: SurfaceHolder?) {
            super.onSurfaceCreated(holder)
            val surface = holder?.surface
            if (surface != null) {
                exoPlayer?.setVideoSurface(surface)
                exoPlayer?.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
            }


        }

        override fun onVisibilityChanged(visible: Boolean) {
            super.onVisibilityChanged(visible)
            Common.logMsg("-------Engine---onVisibilityChanged visible=$visible")
            if (visible) {
                update()
                exoPlayer?.play()
            } else {
                exoPlayer?.pause()
            }
        }


        override fun onSurfaceDestroyed(holder: SurfaceHolder?) {
            super.onSurfaceDestroyed(holder)
            Common.logMsg("------Engine----onSurfaceDestroyed---")
            exoPlayer?.release()
        }

        override fun onDestroy() {
            super.onDestroy()
            Common.logMsg("-------Engine---onDestroy---")


        }

        @OptIn(UnstableApi::class)
        override fun onSurfaceChanged(
            holder: SurfaceHolder?,
            format: Int,
            width: Int,
            height: Int
        ) {
            super.onSurfaceChanged(holder, format, width, height)

        }

        @OptIn(UnstableApi::class)
        private fun update() {
            val uri = getVideoUrl(this@LiveWallpaperService)
//            val test = "android.resource://" + packageName + "/" + R.raw.test1
            if (uri != null) {
                val fromUri = MediaItem.fromUri(uri)
                val mediaSource = ProgressiveMediaSource.Factory(
                    DefaultDataSourceFactory(this@LiveWallpaperService, "user-agent")
                ).createMediaSource(fromUri)
                exoPlayer?.setMediaSource(mediaSource)
                exoPlayer?.prepare()
                exoPlayer?.playWhenReady = true
            }
        }


    }


    private fun getVideoUrl(con: Context): Uri? {

        val file = File(Shared.video_path)
        val uri = if (file.isFile && file.exists()) {
            Common.getUriFromFilePath(con, Shared.video_path)
        } else {
            null
        }
        return uri
    }
}