package com.fastcashier.lib_common.ui.widget

import android.content.Context
import android.util.AttributeSet
import kotlin.jvm.JvmOverloads
import android.widget.LinearLayout
import com.fastcashier.lib_common.R
import com.fastcashier.lib_common.ui.widget.NumKeyboard
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import java.util.ArrayList

class NumKeyboard @JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null) :
    LinearLayout(context, attrs) {
    private val btn_7: View
    private val btn_8: View
    private val btn_9: View
    private val btn_6: View
    private val btn_5: View
    private val btn_4: View
    private val btn_3: View
    private val btn_2: View
    private val btn_1: View
    private val btn_0: View
    private val delete: View
    val okButton: Button
    private val buttons: MutableList<View>
    var keyboardClickListener: KeyboardClickListener? = null

    internal inner class ClickListener : OnClickListener {
        override fun onClick(v: View) {
            if (keyboardClickListener == null) {
                return
            }
            val i = v.id
            if (i == R.id.btn_7) {
                keyboardClickListener!!.onNumClick(7)
            }
            if (i == R.id.btn_8) {
                keyboardClickListener!!.onNumClick(8)
            }
            if (i == R.id.btn_9) {
                keyboardClickListener!!.onNumClick(9)
            }
            if (i == R.id.btn_6) {
                keyboardClickListener!!.onNumClick(6)
            }
            if (i == R.id.btn_5) {
                keyboardClickListener!!.onNumClick(5)
            }
            if (i == R.id.btn_4) {
                keyboardClickListener!!.onNumClick(4)
            }
            if (i == R.id.btn_3) {
                keyboardClickListener!!.onNumClick(3)
            }
            if (i == R.id.btn_2) {
                keyboardClickListener!!.onNumClick(2)
            }
            if (i == R.id.btn_1) {
                keyboardClickListener!!.onNumClick(1)
            }
            if (i == R.id.btn_0) {
                keyboardClickListener!!.onNumClick(0)
            }
            if (i == R.id.btn_delete) {
                keyboardClickListener!!.onNumClick(DELETE)
            }
            if (i == R.id.btn_sure) {
                keyboardClickListener!!.onNumClick(SURE)
            }
        }
    }

    interface KeyboardClickListener {
        fun onNumClick(num: Int)
    }

    companion object {
        const val DELETE = -2
        const val SURE = -3
    }

    init {
        val view =
            LayoutInflater.from(context).inflate(R.layout.app_layout_keyboard_number, this, true)
        buttons = ArrayList(12)
        val clickListener: ClickListener = ClickListener()
        btn_7 = view.findViewById(R.id.btn_7)
        btn_8 = view.findViewById(R.id.btn_8)
        btn_9 = view.findViewById(R.id.btn_9)
        btn_6 = view.findViewById(R.id.btn_6)
        btn_5 = view.findViewById(R.id.btn_5)
        btn_4 = view.findViewById(R.id.btn_4)
        btn_3 = view.findViewById(R.id.btn_3)
        btn_2 = view.findViewById(R.id.btn_2)
        btn_1 = view.findViewById(R.id.btn_1)
        btn_0 = view.findViewById(R.id.btn_0)
        delete = view.findViewById(R.id.btn_delete)
        okButton = view.findViewById(R.id.btn_sure)
        buttons.add(btn_7)
        buttons.add(btn_8)
        buttons.add(btn_9)
        buttons.add(btn_6)
        buttons.add(btn_5)
        buttons.add(btn_4)
        buttons.add(btn_3)
        buttons.add(btn_2)
        buttons.add(btn_1)
        buttons.add(btn_0)
        buttons.add(delete)
        buttons.add(okButton)
        for (btn in buttons) {
            btn.setOnClickListener(clickListener)
        }
    }

    fun setKeyNumKeyboardClickListener(keyboardClickListener: KeyboardClickListener){
        this.keyboardClickListener = keyboardClickListener
    }
}