package edu.mayo.phenoportal.client.navigation;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

import edu.mayo.phenoportal.client.Htp;
import edu.mayo.phenoportal.client.core.ContextAreas;
import edu.mayo.phenoportal.client.events.ContextAreaChangedEvent;
import edu.mayo.phenoportal.client.events.LoggedInEvent;
import edu.mayo.phenoportal.client.events.LoggedInEventHandler;
import edu.mayo.phenoportal.client.events.LoggedOutEvent;
import edu.mayo.phenoportal.client.events.LoggedOutEventHandler;
import edu.mayo.phenoportal.client.phenotype.PhenotypeService;
import edu.mayo.phenoportal.client.phenotype.PhenotypeServiceAsync;
import edu.mayo.phenoportal.client.utils.MessageWindow;
import edu.mayo.phenoportal.shared.User;

public class AlgorithmPanel extends VLayout {

    static Logger lgr = Logger.getLogger(AlgorithmPanel.class.getName());
    private static final String UPLOAD_TITLE = "Upload Phenotype";
    private static final String CREATE_TITLE = "Create Phenotype";

    private static final int BUTTON_WIDTH = 175;

    private static final String CREATE_URL = "https://www.emeasuretool.cms.gov/MeasureAuthoringTool/Login.html";

    private IButton i_createButton;
    private IButton i_uploadButton;

    public AlgorithmPanel() {
        super();

        init();

        createLoggedInEventHandler();
        createLoggedOutEventHandler();
    }

    private void init() {

        setMargin(25);
        setMembersMargin(15);

        i_createButton = new IButton(CREATE_TITLE);
        i_createButton.setWidth(BUTTON_WIDTH);
        i_createButton.setIcon("createPhenotype.png");
        i_createButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
	            /* TODO: Implement with actual users creds */
	            PhenotypeServiceAsync service = GWT.create(PhenotypeService.class);
	            service.getMatEditorUrl(Htp.getLoggedInUser(), new AsyncCallback<String>() {
		            @Override
		            public void onFailure(Throwable caught) {
			            String title = "Login Failed";
			            String message = "Failed to login to the Measure Authoring Tool.<br/>&nbsp;Invalid Id/Password. Please try again.";
			            MessageWindow messageWindow = new MessageWindow(title, message);
			            messageWindow.show();
		            }

		            @Override
		            public void onSuccess(String result) {
			            Window.open(result, "mat", null);
		            }
	            });
            }
        });

        i_uploadButton = new IButton(UPLOAD_TITLE);
        i_uploadButton.setWidth(BUTTON_WIDTH);
        i_uploadButton.setIcon("uploadPhenotype.png");
        i_uploadButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                try {
                    Htp.EVENT_BUS.fireEvent(new ContextAreaChangedEvent(ContextAreas.types.UPLOAD));
                } catch (Exception e) {
                    lgr.log(Level.INFO, e.getMessage());
                    e.printStackTrace();
                }
            }
        });

        setEnablementBasedOnUserLoggedIn();

        addMember(i_createButton);
        addMember(i_uploadButton);
    }

    private void setEnablementBasedOnUserLoggedIn() {
        User user = Htp.getLoggedInUser();

        // Admin = 1, Execute = 2
        if (user != null && user.getRole() <= 2) {
            i_uploadButton.setDisabled(false);
        } else {
            i_uploadButton.setDisabled(true);
        }

    }

    /**
     * Create a handler to change Logged in/out button based on the log in state
     */
    private void createLoggedInEventHandler() {
        Htp.EVENT_BUS.addHandler(LoggedInEvent.TYPE, new LoggedInEventHandler() {

            @Override
            public void onLoggedIn(LoggedInEvent loggedInEvent) {
                setEnablementBasedOnUserLoggedIn();
            }
        });
    }

    /**
     * Create a handler to change Logged in/out button based on the log in state
     */
    private void createLoggedOutEventHandler() {
        Htp.EVENT_BUS.addHandler(LoggedOutEvent.TYPE, new LoggedOutEventHandler() {

            @Override
            public void onLoggedOut(LoggedOutEvent loggedOutEvent) {
                setEnablementBasedOnUserLoggedIn();
            }
        });
    }
}

@@ -2153,16 +2153,18 @@
 reds */%0A
+//
 %09       
@@ -2236,16 +2236,18 @@
 class);%0A
+//
 %09       
@@ -2328,16 +2328,18 @@
 ng%3E() %7B%0A
+//
 %09%09      
@@ -2346,32 +2346,34 @@
       @Override%0A
+//
 %09%09            pu
@@ -2408,24 +2408,26 @@
 e caught) %7B%0A
+//
 %09%09%09         
@@ -2460,16 +2460,18 @@
 ailed%22;%0A
+//
 %09%09%09     
@@ -2593,16 +2593,18 @@
 gain.%22;%0A
+//
 %09%09%09     
@@ -2675,16 +2675,18 @@
 ssage);%0A
+//
 %09%09%09     
@@ -2714,16 +2714,18 @@
 show();%0A
+//
 %09%09      
@@ -2732,17 +2732,21 @@
       %7D%0A
-%0A
+//%0A//
 %09%09      
@@ -2761,16 +2761,18 @@
 verride%0A
+//
 %09%09      
@@ -2816,16 +2816,20 @@
 sult) %7B%0A
+////
 %09%09%09     
@@ -2869,16 +2869,18 @@
  null);%0A
+//
 %09%09      
@@ -2887,16 +2887,18 @@
       %7D%0A
+//
 %09       
@@ -2902,24 +2902,69 @@
         %7D);%0A
+%09%09%09    Window.open(CREATE_URL, %22mat%22, null);%0A
             

Create Phenotype button forwards user to cms.gov MAT site