package com.adealink.weparty.commonui.imageview

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import com.opensource.svgaplayer.SVGADynamicEntity
import com.opensource.svgaplayer.WenextSvgaView
import com.opensource.svgaplayer.control.ControllerListener
import com.opensource.svgaplayer.utils.Supplier

/**
 * 自动按比例缩放svga image view
 */
@Deprecated(message = "已废弃，使用AvatarFrameView替代")
class AvatarFrameSvgaView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : WenextSvgaView(context, attrs, defStyleAttr) {

    private var originWidth: Int = 0
    private var originHeight: Int = 0
    private var textWidthPercent: Double = 0.28

    override fun onAttrsLoad(typedArray: TypedArray, context: Context) {
        super.onAttrsLoad(typedArray, context)
    }

    fun setAvatarFrameUrl(
        url: String,
        ratio: Float,
        dynamicEntity: SVGADynamicEntity?,
        listener: ControllerListener? = null
    ) {
        scaleLayout(ratio)
        if (dynamicEntity == null) {
            setUrl(url, listener = listener)
        } else {
            setUrl(url, supplier = object : Supplier<SVGADynamicEntity> {
                override fun get(): SVGADynamicEntity {
                    return dynamicEntity
                }
            }, listener = listener)
        }
        requestLayout()
    }

    private fun scaleLayout(ratio: Float) {
        val lp = layoutParams
        if (originWidth == 0) {
            originWidth = lp.width
        }
        if (originHeight == 0) {
            originHeight = lp.height
        }
        lp.width = (originWidth * ratio).toInt()
        lp.height = (originHeight * ratio).toInt()
        layoutParams = lp
    }
}