package com.rdiot.yx485.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.DataBindingUtil
import com.rdiot.yx485.R
import com.rdiot.yx485.databinding.ViewNavbarBinding
import com.rdiot.yx485.util.setClickLimitListener

/**
 * 自定义Navbar
 * @author mR2hao
 * @date 2021/10/15
 */
class ZNavbar(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) {
    var binding: ViewNavbarBinding =
        DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.view_navbar,
            this,
            true
        )

    private var leftIcon: Int = -1
    private var rightIcon: Int = -1
    private var rightText: String? = null
    private var leftText: String? = null


    init {
        val typedArray = context.obtainStyledAttributes(
            attrs,
            R.styleable.ZNavbar
        )
        val bgColor = typedArray.getColor(R.styleable.ZNavbar_zBgColor, 0)
        binding.cl.setBackgroundColor(bgColor)


        //标题
        typedArray.getString(R.styleable.ZNavbar_zTitle)?.let { binding.tvTitle.text = it }
        //副标题
        val subTitle = typedArray.getString(R.styleable.ZNavbar_zSubTitle)
        if (!subTitle.isNullOrBlank()) {
            binding.tvSubTitle.text = subTitle
        } else {
            binding.tvSubTitle.visibility = GONE
            binding.tvTitle.textSize = 20f
        }

        //左图标按钮
        leftIcon = typedArray.getResourceId(R.styleable.ZNavbar_zLeftIcon, -1)
        if (leftIcon != -1) {
            binding.ibLeft.setImageResource(leftIcon)
        }
        //左文本按钮
        leftText = typedArray.getString(R.styleable.ZNavbar_zLeftText)
        if (!leftText.isNullOrBlank()) {
            binding.tvTextLeft.text = leftText
            binding.tvTextLeft.visibility = VISIBLE
            binding.ibLeft.visibility = GONE
        } else if (leftIcon != -1) {
            binding.ibLeft.setImageResource(leftIcon)
        }

        if (leftIcon == -1) {
            binding.ibLeft.visibility = GONE
        }
        if (leftText.isNullOrBlank()) {
            binding.tvTextLeft.visibility = GONE
        }

        //右图标按钮
        rightIcon = typedArray.getResourceId(R.styleable.ZNavbar_zRightIcon, -1)

        //右文本按钮
        rightText = typedArray.getString(R.styleable.ZNavbar_zRightText)

        if (!rightText.isNullOrBlank()) {
            binding.tvTextRight.text = rightText
            binding.tvTextRight.visibility = VISIBLE
            binding.ibRight.visibility = GONE
        } else if (rightIcon != -1) {
            binding.ibRight.setImageResource(rightIcon)
        }

        if (rightIcon == -1) {
            binding.ibRight.visibility = GONE
        }
        if (rightText.isNullOrBlank()) {
            binding.tvTextRight.visibility = GONE
        }
        typedArray.recycle()
    }


    /**
     * 设置标题
     * @param title String 标题内容
     */
    fun setTitle(title: String?) {
        binding.tvTitle.text = title
    }

    fun setSubTitle(subTitle: String?) {
        binding.tvSubTitle.text = subTitle
    }

    /**
     * 获取标题
     * @return String
     */
    fun getTitle(): String = binding.tvTitle.text.toString()

    /** 设置按钮点击事件 */
    fun setLeftClickListener(action: () -> Unit) {
        if (leftIcon != -1 && leftText.isNullOrBlank()) {
            binding.ibLeft.visibility = VISIBLE
            binding.ibLeft.setClickLimitListener { action() }
        }
        if (leftText != null) {
            binding.tvTextLeft.visibility = VISIBLE
            binding.tvTextLeft.setClickLimitListener { action() }
        }

        if (leftIcon == -1 && leftText.isNullOrBlank()){
            binding.ibLeft.visibility = VISIBLE
            binding.ibLeft.setClickLimitListener { action() }
        }
    }

    /** 设置按钮点击事件 */
    fun setRightClickListener(action: () -> Unit) {
        if (rightIcon != -1 && rightText.isNullOrBlank()) {
            binding.ibRight.visibility = VISIBLE
            binding.ibRight.setClickLimitListener { action() }
        }
        if (rightText != null) {
            binding.tvTextRight.visibility = VISIBLE
            binding.tvTextRight.setClickLimitListener { action() }
        }
    }

    /**
     * 左按钮使能
     * @param enable Boolean
     */
    fun setLeftBtnEnable(enable: Boolean) {
        binding.ibLeft.isEnabled = enable
    }

    /**
     * 右按钮使能
     * @param enable Boolean
     */
    fun setRightBtnEnable(enable: Boolean) {
        binding.ibRight.isEnabled = enable
        binding.tvTextRight.isEnabled = enable
    }

    /** 设置右按钮可见 */
    fun setRightBtnVisible(visible: Boolean) {
        binding.ibRight.visibility = if (visible) VISIBLE else GONE
        binding.tvTextRight.visibility = if (visible) VISIBLE else GONE
    }

    fun setRightTextBtnVisible(visible: Boolean) {
        binding.tvTextRight.visibility = if (visible) VISIBLE else GONE
    }

    fun setRightImageBtnVisible(visible: Boolean) {
        binding.ibRight.visibility = if (visible) VISIBLE else GONE
    }

    /**
     * 设置右按钮图标
     * @param icon Int
     */
    fun setRightBtnIcon(@DrawableRes icon: Int) {
        binding.tvTextRight.visibility = GONE

        binding.ibRight.setImageResource(icon)
        binding.ibRight.visibility = VISIBLE
    }

    fun setRightBtnText(@StringRes id: Int) {
        binding.ibRight.visibility = GONE

        binding.tvTextRight.setText(id)
        binding.tvTextRight.visibility = VISIBLE

    }

    fun setRightBtnTextColor(@ColorRes color: Int) {
        binding.tvTextRight.setTextColor(resources.getColor(color, null))
    }


}