package com.exquisite.keyboard;

import android.content.Context;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.KeyboardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;

import com.exquisite.R;

public class KeyboardService extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
    private int[] ViewXmls = new int[4];
    private MyKeyboard myKeyboard;

    public KeyboardService() {

    }

    @Override
    public View onCreateInputView() {
        ViewXmls[0] = R.xml.view_1;
        ViewXmls[1] = R.xml.view_2;
        ViewXmls[2] = R.xml.view_3;
        ViewXmls[3] = R.xml.view_4;

        View inputView = LayoutInflater.from(this).inflate(R.layout.keyboard_view, null, false);
        myKeyboard = inputView.findViewById(R.id.keyboardView);
        myKeyboard.setKeyboard(new MyKeyboard.KeyBoard(this, ViewXmls[0]));
        myKeyboard.setOnKeyboardActionListener(this);
//        myKeyboard.setPreviewEnabled(false);
        return inputView;
    }

    @Override
    public void onWindowShown() {
        super.onWindowShown();
        myKeyboard.updateConfigView(this);
        myKeyboard.invalidate();
    }

    @Override
    public void onPress(int primaryCode) {

    }

    @Override
    public void onRelease(int primaryCode) {

    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {

        switch (primaryCode) {
            case MyKeyboard.KeyBoard.KEYCODE_SHIFT:
                if (myKeyboard.getShiiftType() == 0) {
                    changeView(3);
                    myKeyboard.setShiiftType(1);
                } else {
                    changeView(0);
                    myKeyboard.setShiiftType(0);
                }
                break;
            case MyKeyboard.KeyBoard.KEYCODE_MODE_CHANGE:
                if (myKeyboard.getViewType() == 0) {
                    changeView(1);
                } else {
                    changeView(0);
                }
                break;
            case MyKeyboard.KeyBoard.KEYCODE_SHIFT_123:
                changeView(1);
                break;
            case MyKeyboard.KeyBoard.KEYCODE_SHIFT_MORE:
                changeView(2);
                break;
            case MyKeyboard.KeyBoard.KEYCODE_DELETE:
                getCurrentInputConnection().deleteSurroundingText(1, 0);
                break;
            case MyKeyboard.KeyBoard.KEYCODE_DONE:
                getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_DONE);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(myKeyboard.getWindowToken(), 0);
                break;
            default:
                char value;
                if (myKeyboard.getShiiftType() == 1) {
                    value = Character.toUpperCase((char) primaryCode);
                } else {
                    value = (char) primaryCode;
                }
                getCurrentInputConnection().commitText(String.valueOf(value), 1);
        }

    }

    private void changeView(int i) {
        myKeyboard.setViewType(i);
        myKeyboard.setShiiftType(i);
        myKeyboard.setKeyboard(new MyKeyboard.KeyBoard(this, ViewXmls[i]));
    }

    @Override
    public void onText(CharSequence text) {

    }

    @Override
    public void swipeLeft() {

    }

    @Override
    public void swipeRight() {

    }

    @Override
    public void swipeDown() {

    }

    @Override
    public void swipeUp() {

    }
}