package com.fastcashier.lib_common.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.fastcashier.lib_common.R;

import java.util.ArrayList;
import java.util.List;

public class DecimalKeyboard extends LinearLayout {

    public static final int DELETE = -2;
    public static final int DOT = -3;

    private View btn_7, btn_8, btn_9, btn_6, btn_5, btn_4, btn_3, btn_2, btn_1, btn_0;
    private View delete;
    private Button btn_dot;
    private List<View> buttons;

    private KeyboardClickListener keyboardClickListener;

    public DecimalKeyboard(Context context) {
        this(context, null);
    }

    public DecimalKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.app_layout_keyboard_decimal, this, true);
        buttons = new ArrayList<>(12);
        ClickListener clickListener = new 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);
        btn_dot = view.findViewById(R.id.btn_dot);
        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(btn_dot);
        for (View btn : buttons) {
            btn.setOnClickListener(clickListener);
        }
    }

    class ClickListener implements OnClickListener {

        @Override
        public void onClick(View v) {

            if (keyboardClickListener == null) {
                return;
            }
            int i = v.getId();

            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_dot) {
                keyboardClickListener.onNumClick(DOT);
            }
        }
    }

    public KeyboardClickListener getKeyboardClickListener() {
        return keyboardClickListener;
    }

    public void setKeyboardClickListener(KeyboardClickListener keyboardClickListener) {
        this.keyboardClickListener = keyboardClickListener;
    }

    public interface KeyboardClickListener {
        void onNumClick(int num);
    }
}
