package com.adealink.weparty.room.widget

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.annotation.ColorInt
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.room.databinding.WidgetRoomNameBinding

class RoomNameView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private val binding = WidgetRoomNameBinding.inflate(LayoutInflater.from(context), this)

    fun setLeftDrawable(drawable: Drawable?, size: Int = 11.dp(), marginEnd: Int = 2.dp()) {
        if (drawable == null) {
            binding.ivLeft.gone()
            return
        }
        binding.ivLeft.show()
        binding.ivLeft.setImageDrawable(drawable)
        binding.ivLeft.updateLayoutParams<ConstraintLayout.LayoutParams> {
            this.width = size
            this.height = size
            this.marginEnd = marginEnd
        }
    }

    fun setNameColor(@ColorInt color: Int) {
        binding.tvName.setTextColor(color)
    }

    fun setName(name: String?) {
        binding.tvName.text = name
    }

    fun getLeftDrawable(): AppCompatImageView {
        return binding.ivLeft
    }

    fun getNameTextView(): AppCompatTextView {
        return binding.tvName
    }

}