package com.adealink.weparty.room.micseat.decor

import android.content.Context
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import com.adealink.frame.image.view.NetworkImageView
import com.adealink.weparty.room.R
import com.adealink.weparty.room.micseat.DecorDynamicLayers
import com.facebook.drawee.generic.RoundingParams
import com.adealink.weparty.R as APP_R

open class AvatarDecorView(
    dynamicLayers: DecorDynamicLayers,
    private var emptyResId: Int = R.drawable.room_micseat_seat_unlock_ic,
    private var lockResId: Int = R.drawable.room_micseat_seat_lock_ic,
    private val topMarginRatio: Float = AVATAR_MARGIN_TOP_RATIO
) : BaseDecorView<NetworkImageView>(R.id.id_mic_seat_avatar_decor, dynamicLayers),
    IAvatarDecorApi,
    ILockDecorApi {

    override val autoAttach: Boolean = true

    private var isLock = false
    private var isOnMic = false

    override fun createView(context: Context): NetworkImageView {
        return NetworkImageView(context)
            .apply {
                setDefaultImageResId(emptyResId)
                setFailureImage(APP_R.drawable.common_default_avatar_ic)
            }
    }

    override fun getLayoutParams(): ConstraintLayout.LayoutParams {
        return ConstraintLayout.LayoutParams(baseSize, baseSize).apply {
            startToStart = ConstraintSet.PARENT_ID
            endToEnd = ConstraintSet.PARENT_ID
            topToTop = ConstraintSet.PARENT_ID
            topMargin = (baseSize * topMarginRatio).toInt()
        }
    }

    override fun showAvatar(url: String) {
        isOnMic = true
        isLock = false
        view {
            it.hierarchy.roundingParams = RoundingParams.asCircle()
            if (url.isEmpty()) {
                it.setActualImageResource(APP_R.drawable.common_default_avatar_ic)
            } else {
                it.setProgressBarImage(APP_R.drawable.common_default_avatar_ic)
                it.setImageUrl(url)
            }
        }
    }

    override fun resetAvatar() {
        emptyResId = R.drawable.room_micseat_seat_unlock_ic
        lockResId = R.drawable.room_micseat_seat_lock_ic
        isLock = false
        isOnMic = false
    }

    override fun getAvatarView(): View? {
        return view()
    }

    override fun showLock(lock: Boolean) {
        isLock = lock
        isOnMic = false
        updateMicState()
    }

    override fun setAvatarEmptyResId(id: Int) {
        emptyResId = id
        updateMicState()
    }

    override fun setAvatarLockResId(id: Int) {
        lockResId = id
        updateMicState()
    }

    private fun updateMicState() {
        val resId = when {
            isOnMic -> null
            isLock -> lockResId
            else -> emptyResId
        }
        view { v ->
            resId?.let { resId ->
                v.hierarchy.roundingParams = null
                v.setActualImageResource(resId)
            }
        }
    }
}