/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 Sri Harsha Chilakapati
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.shc.webgl4j.client;

/**
 * @author Sri Harsha Chilakapati
 */
public final class WEBGL_lose_context
{
    /* Prevent instantiation */
    private WEBGL_lose_context()
    {
    }

    public static boolean isSupported()
    {
        if (!WebGL10.isSupported())
            return false;

        if (!WebGL10.isContextCompatible())
            throw new IllegalStateException("You must have a WebGL context >= 1.0 to check if extension is supported.");

        for (String supportedExtension : WebGL10.glGetSupportedExtensions())
        {
            switch (supportedExtension)
            {
                case "WEBGL_lost_context":
                case "O_WEBGL_lost_context":
                case "IE_WEBGL_lost_context":
                case "MOZ_WEBGL_lost_context":
                case "WEBKIT_WEBGL_lost_context":
                    return true;
            }
        }

        return false;
    }

    public static void enableExtension()
    {
        if (!WebGL10.isContextCompatible())
            throw new IllegalStateException("You must have a WebGL context >= 1.0 to enable this extension.");

        if (!isSupported())
            throw new RuntimeException("This browser does not support the WEBGL_lost_context extension.");

        if (!isExtensionEnabled())
            nEnableExtension();
    }

    private static void checkExtension()
    {
        if (!WebGL10.isContextCompatible())
            throw new RuntimeException("You must create a WebGL context before accessing extension methods.");

        if (!isExtensionEnabled())
            throw new IllegalStateException("Extension must be enabled before using any members.");
    }

    public static void glLoseContext()
    {
        checkExtension();
        nglLoseContext();
    }

    public static void glRestoreContext()
    {
        checkExtension();
        nglRestoreContext();
    }

    public static native boolean isExtensionEnabled() /*-{
        return typeof ($wnd.gl.wlc_ext) !== 'undefined';
    }-*/;

    private static native void nEnableExtension() /*-{
        $wnd.gl.wlc_ext = $wnd.gl.getExtension('WEBGL_lost_context') ||
            $wnd.gl.getExtension('O_WEBGL_lost_context') ||
            $wnd.gl.getExtension('IE_WEBGL_lost_context') ||
            $wnd.gl.getExtension('MOZ_WEBGL_lost_context') ||
            $wnd.gl.getExtension('WEBKIT_WEBGL_lost_context');
    }-*/;

    private static native void nglLoseContext() /*-{
        $wnd.gl.wlc_ext.loseContext();
    }-*/;

    private static native void nglRestoreContext() /*-{
        $wnd.gl.wlc_ext.restoreContext();
    }-*/;
}

@@ -1786,33 +1786,33 @@
  case %22WEBGL_los
-t
+e
 _context%22:%0A     
@@ -1831,33 +1831,33 @@
 ase %22O_WEBGL_los
-t
+e
 _context%22:%0A     
@@ -1877,33 +1877,33 @@
 se %22IE_WEBGL_los
-t
+e
 _context%22:%0A     
@@ -1924,33 +1924,33 @@
 e %22MOZ_WEBGL_los
-t
+e
 _context%22:%0A     
@@ -1974,33 +1974,33 @@
 WEBKIT_WEBGL_los
-t
+e
 _context%22:%0A     
@@ -2387,33 +2387,33 @@
 rt the WEBGL_los
-t
+e
 _context extensi
@@ -3284,33 +3284,33 @@
 nsion('WEBGL_los
-t
+e
 _context') %7C%7C%0A  
@@ -3344,33 +3344,33 @@
 ion('O_WEBGL_los
-t
+e
 _context') %7C%7C%0A  
@@ -3405,33 +3405,33 @@
 on('IE_WEBGL_los
-t
+e
 _context') %7C%7C%0A  
@@ -3467,33 +3467,33 @@
 n('MOZ_WEBGL_los
-t
+e
 _context') %7C%7C%0A  
@@ -3540,17 +3540,17 @@
 EBGL_los
-t
+e
 _context

Fix typo in WEBGL_lose_context causing it not to be detected