package im.actor.core.js.modules;

import im.actor.core.js.JsMessenger;
import im.actor.core.modules.AbsModule;
import im.actor.core.modules.ModuleActor;
import im.actor.core.modules.ModuleContext;
import im.actor.runtime.actors.ActorCreator;
import im.actor.runtime.actors.ActorRef;
import im.actor.runtime.actors.Cancellable;
import im.actor.runtime.actors.Props;

import static im.actor.runtime.actors.ActorSystem.system;

public class JsIdleModule extends AbsModule {

    private static final long TIMEOUT = 3 * 60 * 1000/*3 min*/;

    private JsMessenger messenger;
    private ActorRef idleActor;

    public JsIdleModule(final JsMessenger messenger, final ModuleContext context) {
        super(context);

        this.messenger = messenger;

        this.messenger.onAppVisible();

        idleActor = system().actorOf(Props.create(new ActorCreator() {
            @Override
            public IdleActor create() {
                return new IdleActor(messenger, context);
            }
        }), "js/idle_timer");
    }

    public void onHidden() {
        idleActor.send(new IdleActor.OnHidden());
    }

    public void onVisible() {
        idleActor.send(new IdleActor.OnVisible());
    }

    private static class IdleActor extends ModuleActor implements JsIdleCallback {

        private boolean isAppVisible = true;
        private JsMessenger messenger;
        private Cancellable flushCancellable;

        public IdleActor(JsMessenger messenger, ModuleContext context) {
            super(context);
            this.messenger = messenger;
        }

        @Override
        public void preStart() {
            // Log.d("JsIdle", "preStart");
            JsIdleDetection.subscribe(this);
            scheduleFlush();
        }

        public void onActionDetected() {
            // Log.d("JsIdle", "onActionDetected");
            if (!isAppVisible) {
                isAppVisible = true;
                messenger.onAppVisible();
            }
            scheduleFlush();
        }

        void scheduleFlush() {
            if (flushCancellable != null) {
                flushCancellable.cancel();
                flushCancellable = null;
            }
            flushCancellable = schedule(new FlushTimeout(), TIMEOUT);
        }

        public void onTimeoutDetected() {
            // Log.d("JsIdle", "onTimeoutDetected");
            if (isAppVisible) {
                isAppVisible = false;
                messenger.onAppHidden();
            }
        }

        public void onHidden() {
            // Log.d("JsIdle", "onHidden");
            onTimeoutDetected();
        }

        public void onVisible() {
            // Log.d("JsIdle", "onVisible");
            onActionDetected();
        }

        @Override
        public void onReceive(Object message) {
            if (message instanceof FlushTimeout) {
                onTimeoutDetected();
            } else if (message instanceof OnHidden) {
                onHidden();
            } else if (message instanceof OnVisible) {
                onVisible();
            } else {
                super.onReceive(message);
            }
        }

        private class FlushTimeout {

        }

        public static class OnHidden {

        }

        public static class OnVisible {

        }
    }
}

@@ -65,16 +65,74 @@
 senger;%0A
+import im.actor.core.js.providers.electron.JsElectronApp;%0A
 import i
@@ -884,203 +884,64 @@
 rOf(
-Props.create(new ActorCreator() %7B%0A            @Override%0A            public IdleActor create() %7B%0A                return new IdleActor(messenger, context);%0A            %7D%0A        %7D), %22js/idle_timer%22
+%22js/idle_timer%22, () -%3E new IdleActor(messenger, context)
 );%0A 
@@ -1333,16 +1333,81 @@
 ellable;
+%0A        private boolean isElectron = JsElectronApp.isElectron();
 %0A%0A      
@@ -1619,40 +1619,31 @@
     
-// Log.d(%22JsIdle%22, %22preStart%22);%0A
+if (!isElectron) %7B%0A    
     
@@ -1687,32 +1687,36 @@
 s);%0A            
+    
 scheduleFlush();
@@ -1708,32 +1708,46 @@
 cheduleFlush();%0A
+            %7D%0A
         %7D%0A%0A     
@@ -1786,60 +1786,8 @@
 ) %7B%0A
-            // Log.d(%22JsIdle%22, %22onActionDetected%22);%0A
     
@@ -1900,32 +1900,67 @@
 ;%0A            %7D%0A
+            if (!isElectron) %7B%0A    
             sche
@@ -1964,32 +1964,46 @@
 cheduleFlush();%0A
+            %7D%0A
         %7D%0A%0A     
@@ -2297,61 +2297,8 @@
 ) %7B%0A
-            // Log.d(%22JsIdle%22, %22onTimeoutDetected%22);%0A
     
@@ -2466,52 +2466,8 @@
 ) %7B%0A
-            // Log.d(%22JsIdle%22, %22onHidden%22);%0A
     
@@ -2544,53 +2544,8 @@
 ) %7B%0A
-            // Log.d(%22JsIdle%22, %22onVisible%22);%0A
     

Disable IDLE detection on Electron