package com.cr.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import com.cr.cruav.R
import com.cr.data.CrUtil

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/30 14:38
 * 描述：导航栏组件
 */
class CrNavigationBarWidget @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : CrLinearLayoutWidget(context, attrs, defStyleAttr), View.OnClickListener {
    // define: 2023/3/30 定义控件
    var lblLeft: TextView? = null
    var lblTitle: TextView? = null
    var lblRight: TextView? = null

    // define: 2023/3/30 定义接口
    var listener:CrNavigationBarListener? = null

    /**
     * 对外接口
     */
    interface CrNavigationBarListener {
        /*回退*/
        fun onGotoBack()

        /*关闭*/
        fun onDismiss()
    }
    /**
     * 初始化
     */
    init {
        initView(R.layout.wg_navigation_bar)
        joinControls()
        initProperty(attrs!!)
    }

    /**
     * 覆写挂接控件
     */
    override fun joinControls() {
        super.joinControls()
        // todo: 2023/3/30 挂载控件
        lblLeft = findViewById(R.id.nv_left)
        lblRight = findViewById(R.id.nv_right)
        lblTitle = findViewById(R.id.nv_title)
        // todo: 2023/3/30 初始化显示内容
        CrUtil.setFont(context!!,lblLeft!!,R.string.ico_nv_left)
        CrUtil.setFont(context!!,lblRight!!,R.string.ico_nv_right)
        // todo: 2023/4/3 挂接事件
        lblLeft?.setOnClickListener(this)
        lblRight?.setOnClickListener(this)
    }

    /**
     * 初始化属性
     * @param attrs AttributeSet
     */
    override fun initProperty(attrs: AttributeSet) {
        super.initProperty(attrs)
        var attributes:TypedArray?=context!!.obtainStyledAttributes(attrs!!,R.styleable.ViewNavigationBar)
        attributes?.let { itAttr ->
            // todo: 2023/3/30 设置文本
            var title:String? = itAttr.getString(R.styleable.ViewNavigationBar_crTitle)
            title.let { lblTitle?.text = it }
            // todo: 2023/3/30 设置返回箭头
            var isGotoBack = itAttr.getBoolean(R.styleable.ViewNavigationBar_isGotoBack,false)
            lblLeft?.visibility = if(isGotoBack) VISIBLE else GONE
            // todo: 2023/3/30 设置关闭按钮
            var isDismiss = itAttr.getBoolean(R.styleable.ViewNavigationBar_isDismiss,false)
            lblRight?.visibility = if(isDismiss) VISIBLE else GONE
        }
        attributes?.recycle()
    }

    /**
     * 视图点击事件
     * @param view View
     */
    override fun onClick(view: View?) {
        when(view?.id){
            R.id.nv_left->
                listener?.onGotoBack()
            R.id.nv_right->
                listener?.onDismiss()
        }
    }

    /**
     * 设置按钮的显示或隐藏
     * @param backIsVisible Boolean
     * @param dismissIsVisible Boolean
     */
    fun crSetVisible(backIsVisible: Boolean, dismissIsVisible: Boolean) {
        lblLeft!!.visibility = if (backIsVisible) VISIBLE else GONE
        lblRight!!.visibility = if (dismissIsVisible) VISIBLE else GONE
    }

    /**
     * 设置标题
     * @param title 标题
     */
    fun crSetTitle(title: String?) {
        lblTitle!!.text = title
    }

    /**
     * 设置显示标题
     * @param titleRes Int
     */
    fun crSetTitle(titleRes:Int){
        lblTitle!!.setText(titleRes)
    }
}