package info.knacki.pass.ui.alertPrompt.integratedKeyboard;

import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.util.Xml;

import java.util.Locale;

import info.knacki.pass.R;

public class KeyboardWidget extends KeyboardView implements KeyboardView.OnKeyboardActionListener {
    private final Keyboard fKeyboard;
    private Keyboard fNumberKeyboard;
    private Keyboard fSymbolsKeyboard;
    private OnKeyListener fListener;
    private final Context fContext;
    private boolean fIsDisplayingLetters;

    public KeyboardWidget(Context c) {
        super(c, Xml.asAttributeSet(c.getResources().getXml(R.xml.keyboard_attributes)));
        fContext = c;
        fKeyboard = new Keyboard(c, R.xml.qwerty_keyboard);
        setKeyboard(fKeyboard);
        setOnKeyboardActionListener(this);
        setPreviewEnabled(false);
        fIsDisplayingLetters = true;
    }

    public KeyboardWidget SetListener(OnKeyListener listener) {
        fListener = listener;
        return this;
    }

    @Override
    public void onPress(int primaryCode) {
    }

    @Override
    public void onRelease(int primaryCode) {
    }

    private Keyboard SetActiveLayout(Keyboard loadedKeyboard, int layout) {
        Keyboard k = loadedKeyboard == null ? new Keyboard(fContext, layout) : loadedKeyboard;
        setKeyboard(k);
        return k;
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        if (primaryCode == fContext.getResources().getInteger(R.integer.id_keyboard_numbers)) {
            if (fIsDisplayingLetters)
                fNumberKeyboard = SetActiveLayout(fNumberKeyboard, R.xml.qwerty_symbols);
            else
                setKeyboard(fKeyboard);
            fIsDisplayingLetters = !fIsDisplayingLetters;
            setShifted(false);
        } else if (primaryCode == fContext.getResources().getInteger(R.integer.id_keyboard_shift)) {
            if (!fIsDisplayingLetters) {
                if (isShifted()) {
                    setKeyboard(fNumberKeyboard);
                    setShifted(false);
                } else {
                    fSymbolsKeyboard = SetActiveLayout(fSymbolsKeyboard, R.xml.qwerty_symbol_shift);
                    setShifted(true);
                }
            }
            else
                setShifted(!isShifted());
        } else if (primaryCode == fContext.getResources().getInteger(R.integer.id_keyboard_delete)) {
            fListener.OnBackspace();
        } else {
            String text = new String(Character.toChars(primaryCode));

            if (isShifted() && fIsDisplayingLetters)
                text = text.toUpperCase(Locale.getDefault());
            if (text.length() > 0)
                fListener.OnKeyReceived(text);
        }
    }

    @Override
    public void onText(CharSequence text) {
    }

    @Override
    public void swipeLeft() {

    }

    @Override
    public void swipeRight() {

    }

    @Override
    public void swipeDown() {

    }

    @Override
    public void swipeUp() {

    }

    public interface OnKeyListener {
        void OnKeyReceived(CharSequence c);

        void OnBackspace();
    }
}
