// Copyright (C) 2009 Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.caja.demos.playground.client;

import com.google.caja.demos.playground.client.ui.PlaygroundView;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Main entry point for GWT-based Caja playground
 *
 * @author Jasvir Nagra (jasvir@gmail.com)
 */
public class Playground implements EntryPoint {
  private PlaygroundView gui;

  private PlaygroundServiceAsync cajolingService =
    GWT.create(PlaygroundService.class);

  public void loadSource(String url) {
    loadSource(Window.Location.getHref(), url);
  }

  public void loadSource(String base, String url) {
    gui.setLoading(true);
    gui.setUrl(url);
    cajolingService.fetch(base, url, new AsyncCallback<String>() {
      public void onFailure(Throwable caught) {
        gui.setLoading(false);
        gui.addCompileMessage(caught.getMessage());
        gui.selectTab(PlaygroundView.Tabs.COMPILE_WARNINGS);
      }

      public void onSuccess(String result) {
        gui.setLoading(false);
        gui.setOriginalSource(result);
        gui.setCajoledSource("", "");
        gui.selectTab(PlaygroundView.Tabs.SOURCE);
      }
    });
  }

  public void clearPolicy() {
    gui.setPolicySource("");
  }
  
  public void loadPolicy(String url) {
    gui.setLoading(true);
    gui.setPolicyUrl(url);
    cajolingService.fetch(Window.Location.getHref(), url,
        new AsyncCallback<String>() {
          public void onFailure(Throwable caught) {
            gui.setLoading(false);
            gui.addCompileMessage(caught.getMessage());
            gui.selectTab(PlaygroundView.Tabs.COMPILE_WARNINGS);
          }
    
          public void onSuccess(String result) {
            gui.setLoading(false);
            gui.setPolicySource(result);
            gui.selectTab(PlaygroundView.Tabs.POLICY);
          }
        });
  }

  public void cajole(String uri, String input, final String policy,
      boolean debugMode) {
    gui.setLoading(true);
    cajolingService.cajole(Window.Location.getHref(), uri, input, debugMode,
                           new AsyncCallback<CajolingServiceResult>() {
      public void onFailure(Throwable caught) {
        gui.setLoading(false);
        gui.addCompileMessage(caught.getMessage());
      }

      public void onSuccess(CajolingServiceResult result) {
        gui.setLoading(false);
        if (result == null) {
          gui.addCompileMessage("An unknown error occurred");
          gui.selectTab(PlaygroundView.Tabs.COMPILE_WARNINGS);
          return;
        }
        for (String message: result.getMessages()) {
          gui.addCompileMessage(message);
        }
        if (result.getHtml() != null) {
          gui.setCajoledSource(result.getHtml(), result.getJavascript());
          gui.setRenderedResult(policy, result.getHtml(), result.getJavascript());
          gui.selectTab(PlaygroundView.Tabs.RENDER);
        } else {
          gui.setCajoledSource(null, null);
          gui.setRenderedResult(null, null, null);
          gui.selectTab(PlaygroundView.Tabs.COMPILE_WARNINGS);
        }
      }
    });
  }

  public void onModuleLoad() {
    gui = new PlaygroundView(this);
    gui.setLoading(true);
    cajolingService.getBuildInfo(new AsyncCallback<String>() {
      public void onFailure(Throwable caught) {
        gui.setLoading(false);
        gui.addCompileMessage(caught.getMessage());
        gui.setVersion("Unknown");
      }

      public void onSuccess(String result) {
        gui.setLoading(false);
        gui.setVersion(result);
      }
    });
  }
}

@@ -785,16 +785,110 @@
 nt.GWT;%0A
+import com.google.gwt.user.client.History;%0Aimport com.google.gwt.user.client.HistoryListener;%0A
 import c
@@ -921,16 +921,16 @@
 Window;%0A
-
 import c
@@ -1124,16 +1124,33 @@
 tryPoint
+, HistoryListener
  %7B%0A  pri
@@ -1430,32 +1430,32 @@
 tLoading(true);%0A
-
     gui.setUrl(u
@@ -1451,32 +1451,109 @@
 ui.setUrl(url);%0A
+    if (!url.equals(History.getToken())) %7B%0A      History.newItem(url);%0A    %7D%0A
     cajolingServ
@@ -3954,32 +3954,198 @@
  %7D%0A    %7D);%0A  %7D%0A%0A
+  public void onHistoryChanged(String historyToken) %7B%0A      if (null == historyToken %7C%7C %22%22.equals(historyToken))%0A        return;%0A      loadSource(historyToken);%0A  %7D%0A%0A
   public void on
@@ -4579,18 +4579,95 @@
   %7D%0A    %7D);%0A
+    History.addHistoryListener(this);%0A    History.fireCurrentHistoryState();%0A
   %7D%0A%7D%0A

Support browser history (back and forward buttons) in the playground Allow easier to share links to examples in the playground eg. http://caja.appspot.com/#http://www.thinkfu.com/clock.html