package com.zxbw.modulemain.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import android.widget.TextView
import com.zxbw.modulemain.R

/**
 * Created by gaoleichao on 2018/12/28
 */
class NavigationBottomView : RelativeLayout {
    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    private var mUserText: TextView? = null
    private var mCashCheckText: TextView? = null
    private var mRightText: TextView? = null
    private var mShopText: TextView? = null
    private var navifationCallback: NavifationCallback? = null

    private fun init() {
        LayoutInflater.from(context).inflate(R.layout.view_navigation_bottom, this)
        mRightText = findViewById(R.id.id_view_navigation_bottom_box)
        mShopText = findViewById(R.id.id_view_navigation_bottom_shop)
        mCashCheckText = findViewById(R.id.id_view_navigation_bottom_cash)
        mUserText = findViewById(R.id.id_view_navigation_bottom_user)

        mRightText?.setOnClickListener {
            setBottomDraw(0)
            navifationCallback?.onNavigationClick(it.id)
        }
        mShopText?.setOnClickListener {
            setBottomDraw(1)
            navifationCallback?.onNavigationClick(it.id)
        }
        mCashCheckText?.setOnClickListener {
            setBottomDraw(2)
            navifationCallback?.onNavigationClick(it.id)
        }
        mUserText?.setOnClickListener {
            setBottomDraw(3)
            navifationCallback?.onNavigationClick(it.id)
        }
    }

    private fun setBottomDraw(index: Int) {
        mRightText?.setTextColor(resources.getColor(R.color.color_999999))
        mCashCheckText?.setTextColor(resources.getColor(R.color.color_999999))
        mUserText?.setTextColor(resources.getColor(R.color.color_999999))
        mShopText?.setTextColor(resources.getColor(R.color.color_999999))
        var rightDrawable = resources.getDrawable(R.drawable.icon_bottom_right_default)

        var shopDrawable = resources.getDrawable(R.drawable.icon_bottom_shop_default)

        var cashDrawable = resources.getDrawable(R.drawable.icon_bottom_cash_default)

        var userDrawable = resources.getDrawable(R.drawable.icon_bottom_my_default)

        when (index) {
            0 -> {
                mRightText?.setTextColor(resources.getColor(R.color.color_333333))
                rightDrawable = resources.getDrawable(R.drawable.icon_bottom_right_click)
            }
            1 -> {
                mShopText?.setTextColor(resources.getColor(R.color.color_333333))
                shopDrawable = resources.getDrawable(R.drawable.icon_bottom_shop_click)
            }
            2 -> {
                mCashCheckText?.setTextColor(resources.getColor(R.color.color_333333))
                cashDrawable = resources.getDrawable(R.drawable.icon_bottom_cash_click)
            }
            3 -> {
                mUserText?.setTextColor(resources.getColor(R.color.color_333333))
                userDrawable = resources.getDrawable(R.drawable.icon_bottom_my_click)
            }
        }

        rightDrawable.setBounds(
            0,
            0,
            rightDrawable.minimumWidth,
            rightDrawable.minimumHeight
        )
        shopDrawable.setBounds(
            0,
            0,
            shopDrawable.minimumWidth,
            shopDrawable.minimumHeight
        )
        cashDrawable.setBounds(
            0,
            0,
            cashDrawable.minimumWidth,
            cashDrawable.minimumHeight
        )
        userDrawable.setBounds(
            0,
            0,
            userDrawable.minimumWidth,
            userDrawable.minimumHeight
        )
        mRightText?.setCompoundDrawables(null, rightDrawable, null, null)
        mShopText?.setCompoundDrawables(null, shopDrawable, null, null)
        mCashCheckText?.setCompoundDrawables(null, cashDrawable, null, null)
        mUserText?.setCompoundDrawables(null, userDrawable, null, null)
    }


    fun setNavifationCallback(navifationCallback: NavifationCallback) {
        this.navifationCallback = navifationCallback
    }


    interface NavifationCallback {

        fun onNavigationClick(vId: Int)
    }

}