package com.genlot.ninglottery.widget;

import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.EditText;

import com.genlot.ninglottery.R;

import java.lang.reflect.Method;

public class FeatureCodeKeyboard {

    private KeyCodeFinishListener mListener;
    private KeyboardView mKeyboardView;
    private Keyboard mKeyboard;
    private EditText mEditText;
    private boolean isShow = false;
    private ViewGroup mRootView;

    public FeatureCodeKeyboard(Context context, EditText editText, ViewGroup viewGroup, boolean setFocusEvent) {
        mEditText = editText;
        mKeyboard = new Keyboard(context, R.xml.keyboard_3);
        mKeyboardView = (KeyboardView) LayoutInflater.from(context).inflate(R.layout.view_keyboard2, null);
        mKeyboardView.setKeyboard(mKeyboard);
        mKeyboardView.setEnabled(true);
        mKeyboardView.setPreviewEnabled(false);
        mKeyboardView.setOnKeyboardActionListener(mKeyboardListener);
        mRootView = viewGroup;
        if(setFocusEvent){
            mEditText.setOnFocusChangeListener((v, hasFocus) -> {
                if (hasFocus) {
                    hideSystemSoftKeyboard(mEditText);
                    v.postDelayed(() -> showKeyboard(),300);
                } else {
                    hideKeyboard();
                }
            });
        }
    }

    private void hideSystemSoftKeyboard(EditText edit) {
        int ver = android.os.Build.VERSION.SDK_INT;
        String methodName = null;
        if (ver >= 16) {
            methodName = "setShowSoftInputOnFocus";
        } else if (ver >= 14) {
            methodName = "setSoftInputShownOnFocus";
        }

        if (methodName == null) {
            edit.setInputType(InputType.TYPE_NULL);
        } else {
            Class<EditText> editText = EditText.class;
            Method method = null;
            try {
                method = editText.getMethod(methodName, boolean.class);
                method.setAccessible(true);
                method.invoke(edit, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private KeyboardView.OnKeyboardActionListener mKeyboardListener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int primaryCode) {
        }

        @Override
        public void onRelease(int primaryCode) {
        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable editable = mEditText.getText();
            int start = mEditText.getSelectionStart();
            switch (primaryCode) {
                case Keyboard.KEYCODE_DELETE:
                    if (mEditText.hasSelection()) {
                        editable.clear();
                    } else {
                        if (editable != null && editable.length() > 0) {
                            if (start > 0) {
                                editable.delete(start - 1, start);
                            }
                        }
                    }
                    break;
                case Keyboard.KEYCODE_DONE:
                    if (mListener != null) {
                        mListener.onKeyCodeFinish();
                    }
                    break;
                case -25:
                    if (mListener != null) {
                        mListener.onKeyClear();
                    }
                    break;
                default:
                    if (mEditText.hasSelection()) {
                        editable.clear();
                    }
                    editable.insert(start, Character.toString((char) primaryCode));
                    break;
            }
        }

        @Override
        public void onText(CharSequence text) {
        }

        @Override
        public void swipeLeft() {
        }

        @Override
        public void swipeRight() {
        }

        @Override
        public void swipeDown() {
        }

        @Override
        public void swipeUp() {
        }
    };

    public interface KeyCodeFinishListener {
        void onKeyCodeFinish();

        void onKeyClear();
    }

    public void setOnKeyCodeFinish(KeyCodeFinishListener listener) {
        mListener = listener;
    }

    public void hideKeyboard() {
        if (mRootView != null && mKeyboardView != null && isShow) {
            isShow = false;
            mRootView.removeView(mKeyboardView);
        }
    }

    public void showKeyboard() {
        hideSystemSoftKeyboard(mEditText);
        if (!isShow) {
            mRootView.addView(mKeyboardView);
            isShow = true;
        }
    }

    public void changeEditText(EditText editText){
        mEditText = editText;
    }
}
