package com.grkj.iscs_mc.view.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.LayoutTitleBarBinding
import com.grkj.iscs_mc.util.SPUtils

class TitleBar(ctx: Context, attrs: AttributeSet) : RelativeLayout(ctx, attrs) {

    private var mBinding: LayoutTitleBarBinding

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

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

        attrSet.getString(R.styleable.TitleBar_bar_title)?.let { mBinding.tvModuleName.text = it }

        attrSet.getResourceId(R.styleable.TitleBar_bar_icon, 0).let {
            mBinding.ivIcon.setBackgroundResource(it)
        }

        attrSet.recycle()

        mBinding.tvUser.text = SPUtils.getLoginUser(ctx)?.userName
    }

    fun setTitle(title: String) {
        mBinding.tvModuleName.text = title
    }

    fun setIcon(icon: Int) {
        mBinding.ivIcon.setImageResource(icon)
    }
}