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

import android.content.Context
import android.util.TypedValue
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.widget.TextViewCompat
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.aab.util.getCompatDrawable
import com.adealink.frame.util.DisplayUtil.dp2px
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.module.room.data.MicIndex
import com.adealink.weparty.room.R
import com.adealink.weparty.room.micseat.DecorDynamicLayers
import com.adealink.weparty.room.widget.RoomNameView
import com.adealink.weparty.R as APP_R

class NameDecorView(
    dynamicLayers: DecorDynamicLayers,
    val isHost: Boolean = false,
    val topMargin: Int = 6.dp()
) : BaseDecorView<RoomNameView>(R.id.id_mic_seat_name_decor, dynamicLayers),
    INameDecorApi,
    IMicIndexDecorApi,
    IRoomRoleDecorApi {

    override val autoAttach: Boolean = true

    override fun createView(context: Context): RoomNameView {
        return RoomNameView(context).apply {
            minHeight = 14.dp()
        }
    }

    override fun onViewAdded(view: RoomNameView) {
        if (scale < 1) {
            TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
                view.getNameTextView(),
                6,
                11,
                1,
                TypedValue.COMPLEX_UNIT_DIP
            )
        }
        if (isHost) {
            view.setLeftDrawable(getCompatDrawable(R.drawable.room_dispatch_center_host_tag_ic), size = 11.dp())
        } else {
            view.setLeftDrawable(null)
        }
    }

    override fun getLayoutParams(): ConstraintLayout.LayoutParams {
        return ConstraintLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            when (scale < 1) {
                true -> dp2px(15f * scale)
                else -> ViewGroup.LayoutParams.WRAP_CONTENT
            }
        ).apply {
            constrainedWidth = true
            topToBottom = R.id.id_mic_seat_avatar_decor
            startToStart = ConstraintLayout.LayoutParams.PARENT_ID
            endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
            this.topMargin = (this@NameDecorView.topMargin * scale).toInt()
        }
    }

    override fun showName(name: String, uid: String) {
        view {
            it.setNameColor(getCompatColor(APP_R.color.white))
            it.setName(name)
        }
    }

    override fun showMicIndex(micIndex: MicIndex) {
        view {
            it.setNameColor(getCompatColor(APP_R.color.color_99FFFFFF))
            it.setName(micIndex.micName.invoke())
        }
    }
}