package com.swago.room.gift.control

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.opensource.svgaplayer.SVGACallback
import com.opensource.svgaplayer.SVGADrawable
import com.opensource.svgaplayer.SVGAParser
import com.opensource.svgaplayer.SVGAVideoEntity
import com.swago.baseswago.model.live.gift.IMGiftModel
import com.swago.baseswago.util.LogUtil
import com.swago.room.databinding.ViewSvgaBinding
import java.io.File
import java.net.URL

/**
 *@date 2021/12/11 17:04
 *description:
 */
class XSvgPlayer : ConstraintLayout {

    private var binding:ViewSvgaBinding? = null
    private var svgaParser:SVGAParser? = null

    var nextSvgPlay:(()->Unit)? = null

    /**
     * 是否正在播放svg
     */
    var isPlaying = false

    constructor(context: Context) : super(context)
    constructor(context: Context, mAttributeSet: AttributeSet?) : super(context, mAttributeSet){
        initView(context)
    }

    private fun initView(context: Context) {
        binding = ViewSvgaBinding.inflate(LayoutInflater.from(context),this,true)
        svgaParser = SVGAParser(context)
    }


    fun playSvga(imGiftModel:IMGiftModel){
        svgaParser!!.let {
            LogUtil.d("svgPlayer","xxx--$isPlaying")
            it.decodeFromURL(URL(imGiftModel.svga),object:SVGAParser.ParseCompletion{
                override fun onComplete(videoItem: SVGAVideoEntity) {
                    binding!!.apply {
                        val drawable = SVGADrawable(videoItem)
                        svgaImageView.visibility = View.VISIBLE
                        svgaImageView.setImageDrawable(drawable)
                        svgaImageView.loops = 1
                        svgaImageView.callback = callback
                        LogUtil.d("svgPlayer","onComplete--$isPlaying")
                        if (!isPlaying){
                            svgaImageView.startAnimation()
                            isPlaying = true
                        }

                    }

                }

                override fun onError() {
                    binding?.apply {
                        isPlaying = false
                        LogUtil.d("svgPlayer","onError--$isPlaying")
                        svgaImageView.visibility = View.GONE
                        nextSvgPlay?.invoke()
                    }
                }
            },object:SVGAParser.PlayCallback{
                override fun onPlay(file: List<File>) {
                    LogUtil.d("svgPlayer","onPlay--${file.size}")
                }

            })

        }
    }

    fun clear(){
        binding?.apply {
            isPlaying = false
            svgaImageView.pauseAnimation()
            svgaImageView.clear()
        }

    }

    private var callback = object:SVGACallback {

        override fun onFinished() {
            isPlaying = false
            LogUtil.d("svgPlayer","onFinished--${isPlaying}")
            binding?.svgaImageView?.visibility = View.GONE
            nextSvgPlay?.invoke()
        }

        override fun onPause() {
            LogUtil.d("svgPlayer","onPause--${isPlaying}")
        }

        override fun onRepeat() {
            LogUtil.d("svgPlayer","onRepeat--${isPlaying}")
        }

        override fun onStep(frame: Int, percentage: Double) {
        }
    }

}