package com.florianf.gwtcordova.client;

import com.florianf.gwtcordova.client.elemental.Document;
import com.florianf.gwtcordova.client.elemental.EventListener;
import com.florianf.gwtcordova.client.plugin.camera.Camera;
import com.florianf.gwtcordova.client.plugin.device.Device;
import com.florianf.gwtcordova.client.plugin.dialogs.Notification;
import com.florianf.gwtcordova.client.plugin.network.Connection;
import com.florianf.gwtcordova.client.plugin.statusbar.StatusBar;

/**
 * Created by florian on 09.08.15.
 */
public abstract class Cordova {

//    <script type="text/javascript" src="cordova.js"></script>

//    var requester ='http://192.168.0.17:9876/recompile-requester/gwtcordovashowcase';
//    var tag = document.createElement('script');
//    tag.src = requester;
//    document.head.insertBefore(tag, document.head.firstElementChild);

    private static boolean deviceReady;

    /*
     * This is necessary until JSInterop 1.0 is ready.
     */
    public static native Device getDevice()/*-{
        return $wnd.device;
    }-*/;

    public static native Connection getConnection()/*-{
        return $wnd.navigator.connection;
    }-*/;

    public static native Notification getNotification()/*-{
        return $wnd.navigator.notification;
    }-*/;

    public static native StatusBar getStatusBar()/*-{
        return $wnd.StatusBar;
    }-*/;

    public static native Camera getCamera()/*-{
        return $wnd.navigator.camera;
    }-*/;

    // same as in com.google.gwt.dom.client.Document;
    private static native Document getDocument() /*-{
        return $doc;
    }-*/;

    // gwtphonegap
    public static native boolean isDeviceAvailable() /*-{
        return typeof(device) != 'undefined';
    }-*/;

    /**
     * from gwt-api-generator:
     * Returns the JsInterop instance of Document
     */

    public static void init() {

        getDocument().addEventListener("deviceready", event -> {
            deviceReady = true;
        });
    }

    public static void init(EventListener eventListener) {
        getDocument().addEventListener("deviceready", eventListener);
        init();
    }

    public static boolean isDeviceReady() {
        return deviceReady;
    }
}

@@ -86,79 +86,9 @@
 tal.
-Document;%0Aimport com.florianf.gwtcordova.client.elemental.EventListener
+*
 ;%0Aim
@@ -897,511 +897,8 @@
   */
-%0A    public static native Device getDevice()/*-%7B%0A        return $wnd.device;%0A    %7D-*/;%0A%0A    public static native Connection getConnection()/*-%7B%0A        return $wnd.navigator.connection;%0A    %7D-*/;%0A%0A    public static native Notification getNotification()/*-%7B%0A        return $wnd.navigator.notification;%0A    %7D-*/;%0A%0A    public static native StatusBar getStatusBar()/*-%7B%0A        return $wnd.StatusBar;%0A    %7D-*/;%0A%0A    public static native Camera getCamera()/*-%7B%0A        return $wnd.navigator.camera;%0A    %7D-*/;
 %0A%0A  
@@ -1300,16 +1300,123 @@
 it() %7B%0A%0A
+        com.florianf.gwtcordova.client.myelemental.Document.addEventListener(%22deviceready%22, event1 -%3E %7B%7D);%0A
         
@@ -1668,23 +1668,608 @@
 init();%0A
-
     %7D%0A%0A
+    public static void onReady(com.florianf.gwtcordova.client.myelemental.EventListener eventListener) %7B%0A        if (deviceReady) %7B%0A           eventListener.handleEvent(new Event() %7B%0A               @Override%0A               public Detail getDetail() %7B%0A                   return null;%0A               %7D%0A%0A               @Override%0A               public EventTarget getTarget() %7B%0A                   return null;%0A               %7D%0A           %7D);%0A        %7D else %7B%0A            com.florianf.gwtcordova.client.myelemental.Document.addEventListener(%22deviceready%22, eventListener);%0A        %7D%0A    %7D%0A%0A%0A
     publ

remove old syntax stuff; add onReady Method