package com.key.vibekeyboard.inputmethod.service;

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;

import com.key.vibekeyboard.R;
import com.key.vibekeyboard.inputmethod.view.CustomKeyboardView;

public class MyInputMethodService extends InputMethodService implements KeyboardView.OnKeyboardActionListener {

    private Keyboard keyboardLowercase;
    private Keyboard keyboardUppercase;
    private Keyboard keyboardSymbols;
    private Keyboard keyboardMoreSymbols;
    private Keyboard currentKeyboard;

    private CustomKeyboardView customKeyboardView;

    private static final int KEYCODE_MORE_SYMBOLS = -1000;

    @Override
    public View onCreateInputView() {
        customKeyboardView = (CustomKeyboardView) getLayoutInflater().inflate(R.layout.custom_keyboard_view, null, false);

        initializeKeyboards();
        setUpCustomKeyboardView();

        return customKeyboardView;
    }

    private void initializeKeyboards() {
        keyboardLowercase = new Keyboard(this, R.xml.keyboard_lowercase);
        keyboardUppercase = new Keyboard(this, R.xml.keyboard_uppercase);
        keyboardSymbols = new Keyboard(this, R.xml.keyboard_symbols);
        keyboardMoreSymbols = new Keyboard(this, R.xml.keyboard_more_symbols);

        currentKeyboard = keyboardLowercase;
    }

    private void setUpCustomKeyboardView() {
        customKeyboardView.setKeyboard(currentKeyboard);
        customKeyboardView.setOnKeyboardActionListener(this);
    }

    @Override
    public void onWindowShown() {
        super.onWindowShown();
        updateKeyboardBackground(); // 更新背景
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection inputConnection = getCurrentInputConnection();
        if (inputConnection == null) return;

        switch (primaryCode) {
            case Keyboard.KEYCODE_DELETE:
                handleDelete(inputConnection);
                break;

            case Keyboard.KEYCODE_SHIFT:
                toggleCaseMode();
                break;

            case Keyboard.KEYCODE_MODE_CHANGE:
                toggleKeyboardLayout();
                break;

            case Keyboard.KEYCODE_DONE:
                performActionBasedOnEditorInfo(inputConnection);
                break;

            case KEYCODE_MORE_SYMBOLS:
                toggleMoreSymbolsKeyboard();
                break;

            default:
                handleCharacterInput(primaryCode, inputConnection);
                break;
        }
    }

    private void handleDelete(InputConnection inputConnection) {
        inputConnection.deleteSurroundingText(1, 0);
    }

    private void toggleCaseMode() {
        if (currentKeyboard == keyboardLowercase) {
            switchToNewKeyboard(keyboardUppercase);
            customKeyboardView.setShiftState(CustomKeyboardView.ShiftState.CAPS);
        } else {
            switchToNewKeyboard(keyboardLowercase);
            customKeyboardView.setShiftState(CustomKeyboardView.ShiftState.NORMAL);
        }
    }

    private void toggleKeyboardLayout() {
        if (currentKeyboard == keyboardLowercase || currentKeyboard == keyboardUppercase) {
            switchToNewKeyboard(keyboardSymbols);
        } else if (currentKeyboard == keyboardSymbols || currentKeyboard == keyboardMoreSymbols) {
            switchToNewKeyboard(keyboardLowercase);
        }
    }

    private void switchToNewKeyboard(Keyboard newKeyboard) {
        if (currentKeyboard != newKeyboard) {
            currentKeyboard = newKeyboard;
            customKeyboardView.setKeyboard(currentKeyboard);
            updateKeyboardBackground();
        }
    }

    private void performActionBasedOnEditorInfo(InputConnection inputConnection) {
        EditorInfo editorInfo = getCurrentInputEditorInfo();
        if ((editorInfo.imeOptions & EditorInfo.IME_ACTION_SEARCH) != 0) {
            inputConnection.performEditorAction(EditorInfo.IME_ACTION_SEARCH);
        } else if ((editorInfo.imeOptions & EditorInfo.IME_ACTION_DONE) != 0) {
            inputConnection.performEditorAction(EditorInfo.IME_ACTION_DONE);
        } else {
            inputConnection.performEditorAction(EditorInfo.IME_ACTION_UNSPECIFIED);
        }
    }

    private void toggleMoreSymbolsKeyboard() {
        switchToNewKeyboard(currentKeyboard == keyboardSymbols ? keyboardMoreSymbols : keyboardSymbols);
    }

    private void handleCharacterInput(int primaryCode, InputConnection inputConnection) {
        char code = (char) primaryCode;
        inputConnection.commitText(String.valueOf(code), 1);
    }

    private void updateKeyboardBackground() {
        customKeyboardView.updateKeyboardView(this);
        customKeyboardView.invalidate();
    }

    @Override
    public void onPress(int primaryCode) {
    }

    @Override
    public void onRelease(int primaryCode) {
    }

    @Override
    public void onText(CharSequence text) {
    }

    @Override
    public void swipeLeft() {
    }

    @Override
    public void swipeRight() {
    }

    @Override
    public void swipeDown() {
    }

    @Override
    public void swipeUp() {
    }
}

