package com.xugame.gameconsole.dialog;

import android.app.Dialog;
import android.app.Instrumentation;
import android.content.Context;
import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;



public abstract class BaseDialog extends Dialog {
    protected int IDLE_TIME = 1000 * 30;

    protected boolean isFirstFocus = true;
    private CountDownTimer mTimerIdle = null;

    private ExecutorService mExecutorService;


    protected BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.setCancelable(false);

        this.hideNavigationBar();

        this.setCanceledOnTouchOutside(false);

        this.mExecutorService = Executors.newSingleThreadExecutor();
    }


    protected void stopIdleTimer() {
        if (this.mTimerIdle != null)
            this.mTimerIdle.cancel();
    }

    @Override
    public void show() {
        if (!isShowing())
             super.show();
    }

    @Override
    public void dismiss() {

        this.stopIdleTimer();
        super.dismiss();
    }


    protected void startIdleTimer() {
        if (this.mTimerIdle != null)
            this.mTimerIdle.cancel();
        else {
            this.mTimerIdle = new CountDownTimer(IDLE_TIME, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {

                }

                @Override
                public void onFinish() {
//                    onIdleTimeout();
                    startIdleTimer();
                }
            };
        }

        this.mTimerIdle.start();
    }

    protected BaseDialog(Context context) {
        super(context);
        this.setCancelable(false);
        this.hideNavigationBar();

        this.setCanceledOnTouchOutside(false);

        this.mExecutorService = Executors.newSingleThreadExecutor();
    }

    protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.setCancelable(false);
        this.hideNavigationBar();
    }

    private void hideNavigationBar() {
        int flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        this.getWindow().getDecorView().setSystemUiVisibility(flags);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

//    protected void focusSoundEffect() {
//        if (this.isFirstFocus)
//            this.isFirstFocus = false;
//        else
//            SoundManager.getInstance(this.getContext()).playSoundEffect(SoundType.NUM_FOCUS);
//
//    }

    protected void sendKeyCode(int keyCode, KeyEvent event) {
        KeyEvent keyEvent = new KeyEvent(event.getDownTime(),
                event.getEventTime(),
                event.getAction(),
                keyCode,
                event.getRepeatCount(),
                event.getMetaState(),
                event.getDeviceId(),
                -100,
                event.getFlags());
        sendKeyCode(keyEvent);
    }

    private void sendKeyCode(final KeyEvent event) {
        mExecutorService.execute(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    Instrumentation inst = new Instrumentation();
                    inst.sendKeySync(event);
                } catch (Exception e) {
                }
            }
        });
    }

    @Override
    public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
        int keyCode = event.getKeyCode();
        if (keyCode == 98 || keyCode == 99 || keyCode == 100 || keyCode == 101 || keyCode == 108) {
            return true;
        }
        if (event.getKeyCode() == KeyEvent.KEYCODE_BUTTON_B) {
            this.sendKeyCode(KeyEvent.KEYCODE_BACK, event);
            return true;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_BUTTON_A) {
            this.sendKeyCode(KeyEvent.KEYCODE_DPAD_CENTER, event);
            return true;
        }
        return super.dispatchKeyEvent(event);
    }
}
