package com.dlc.eboard.ui.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.dlc.eboard.R
import com.dlc.eboard.databinding.WidgetNavViewCompatBinding

/**
 * info
 *
 * @author ReiChin_
 */
class NavViewCompat @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    ConstraintLayout(context, attrs, defStyleAttr) {

    private val viewBinding =
        WidgetNavViewCompatBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        val typeArray = context.obtainStyledAttributes(attrs, R.styleable.NavViewCompat)
        val location = typeArray.getString(R.styleable.NavViewCompat_location)
        val showHome = typeArray.getBoolean(R.styleable.NavViewCompat_showHome, true)
        val showBack = typeArray.getBoolean(R.styleable.NavViewCompat_showBack, true)
        val showFingerEnroll = typeArray.getBoolean(R.styleable.NavViewCompat_showFingerEnroll, false)
        typeArray.recycle()

        viewBinding.navView.setLocationText(location ?: "")
        setShowHome(showHome)
        viewBinding.homeLayout.setOnClickListener { mNavListener?.onHomeViewClicked() }
        viewBinding.backLayout.visibility = if (showBack) VISIBLE else GONE
        viewBinding.backLayout.setOnClickListener { mNavListener?.onBackViewClicked() }
        viewBinding.fingerEnroll.visibility = if (showFingerEnroll) VISIBLE else GONE
        viewBinding.fingerEnroll.setOnClickListener { mNavListener?.onFingerEnrollViewClicked() }
    }

    fun setShowHome(showHome: Boolean) {
        viewBinding.homeLayout.visibility = if (showHome) VISIBLE else GONE
    }

    fun refreshCountDownView(countDown: Int) {
        viewBinding.countDown.text = "(${countDown}s)"
    }

    private var mNavListener: NavListener? = null

    fun setNavListener(listener: NavListener) {
        this.mNavListener = listener
    }

    interface NavListener {
        fun onHomeViewClicked()
        fun onBackViewClicked()
        fun onFingerEnrollViewClicked()
    }

}