/**
 * The MIT License (MIT)
 * 
 * Copyright (c) 2014 mcflugen
 * 
 * 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 edu.colorado.csdms.wmt.client.ui.handler;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.user.client.Window;

import edu.colorado.csdms.wmt.client.Constants;
import edu.colorado.csdms.wmt.client.control.DataManager;
import edu.colorado.csdms.wmt.client.control.DataTransfer;
import edu.colorado.csdms.wmt.client.ui.widgets.QuestionDialogBox;

/**
 * Handles login and logout events in the WMT client.
 * 
 * @author Mark Piper (mark.piper@colorado.edu)
 */
public class AuthenticationHandler implements ClickHandler {

  private DataManager data;

  /**
   * Creates a new {@link AuthenticationHandler}.
   * 
   * @param data the DataManager object for the WMT session
   */
  public AuthenticationHandler(DataManager data) {
    this.data = data;
  }

  /**
   * Prepares and performs the WMT sign-in action.
   */
  public void signIn() {

    // Get WMT username and password from the SignInScreen.
    String username = data.getSignInScreen().getEmailBox().getText();
    data.security.setWmtUsername(username);
    GWT.log("Email: " + data.security.getWmtUsername());
    String password = data.getSignInScreen().getPasswordBox().getText();
    data.security.setWmtPassword(password);
    GWT.log("Password: " + data.security.getWmtPassword());

    // A very basic input check.
    if (username.isEmpty() || password.isEmpty() || !username.contains("@")
        || !username.contains(".")) {
          Window.alert(Constants.LOGIN_ERR);
          return;
    }

    // Authenticate the user with the server.
    DataTransfer.login(data);
  }
  
  /**
   * Prepares and performs the WMT sign-out action.
   */
  public void signOut() {

    final QuestionDialogBox questionDialog =
        new QuestionDialogBox(Constants.QUESTION_SIGN_OUT);
    questionDialog.getChoicePanel().getOkButton().setHTML(Constants.SIGN_OUT);

    // Define handlers.
    ClickHandler okHandler = new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        DataTransfer.logout(data);
        questionDialog.hide();
      }
    };
    DialogCancelHandler cancelHandler = new DialogCancelHandler(questionDialog);

    // Apply handlers to OK and Cancel buttons.
    questionDialog.getChoicePanel().getOkButton().addClickHandler(okHandler);
    questionDialog.getChoicePanel().getCancelButton().addClickHandler(
        cancelHandler);

    // Also apply handlers to "Enter" and "Esc" keys.
    questionDialog.addDomHandler(new ModalKeyHandler(okHandler, cancelHandler),
        KeyDownEvent.getType());

    questionDialog.center();
    questionDialog.getChoicePanel().getOkButton().setFocus(true);
  }
  
  @Override
  public void onClick(ClickEvent event) {

    if (data.security.isLoggedIn()) {

//      final QuestionDialogBox questionDialog =
//          new QuestionDialogBox(Constants.QUESTION_SIGN_OUT);
//      questionDialog.getChoicePanel().getOkButton().setHTML(Constants.SIGN_OUT);
//
//      // Define handlers.
//      ClickHandler okHandler = new ClickHandler() {
//        @Override
//        public void onClick(ClickEvent event) {
//          DataTransfer.logout(data);
//          questionDialog.hide();
//        }
//      };
//      DialogCancelHandler cancelHandler =
//          new DialogCancelHandler(questionDialog);
//
//      // Apply handlers to OK and Cancel buttons.
//      questionDialog.getChoicePanel().getOkButton().addClickHandler(okHandler);
//      questionDialog.getChoicePanel().getCancelButton().addClickHandler(
//          cancelHandler);
//
//      // Also apply handlers to "Enter" and "Esc" keys.
//      questionDialog
//          .addDomHandler(new ModalKeyHandler(okHandler, cancelHandler),
//              KeyDownEvent.getType());
//
//      questionDialog.center();
//      questionDialog.getChoicePanel().getOkButton().setFocus(true);

    }
  }
}

@@ -1815,32 +1815,8 @@
 ler 
-implements ClickHandler 
 %7B%0A%0A 
@@ -3915,1204 +3915,6 @@
   %7D%0A
-  %0A  @Override%0A  public void onClick(ClickEvent event) %7B%0A%0A    if (data.security.isLoggedIn()) %7B%0A%0A//      final QuestionDialogBox questionDialog =%0A//          new QuestionDialogBox(Constants.QUESTION_SIGN_OUT);%0A//      questionDialog.getChoicePanel().getOkButton().setHTML(Constants.SIGN_OUT);%0A//%0A//      // Define handlers.%0A//      ClickHandler okHandler = new ClickHandler() %7B%0A//        @Override%0A//        public void onClick(ClickEvent event) %7B%0A//          DataTransfer.logout(data);%0A//          questionDialog.hide();%0A//        %7D%0A//      %7D;%0A//      DialogCancelHandler cancelHandler =%0A//          new DialogCancelHandler(questionDialog);%0A//%0A//      // Apply handlers to OK and Cancel buttons.%0A//      questionDialog.getChoicePanel().getOkButton().addClickHandler(okHandler);%0A//      questionDialog.getChoicePanel().getCancelButton().addClickHandler(%0A//          cancelHandler);%0A//%0A//      // Also apply handlers to %22Enter%22 and %22Esc%22 keys.%0A//      questionDialog%0A//          .addDomHandler(new ModalKeyHandler(okHandler, cancelHandler),%0A//              KeyDownEvent.getType());%0A//%0A//      questionDialog.center();%0A//      questionDialog.getChoicePanel().getOkButton().setFocus(true);%0A%0A    %7D%0A  %7D%0A
 %7D%0A

Clean up AuthenticationHandler