@file:JvmName("MenuUtils")

package com.felipeporge.utility.menu

import android.content.Context
import android.support.v4.content.ContextCompat
import android.view.MenuItem
import android.view.View
import android.widget.FrameLayout
import com.felipeporge.utility.R
import kotlinx.android.synthetic.main.partial_action_badge.view.*


/**
 * Shows badge on a menu item.
 * @param context   The context.
 * @param iconResId Icon resource id.
 * @param count Count to show.
 */
fun MenuItem.showBadge(context: Context, iconResId: Int, count: Int) {
    val badge = setActionView(R.layout.partial_action_badge).actionView as FrameLayout
    badge.badgeActionIconIv?.setImageDrawable(ContextCompat.getDrawable(context, iconResId))

    if(count > 0){
        badge.badgeActionCountTv.visibility = View.VISIBLE
        badge.badgeActionCountTv.text = "$count"
    }else {
        badge.badgeActionCountTv.visibility = View.INVISIBLE
    }
    isVisible = true
}