package com.grkj.iscs_mc.view.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.LayoutCommonBtnBinding

/**
 * 通用Tab控件
 */
class CommonBtn(private val ctx: Context, attrs: AttributeSet) : LinearLayout(ctx, attrs) {

    private var mBinding: LayoutCommonBtnBinding

    init {
        val root = View.inflate(ctx, R.layout.layout_common_btn, this)
        mBinding = LayoutCommonBtnBinding.bind(root)

        val attrSet = ctx.obtainStyledAttributes(attrs, R.styleable.CommonBtn)

        val btnText = attrSet.getString(R.styleable.CommonBtn_btn_name)
        mBinding.tvName.text = btnText

        val btnIcon = attrSet.getResourceId(R.styleable.CommonBtn_btn_icon, 0)
        if (btnIcon != 0) {
            mBinding.iv.visibility = View.VISIBLE
            mBinding.iv.setImageResource(btnIcon)
        }

        val background = attrSet.getResourceId(R.styleable.CommonBtn_btn_bg, 0)
        if (background != 0) {
            mBinding.root.setBackgroundResource(background)
        }

        attrSet.recycle()
    }

    fun setText(text: String) {
        mBinding.tvName.text = text
    }

    fun setIcon(icon: Int?) {
        icon?.let {
            mBinding.iv.visibility = View.VISIBLE
            mBinding.iv.setImageResource(it)
        } ?: run {
            mBinding.iv.visibility = View.GONE
        }
    }
}