package com.input.board.appskey.keyboard.customer;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.InputMethodService;

import android.media.MediaPlayer;
import android.view.SurfaceView;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;

import com.input.board.appskey.keyboard.R;
import com.input.board.appskey.keyboard.fileTools.SpSkin;


import kotlin.Unit;
import kotlin.jvm.functions.Function2;

public class CustomInputService extends InputMethodService implements MyKeyBoardView.OnKeyboardActionListener {


    private CustomInputView customInputView;
    private MyKeyBoard mKeyBoard;
    private View parentView;
    private SurfaceView surfaceView;
    private MediaPlayer curMediaPlayer;
    
    private int a = R.xml.a;
    private int b= R.xml.b;
    private int c = R.xml.c;

    @SuppressLint("InflateParams")
    @Override
    public View onCreateInputView() {
        parentView = getLayoutInflater().inflate(R.layout.custome_input_view, null);
        findView();
        return parentView;
    }

    private void findView() {
        surfaceView = parentView.findViewById(R.id.surfaceView);
        mKeyBoard = new MyKeyBoard(this, a);
        customInputView = parentView.findViewById(R.id.custom_input_view);
        customInputView.setEnabled(true);
        customInputView.setPreviewEnabled(false);
        customInputView.setKeyboard(mKeyBoard);
        customInputView.setOnKeyboardActionListener(this);
    }

    @Override
    public void onWindowShown() {
        super.onWindowShown();
        String skinPath = SpSkin.INSTANCE.getSkinPath();
        if(skinPath == null || skinPath.isEmpty()){
            customInputView.updateUi(surfaceView);
        }else {
            KeyFunction.INSTANCE.readBgOrVideo(this, surfaceView, new Function2<MediaPlayer, Drawable, Unit>() {
                @Override
                public Unit invoke(MediaPlayer mediaPlayer, Drawable drawable) {
                    if (mediaPlayer != null) {
                        curMediaPlayer = mediaPlayer;
                        curMediaPlayer.start();
                    } else {
                        customInputView.setBackground(drawable);
                    }
                    customInputView.updateUi(surfaceView);
                    return null;
                }
            });
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (curMediaPlayer != null) {
            curMediaPlayer.stop();
            curMediaPlayer.release();
            curMediaPlayer = null;
        }
    }

    @Override
    public void onPress(int primaryCode) {

    }

    @Override
    public void onRelease(int primaryCode) {

    }

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

        InputConnection curInputConnect = getCurrentInputConnection();
        switch (primaryCode) {
            case KeyCodeConst.KEY_CODE_DELETE:
                curInputConnect.deleteSurroundingText(1, 0);
                break;
            case KeyCodeConst.KEY_CODE_SHIFT:
                switchShift();
                break;
            case KeyCodeConst.KEY_CODE_NUMBER_SHIFT:
            case KeyCodeConst.KEY_CODE_SYMBOL_SHIFT:
                switchMoreOrNumber();
                break;
            case KeyCodeConst.KEY_CODE_CHANGE_NUMBER:
            case KeyCodeConst.KEY_CODE_BACK:
                switchNormalOrNumber();
                break;

            case KeyCodeConst.KEY_CODE_COMPLETE:
            case KeyCodeConst.KEY_CODE_CANCEL:
                curInputConnect.performEditorAction(EditorInfo.IME_ACTION_DONE);
                break;
            default:
                String codeToChar = KeyFunction.INSTANCE.primaryCodeToChar(primaryCode);
                curInputConnect.commitText(codeToChar, 1);
                if (customInputView.isLowerCase() == 1) {
                    //自动转小写
                    customInputView.setLowerCase(0);
                    KeyFunction.INSTANCE.keyToLowerCase(mKeyBoard);
                    customInputView.setKeyboard(mKeyBoard);
                }
                break;

        }


    }

    private void switchMoreOrNumber() {
        int mode = customInputView.getMode();
        switch (mode) {
            case 1:
                mKeyBoard = new MyKeyBoard(this, c);
                customInputView.setMode(2);
                customInputView.setKeyboard(mKeyBoard);
                break;
            case 2:
                mKeyBoard = new MyKeyBoard(this, b);
                customInputView.setMode(1);
                customInputView.setKeyboard(mKeyBoard);
                break;
        }

    }

    private void switchNormalOrNumber() {
        int mode = customInputView.getMode();
        switch (mode) {
            case 0:
                mKeyBoard = new MyKeyBoard(this, b);
                customInputView.setMode(1);
                customInputView.setKeyboard(mKeyBoard);
                break;
            case 1:
            case 2:
                mKeyBoard = new MyKeyBoard(this, a);
                customInputView.setMode(0);
                customInputView.setKeyboard(mKeyBoard);
                break;
        }

    }

    private void switchShift() {
        int lowerCase = customInputView.isLowerCase();
        switch (lowerCase) {
            case 0:
                //当前小写转大写
                customInputView.setLowerCase(1);
                KeyFunction.INSTANCE.keyToUpper(mKeyBoard);
                customInputView.setKeyboard(mKeyBoard);
                break;
            case 1:
                //当前大写转锁定大写
                customInputView.setLowerCase(2);
                break;
            case 2:
                //当前锁定大写转小写
                customInputView.setLowerCase(0);
                KeyFunction.INSTANCE.keyToLowerCase(mKeyBoard);
                customInputView.setKeyboard(mKeyBoard);
                break;
        }
    }


    @Override
    public void onText(CharSequence text) {

    }

    @Override
    public void swipeLeft() {

    }

    @Override
    public void swipeRight() {

    }

    @Override
    public void swipeDown() {

    }

    @Override
    public void swipeUp() {

    }


}
