// 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.History;
import com.google.gwt.user.client.HistoryListener;
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, HistoryListener {
  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);
    if (!url.equals(History.getToken())) {
      History.newItem(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 onHistoryChanged(String historyToken) {
      if (null == historyToken || "".equals(historyToken))
        return;
      loadSource(historyToken);
  }

  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);
      }
    });
    History.addHistoryListener(this);
    History.fireCurrentHistoryState();
  }
}

@@ -807,35 +807,116 @@
 gle.gwt.
-user.client.History
+event.logical.shared.ValueChangeEvent;%0Aimport com.google.gwt.event.logical.shared.ValueChangeHandler
 ;%0Aimport
@@ -950,24 +950,16 @@
 .History
-Listener
 ;%0Aimport
@@ -1199,31 +1199,42 @@
 yPoint, 
-HistoryListener
+ValueChangeHandler%3CString%3E
  %7B%0A  pri
@@ -4060,48 +4060,100 @@
 d on
-HistoryChanged(String historyToken) %7B%0A  
+ValueChange(ValueChangeEvent%3CString%3E change) %7B%0A    String historyToken = change.getValue();%0A
     
@@ -4207,34 +4207,32 @@
 ken))%0A      
-  
 return;%0A
       loadSo
@@ -4215,26 +4215,24 @@
     return;%0A
-  
     loadSour
@@ -4728,21 +4728,24 @@
 .add
-HistoryListen
+ValueChangeHandl
 er(t

fix deprecation warnings http://codereview.appspot.com/4282055