package com.genlot.ninglottery.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import com.genlot.ninglottery.R;


/**
 * @author ZhuYongKang
 * @description 带键盘的EditText
 * @date 2019/8/19
 */
@SuppressLint("AppCompatCustomView")
public class KeyboardEditText extends EditText {
    private TextFinishListener mTextEnterListener = null;
    private int mKeyboardType;
    private int mShowLocation;
    boolean isAutoHide;
    private KeyboardManager keyboardManager;

    public KeyboardEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.KeyboardEditText);
        mKeyboardType = ta.getInt(R.styleable.KeyboardEditText_keyboard_type, KeyboardManager.KEYBOARD_FOUR);
        mShowLocation = ta.getInt(R.styleable.KeyboardEditText_keyboard_location, KeyboardManager.DIRECTON_RIGHT_DOWN);
        isAutoHide = ta.getBoolean(R.styleable.KeyboardEditText_isAutoHide, true);
        ta.recycle();
        setOnTouchListener(_touchListener);
        setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
        setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                KeyboardEditText.this.setSelection(KeyboardEditText.this.getText().toString().length());
            }
        });
        keyboardManager = new KeyboardManager(context);
        keyboardManager.setOnKeyboardDismiss(() -> {
            if (mTextEnterListener != null) {
                mTextEnterListener.onTextEnter(this.getText().toString().trim());
            }
            clearFocus();
        });
        setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }
        });
    }


    public void showKeyboard() {
        if (keyboardManager != null) {
            keyboardManager.setAutoHideMode(isAutoHide);
            keyboardManager.setKeyboardType(getContext(), mKeyboardType);
            keyboardManager.showKeyboard(KeyboardEditText.this, mShowLocation);
        }
    }

    public void closeKeyboard() {
        if (keyboardManager != null) {
            keyboardManager.closeKeyboard();
        }
    }

    private OnTouchListener _touchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                if (!keyboardManager.isShowing()) {
                    showKeyboard();
                }
            }
            return false;
        }
    };

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_UP) {
            KeyboardEditText.this.setSelection(KeyboardEditText.this.getText().toString().length());
        }
        return true;
    }

    public void setKeyboardType(int type) {
        mKeyboardType = type;
    }

    public interface TextFinishListener {
        void onTextEnter(String value);
    }

    public void setOnTextFinishListener(TextFinishListener listener) {
        mTextEnterListener = listener;
    }
}
