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

import android.content.Context
import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams
import com.adealink.weparty.module.room.data.SpeakingRippleConfig
import com.adealink.weparty.room.R
import com.adealink.weparty.room.micseat.DecorDynamicLayers
import com.adealink.weparty.room.widget.SpeakingRippleView


class SpeakingRippleDecorView(
    dynamicLayers: DecorDynamicLayers,
    private val config: SpeakingRippleConfig = SpeakingRippleConfig(),
) : BaseDecorView<SpeakingRippleView>(R.id.id_mic_seat_speaking_ripple_decor, dynamicLayers),
    ISpeakingRippleDecorApi {

    private var isSpeaking = false

    override fun createView(context: Context): SpeakingRippleView {
        return SpeakingRippleView(context)
    }

    override fun onViewAdded(view: SpeakingRippleView) {
        super.onViewAdded(view)
        view.setConfig(this@SpeakingRippleDecorView.config)
        view.setBaseSize(baseSize)
        view.setRippleRatio(RIPPLE_RATIO)
    }

    override fun getLayoutParams(): LayoutParams {
        return LayoutParams((baseSize * RIPPLE_RATIO).toInt(), (baseSize * RIPPLE_RATIO).toInt()).apply {
            startToStart = R.id.id_mic_seat_avatar_decor
            endToEnd = R.id.id_mic_seat_avatar_decor
            topToTop = R.id.id_mic_seat_avatar_decor
            bottomToBottom = R.id.id_mic_seat_avatar_decor
        }
    }

    override fun showSpeaking(speaking: Boolean) {
        this.isSpeaking = speaking
        if (speaking) {
            startRipple()
        } else {
            stopRipple()
        }
    }

    private fun startRipple() {
        show()
        view {
            it.startRipple()
        }
    }

    private fun stopRipple() {
        if (isAttached()) {
            view()?.stopRipple()
        }
        gone()

    }

}