package com.keyborad.theme.trendyborad.trendykeyboardhelper;

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

import android.media.MediaPlayer;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.ImageView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.webp.decoder.WebpDrawable;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.keyborad.theme.trendyborad.TrendyApp;
import com.keyborad.theme.trendyborad.R;
import com.keyborad.theme.trendyborad.trendyutils.TrendyKeyNames;
import com.keyborad.theme.trendyborad.trendyutils.TrendyCommon;
import com.keyborad.theme.trendyborad.trendyutils.TrendySaveCurrentTheme;
import com.keyborad.theme.trendyborad.strendyourcecode.TrendyCodeKeyBoard;
import com.keyborad.theme.trendyborad.strendyourcecode.TrendyCodeKeyBoardView;


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

public class TrendyMyInputMethodService extends InputMethodService implements TrendyCodeKeyBoardView.OnKeyboardActionListener {


    private TrendyMyKeyBoardViewTrendy myKeyBoardView;
    private TrendyCodeKeyBoard mKeyBoard;
    private View parentView;


    private ImageView imBG;
    private VideoView videoView;
    
    private int a = R.xml.xml_one;
    private int b= R.xml.xml_two;
    private int c = R.xml.xml_three;

    private int curImeAction = EditorInfo.IME_ACTION_UNSPECIFIED;
    @SuppressLint("InflateParams")
    @Override
    public View onCreateInputView() {
        parentView = getLayoutInflater().inflate(R.layout.default_keyboard_input_view, null);
        findView();
        return parentView;
    }

    private void findView() {

        imBG = parentView.findViewById(R.id.gif_bg);
        videoView = parentView.findViewById(R.id.video_view);
        mKeyBoard = new TrendyCodeKeyBoard(this, a);
        myKeyBoardView = parentView.findViewById(R.id.custom_input_view);
        myKeyBoardView.setEnabled(true);
        myKeyBoardView.setPreviewEnabled(false);
        myKeyBoardView.setKeyboard(mKeyBoard);
        myKeyBoardView.setOnKeyboardActionListener(this);
    }


    @Override
    public void onWindowHidden() {
        super.onWindowHidden();
        if(videoView.isPlaying()){
            videoView.pause();

        }
    }

    @Override
    public void onWindowShown() {
        super.onWindowShown();
        EditorInfo currentInputEditorInfo = getCurrentInputEditorInfo();
        curImeAction = TrendyCommon.INSTANCE.getTextForImeAction(currentInputEditorInfo.imeOptions);
        String skinPath = TrendySaveCurrentTheme.INSTANCE.getSkinPath();

        if(skinPath == null || skinPath.isEmpty()){
            Log.d(TrendyApp.TAG, "---------skinPath= bull");
            myKeyBoardView.updateUi(curImeAction);
        }else {
            Log.d(TrendyApp.TAG, "---------skinPath= 1111");
            com.keyborad.theme.trendyborad.trendykeyboardhelper.TrendyKeyFunctions.INSTANCE.readBgOrVideo(this, new Function2<String, Drawable, Unit>() {
                @Override
                public Unit invoke(String s, Drawable drawable) {
                    Log.d(TrendyApp.TAG, "---------s= "+s+"---------drawable="+drawable);
                    if (s != null) {
                        myKeyBoardView.setBackground(null);
                        if(s.endsWith(".gif")){
                            imBG.setVisibility(View.VISIBLE);
                            videoView.setVisibility(View.GONE);
                            Glide.with(TrendyMyInputMethodService.this)
                                    .load(s)
                                    .addListener(new RequestListener<Drawable>() {
                                        @Override
                                        public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                                            return false;
                                        }

                                        @Override
                                        public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                                            if (resource instanceof WebpDrawable) {
                                                ((WebpDrawable) resource).setLoopCount(WebpDrawable.LOOP_FOREVER);
                                            }
                                            return false;
                                        }
                                    }).into(imBG) ;
                        }else if(s.endsWith(".mp4")){
                            imBG.setVisibility(View.GONE);
                            videoView.setVisibility(View.VISIBLE);
                            videoView.setVideoPath(s);
                            videoView.start();
                            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                @Override
                                public void onPrepared(MediaPlayer mp) {
                                    mp.setLooping(true);
                                }
                            });
                        }

                    } else {
                        myKeyBoardView.setBackground(drawable);
                    }
                    myKeyBoardView.updateUi(curImeAction);
                    return null;
                }
            });
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        videoView.stopPlayback();
    }

    @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 TrendyKeyNames.KEY_CODE_DELETE:
                curInputConnect.deleteSurroundingText(1, 0);
                break;
            case TrendyKeyNames.KEY_CODE_SHIFT:
                switchShift();
                break;
            case TrendyKeyNames.KEY_CODE_NUMBER_SHIFT:
            case TrendyKeyNames.KEY_CODE_SYMBOL_SHIFT:
                switchMoreOrNumber();
                break;
            case TrendyKeyNames.KEY_CODE_CHANGE_NUMBER:
            case TrendyKeyNames.KEY_CODE_BACK:
                switchNormalOrNumber();
                break;

            case TrendyKeyNames.KEY_CODE_COMPLETE:
            case TrendyKeyNames.KEY_CODE_CANCEL:
                curInputConnect.performEditorAction(curImeAction);
//                curInputConnect.performEditorAction(EditorInfo.IME_ACTION_DONE);
                break;
            default:
                String codeToChar = com.keyborad.theme.trendyborad.trendykeyboardhelper.TrendyKeyFunctions.INSTANCE.primaryCodeToChar(primaryCode);
                curInputConnect.commitText(codeToChar, 1);
                if (myKeyBoardView.isLowerCase() == 1) {
                    //自动转小写
                    myKeyBoardView.setLowerCase(0);
                    com.keyborad.theme.trendyborad.trendykeyboardhelper.TrendyKeyFunctions.INSTANCE.keyToLowerCase(mKeyBoard);
                    myKeyBoardView.setKeyboard(mKeyBoard);
                }
                break;

        }


    }

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

    }

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

    }

    private void switchShift() {
        int lowerCase = myKeyBoardView.isLowerCase();
        switch (lowerCase) {
            case 0:
                //当前小写转大写
                myKeyBoardView.setLowerCase(1);
                com.keyborad.theme.trendyborad.trendykeyboardhelper.TrendyKeyFunctions.INSTANCE.keyToUpper(mKeyBoard);
                myKeyBoardView.setKeyboard(mKeyBoard);
                break;
            case 1:
                //当前大写转锁定大写
                myKeyBoardView.setLowerCase(2);
                break;
            case 2:
                //当前锁定大写转小写
                myKeyBoardView.setLowerCase(0);
                TrendyKeyFunctions.INSTANCE.keyToLowerCase(mKeyBoard);
                myKeyBoardView.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() {

    }


}
