package com.qimai.android.widgetlib.bottombar


import android.content.Context
import android.graphics.Color
import android.os.Build
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.qimai.android.tools.dpTopx
import com.qimai.android.widgetlib.R


class BottomBarTab(
    context: Context,
    title: CharSequence,
    tabTextColor: Int,
    selectedTabTextColor: Int, @DrawableRes icon: Int, @DrawableRes selectedIcon: Int,
    listener: OnSelectedTabListener?
) :
    FrameLayout(context) {
    private var mImg: ImageView? = null
    private var mTvTitle: TextView? = null
    private var mTvUnreadCount: TextView? = null
    private var mContext: Context? = null

    var mTabTextColor: Int = 0
    var mSelectedTabTextColor: Int = 0
    private var mIcon: Int = 0
    private var mSelectedIcon: Int = 0
    private var mOnSelectedTabListener: OnSelectedTabListener? = null

    /**
     * 获取当前未读数量
     */
    /**
     * 设置未读数量
     */

    var unreadCount: Int
        get() {
            var count = 0
            if (TextUtils.isEmpty(mTvUnreadCount!!.text)) {
                return count
            }
            if (mTvUnreadCount!!.text.toString() == "99+") {
                return 99
            }
            try {
                count = Integer.valueOf(mTvUnreadCount!!.text.toString())
            } catch (ignored: Exception) {
            }

            return count
        }
        set(num) = if (num <= 0) {
            mTvUnreadCount!!.visibility = View.GONE
        } else {
            mTvUnreadCount!!.text = ""
            mTvUnreadCount!!.visibility = View.VISIBLE
        }

    init {
        init(context, title, tabTextColor, selectedTabTextColor, icon, selectedIcon, listener!!)
    }

    interface OnSelectedTabListener {
        fun onSelected(tabImageView: ImageView?, selected: Boolean)
    }

    private fun init(
        context: Context,
        title: CharSequence,
        tabTextColor: Int,
        selectedTabTextColor: Int,
        icon: Int,
        selectedIcon: Int,
        listener: OnSelectedTabListener
    ) {
        this.mContext = context
        this.mTabTextColor = tabTextColor
        this.mIcon = icon
        this.mSelectedIcon = selectedIcon
        this.mOnSelectedTabListener = listener
        this.mSelectedTabTextColor = selectedTabTextColor

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val typedArray =
                context.obtainStyledAttributes(intArrayOf(android.R.attr.selectableItemBackgroundBorderless))
            val drawable = typedArray.getDrawable(0)
            background = drawable
            typedArray.recycle()
        }

        val lLContainer = LinearLayout(context)
        lLContainer.orientation = LinearLayout.VERTICAL
        lLContainer.gravity = Gravity.CENTER
        val paramsContainer =
            LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        paramsContainer.gravity = Gravity.CENTER_HORIZONTAL
        lLContainer.layoutParams = paramsContainer

        mImg = ImageView(context)
        val dp32 = mContext!!.dpTopx(32.0f)
        mImg!!.layoutParams = ViewGroup.LayoutParams(dp32, dp32)
        lLContainer.addView(mImg)

        mTvTitle = TextView(context)
        mTvTitle!!.text = title
        val tvTitleParams =
            LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        //        tvTitleParams.topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
        mTvTitle!!.textSize = 10f
        mTvTitle!!.setTextColor(
            if (mTabTextColor == 0) ContextCompat.getColor(
                context,
                R.color.QmTopBar_color_FFFFFFF
            ) else mTabTextColor
        )
        mTvTitle!!.layoutParams = tvTitleParams
        lLContainer.addView(mTvTitle)

        addView(lLContainer)

        val min = context.dpTopx(12F)
        //        int padding = SegmentUtil.dip2px(context, 5);
        mTvUnreadCount = TextView(context)
        mTvUnreadCount!!.setBackgroundResource(R.drawable.segment_bg_msg_bubble)
        mTvUnreadCount!!.minWidth = min
        mTvUnreadCount!!.minHeight = min
        mTvUnreadCount!!.setTextColor(Color.WHITE)
        //        mTvUnreadCount.setPadding(padding, 0, padding, 0);
        mTvUnreadCount!!.gravity = Gravity.CENTER
        val tvUnReadParams = LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, min)
        tvUnReadParams.gravity = Gravity.CENTER
        tvUnReadParams.leftMargin = context.dpTopx(17F)
        tvUnReadParams.bottomMargin = context.dpTopx(14F)
        mTvUnreadCount!!.layoutParams = tvUnReadParams
        mTvUnreadCount!!.visibility = View.GONE

        addView(mTvUnreadCount)

        isSelected = false
    }

    override fun setSelected(selected: Boolean) {
        super.setSelected(selected)

        if (mSelectedTabTextColor != 0) {
            mTvTitle!!.setTextColor(if (selected) mSelectedTabTextColor else mTabTextColor)
        }

        if (mOnSelectedTabListener != null && mSelectedIcon == 0 && mIcon == 0) {
            mOnSelectedTabListener!!.onSelected(mImg, selected)
        } else {
            mImg!!.setImageResource(if (selected) mSelectedIcon else mIcon)
        }
    }

    companion object {

        fun newTab(
            context: Context,
            title: CharSequence,
            tabTextColor: Int,
            icon: Int,
            selectedIcon: Int
        ): BottomBarTab {
            return BottomBarTab(context, title, tabTextColor, 0, icon, selectedIcon, null)
        }

        fun newTab(
            context: Context,
            title: CharSequence,
            tabTextColor: Int,
            listener: OnSelectedTabListener
        ): BottomBarTab {
            return BottomBarTab(context, title, tabTextColor, 0, 0, 0, listener)
        }

        fun newTab(
            context: Context,
            title: CharSequence,
            tabTextColor: Int,
            selectedTabTextColor: Int,
            listener: OnSelectedTabListener
        ): BottomBarTab {
            return BottomBarTab(context, title, tabTextColor, selectedTabTextColor, 0, 0, listener)
        }
    }

}
