package com.adealink.weparty.effect

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import com.adealink.frame.effect.view.WeAnimView

/**
 * 头像框动效播放view
 */
class AvatarFrameView(context: Context, attrs: AttributeSet? = null) : WeAnimView(context, attrs) {

    /**
     * 可能触发一次头像框获取接口的拉取，列表场景慎用
     */
    fun showAvatarFrame(uid: Long, cached: Boolean = true) {
//        doOnAttach {
//            val storeOwner = findViewTreeViewModelStoreOwner() ?: return@doOnAttach
//            val lifeCycleOwner = findViewTreeLifecycleOwner() ?: return@doOnAttach
//            val userDecorViewModel = ProfileModule.getUserDecorViewModel(storeOwner)
//            userDecorViewModel?.getDecor(uid = uid, DecorType.AVATAR_FRAME, cached)
//                ?.observe(lifeCycleOwner) {
//                    if (!isAttachedToWindow) {
//                        return@observe
//                    }
//                    when (it) {
//                        is Rlt.Success -> {
//                            val avatarFrameDecor = it.data
//                            if (avatarFrameDecor is AvatarFrameDecor) {
//                                show()
//                                showAvatarFrame(avatarFrameDecor)
//                            } else {
//                                stopPlay()
//                                gone()
//                            }
//
//                        }
//
//                        is Rlt.Failed -> {
//                            stopPlay()
//                            gone()
//                        }
//                    }
//                }
//        }
    }

//    fun showAvatarFrame(
//        avatarFrameConfig: AvatarFrameDecor?,
//    ) {
//        Log.d(TAG, "hashCode:${hashCode()}, showAvatarFrame, avatarFrameConfig: $avatarFrameConfig")
//        if (avatarFrameConfig == null) {
//            stopPlay()
//            gone()
//            return
//        }
//        val effectResource = avatarFrameConfig.effectResource
//        val effectAnimType = EffectAnimType.map(effectResource.animType)
//        if (effectAnimType != EffectAnimType.SVGA && effectAnimType != EffectAnimType.MP4 && effectAnimType != EffectAnimType.TC) {
//            stopPlay()
//            gone()
//            return
//        }
//        show()
//        setConfig(
//            AnimExtraConfig(
//                loop = INFINITE_LOOP,
//                ratio = avatarFrameConfig.ratio,
//            )
//        )
//        setUrl(
//            effectResource.resourceUrl,
//            avatarFrameConfig.extraConfig?.createAnimDynamicProvider()
//        )
//    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        Log.d(TAG, "hashCode:${hashCode()}, onDetachedFromWindow")
    }

    companion object {
        private const val TAG = "AvatarFrameView"
    }
}