/**
 * Copyright 2010 JBoss 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 org.drools.guvnor.client;

import org.drools.guvnor.client.common.GenericCallback;
import org.drools.guvnor.client.explorer.ExplorerLayoutManager;
import org.drools.guvnor.client.rpc.RepositoryServiceFactory;
import org.drools.guvnor.client.rpc.UserSecurityContext;
import org.drools.guvnor.client.security.Capabilities;
import org.drools.guvnor.client.messages.Constants;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.Window;
import com.gwtext.client.util.CSS;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.QuickTips;
import com.gwtext.client.widgets.Viewport;
import com.gwtext.client.widgets.form.Field;

/**
 * This is the main launching/entry point for the JBRMS web console.
 * It essentially sets the initial layout.
 *
 * If you hadn't noticed, this is using GWT from google. Refer to GWT docs
 * if GWT is new to you (it is quite a different way of building web apps).
 */
public class JBRMSEntryPoint
    implements
    EntryPoint, HistoryListener {

    private LoggedInUserInfo loggedInUserInfo;

    public void onModuleLoad() {
        Field.setMsgTarget("side");
        QuickTips.init();
        CSS.swapStyleSheet("theme", "js/ext/resources/css/xtheme-gray.css");
        loggedInUserInfo = new LoggedInUserInfo();
        loggedInUserInfo.setVisible(false);
        checkLoggedIn();
        History.addHistoryListener(this);
    }

	private Panel createMain(Capabilities caps) {
		return (new ExplorerLayoutManager(loggedInUserInfo, caps)).getBaseLayout();
	}


    /**
     * Check if user is logged in, if not, then show prompt.
     * If it is, then we show the app, in all its glory !
     */
    private void checkLoggedIn() {
        RepositoryServiceFactory.getSecurityService().getCurrentUser( new GenericCallback<UserSecurityContext>() {
            public void onSuccess(UserSecurityContext ctx) {
                if ( ctx.userName != null ) {
                	showMain();
                    loggedInUserInfo.setUserName( ctx.userName );
                    loggedInUserInfo.setVisible( true );

                } else {
                	final LoginWidget lw = new LoginWidget();
                	lw.setLoggedInEvent(new Command() {
                        public void execute() {
                        	showMain();
                            loggedInUserInfo.setUserName( lw.getUserName() );
                            loggedInUserInfo.setVisible( true );
                        }
                    } );
                	lw.show();
                }
            }


        } );
    }

	private void showMain() {
		Window.setStatus(((Constants) GWT.create(Constants.class)).LoadingUserPermissions());
		RepositoryServiceFactory.getSecurityService().getUserCapabilities(new GenericCallback<Capabilities>() {
			public void onSuccess(Capabilities cp) {
				Window.setStatus(" ");
				new Viewport(createMain(cp));

			}
		});
	}

	public void onHistoryChanged(String a) {

	}




}

@@ -1056,16 +1056,140 @@
 nt.GWT;%0A
+import com.google.gwt.event.logical.shared.ValueChangeEvent;%0Aimport com.google.gwt.event.logical.shared.ValueChangeHandler;%0A
 import c
@@ -1300,31 +1300,76 @@
 .client.
-HistoryListener
+Window;%0Aimport com.google.gwt.user.client.ui.RootLayoutPanel
 ;%0Aimport
@@ -1388,38 +1388,44 @@
 gwt.user.client.
-Window
+ui.RootPanel
 ;%0Aimport com.gwt
@@ -1920,20 +1920,16 @@
 tryPoint
-%0A   
  impleme
@@ -1931,20 +1931,16 @@
 plements
-%0A   
  EntryPo
@@ -1946,25 +1946,8 @@
 oint
-, HistoryListener
  %7B%0A%0A
@@ -2290,50 +2290,8 @@
 ();%0A
-        History.addHistoryListener(this);%0A
     
@@ -2422,17 +2422,16 @@
 ();%0A%09%7D%0A%0A
-%0A
     /**%0A
@@ -2966,17 +2966,16 @@
 true );%0A
-%0A
         
@@ -3442,18 +3442,16 @@
       %7D%0A
-%0A%0A
         
@@ -3762,102 +3762,436 @@
 %09%09%09%09
-new Viewport(createMain(cp));%0A%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D%0A%0A%09public void onHistoryChanged(String a) %7B%0A%0A%09%7D%0A%0A%0A%0A
+//RootLayoutPanel.get().add(createMain(cp));%0A%09%09%09%09new Viewport(createMain(cp));%0A%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09%0A%09    // Setup a history handler to reselect the associate menu item%0A%09    final ValueChangeHandler%3CString%3E historyHandler = new ValueChangeHandler%3CString%3E() %7B%0A%09      public void onValueChange(ValueChangeEvent%3CString%3E event) %7B%0A              //TODO: Handle History%0A%09      %7D%0A%09    %7D;%0A%09    History.addValueChangeHandler(historyHandler);%09%09%0A%09%7D
 %0A%7D%0A

Replace deprecated GWT API.