package com.genlot.ninglottery.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;


import com.genlot.ninglottery.R;

import java.lang.reflect.Method;

/**
 * @author ZhuYongKang
 * @description 键盘管理类
 * @date 2019/8/19
 */
public class KeyboardManager {
    public static final int KEYBOARD_ONE = 1;
    public static final int KEYBOARD_TWO = 2;
    public static final int KEYBOARD_THREE = 3;
    public static final int KEYBOARD_FOUR = 4;
    //键盘显示位置
    public static final int DIRECTON_LEFT_UP = 0; //左上
    public static final int DIRECTON_LEFT_DOWN = 1; //左下
    public static final int DIRECTON_RIGHT_UP = 2;  //右上
    public static final int DIRECTON_RIGHT_DOWN = 3; //右下

    private KeyboardDismiss mListener;
    private KeyboardView mKbView;
    private Keyboard mKeyboard;
    private EditText mEditText;
    private PopupWindow mPopupWindow;
    private int mKeyboardType;

    public KeyboardManager(Context context) {
        mKeyboard = new Keyboard(context, R.xml.keyboard_4);
        mKbView = (KeyboardView) LayoutInflater.from(context).inflate(R.layout.view_keyboard, null);
        mKbView.setKeyboard(mKeyboard);
        mKbView.setEnabled(true);
        mKbView.setPreviewEnabled(false);
        mKbView.setOnKeyboardActionListener(_keyboardListener);
        mPopupWindow = new PopupWindow(mKbView, 230, 290);
        mPopupWindow.setOnDismissListener(() -> {
            if (mListener != null) {
                mListener.onKeyboardDismiss();
            }
        });
    }

    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 _keyboardListener = 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:
                    mPopupWindow.dismiss();
                    if (mListener != null) {
                        mListener.onKeyboardDismiss();
                    }
                    break;
                default:
                    if (mEditText.hasSelection()) {
                        editable.clear();
                    }
                    editable.insert(start, Character.toString((char) primaryCode));
            }
        }

        @Override
        public void onText(CharSequence text) {

        }

        @Override
        public void swipeLeft() {

        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeDown() {

        }

        @Override
        public void swipeUp() {

        }
    };

    public interface KeyboardDismiss {
        void onKeyboardDismiss();
    }

    public void setOnKeyboardDismiss(KeyboardDismiss listener) {
        mListener = listener;
    }

    public void closeKeyboard() {
        if (mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
            mEditText.clearFocus();
        }
    }

    public void setKeyboardType(Context context, int type) {
        mKeyboardType = type;
        if (type == KEYBOARD_ONE) {
            mKeyboard = null;
            mKeyboard = new Keyboard(context, R.xml.keyboard_1);
            mKbView.setKeyboard(mKeyboard);
        } else if (type == KEYBOARD_TWO) {
            mKeyboard = null;
            mKeyboard = new Keyboard(context, R.xml.keyboard_2);
            mKbView.setKeyboard(mKeyboard);
        } else if (type == KEYBOARD_THREE) {
            mKeyboard = null;
            mKeyboard = new Keyboard(context, R.xml.keyboard_3);
            mKbView.setKeyboard(mKeyboard);
        } else if (type == KEYBOARD_FOUR) {
            mKeyboard = null;
            mKeyboard = new Keyboard(context, R.xml.keyboard_4);
            mKbView.setKeyboard(mKeyboard);
        }
    }

    public void setAutoHideMode(boolean autoHide) {
        if (autoHide) {
            mPopupWindow.setTouchable(true);
            mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.update();
        } else {
            mPopupWindow.setOutsideTouchable(false);
            mPopupWindow.update();
        }
    }

    public boolean isShowing() {
        return mPopupWindow.isShowing();
    }

    public void showKeyboard(EditText editText,int direction) {
        hideSystemSoftKeyboard(editText);
        mEditText = editText;
        int[] location = new int[2];
        mEditText.getLocationInWindow(location);
        switch (direction) {
            case DIRECTON_RIGHT_DOWN:
                mPopupWindow.showAtLocation(mEditText, Gravity.NO_GRAVITY, location[0] + mEditText.getWidth(), location[1] + mEditText.getHeight());
                break;
            case DIRECTON_RIGHT_UP:
                mPopupWindow.showAtLocation(mEditText, Gravity.NO_GRAVITY, 0, 0);
                break;
            case DIRECTON_LEFT_DOWN:
                mPopupWindow.showAtLocation(mEditText, Gravity.NO_GRAVITY, 0, 0);
                break;
            case DIRECTON_LEFT_UP:
                mPopupWindow.showAtLocation(mEditText, Gravity.NO_GRAVITY, location[0], location[1] - 275);
                break;
        }
    }

}
