/*
 * JBoss, Home of Professional Open Source
 * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @author tags. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

package org.jboss.as.console.client.core.bootstrap;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.rpc.AsyncCallback;
import org.jboss.as.console.client.Console;
import org.jboss.as.console.client.core.BootstrapContext;
import org.jboss.as.console.client.shared.Preferences;
import org.jboss.as.console.client.shared.dispatch.AsyncCommand;
import org.jboss.as.console.client.shared.dispatch.DispatchAsync;
import org.jboss.as.console.client.shared.dispatch.impl.DMRAction;
import org.jboss.as.console.client.shared.dispatch.impl.DMRResponse;
import org.jboss.as.console.client.shared.properties.PropertyRecord;
import org.jboss.as.console.client.standalone.StandaloneServer;
import org.jboss.ballroom.client.widgets.window.Feedback;
import org.jboss.dmr.client.ModelNode;
import org.jboss.dmr.client.Property;

import java.util.ArrayList;
import java.util.List;

import static org.jboss.dmr.client.ModelDescriptionConstants.*;

/**
 * @author Heiko Braun
 * @date 5/19/11
 */
public class ExecutionMode implements AsyncCommand<Boolean>{


    private BootstrapContext bootstrap;
    private DispatchAsync dispatcher;

    public ExecutionMode(BootstrapContext bootstrap, DispatchAsync dispatcher) {
        this.bootstrap = bootstrap;
        this.dispatcher = dispatcher;
    }

    @Override
    public void execute(final AsyncCallback<Boolean> callback) {




        ModelNode operation = new ModelNode();
        operation.get(OP).set(COMPOSITE);
        operation.get(ADDRESS).setEmptyList();

        List<ModelNode> steps = new ArrayList<ModelNode>();

        ModelNode execModeOp = new ModelNode();
        execModeOp.get(OP).set(READ_ATTRIBUTE_OPERATION);
        execModeOp.get(NAME).set("process-type");
        execModeOp.get(ADDRESS).setEmptyList();
        steps.add(execModeOp);

        ModelNode envProperties = new ModelNode();
        envProperties.get(OP).set(READ_ATTRIBUTE_OPERATION);
        envProperties.get(ADDRESS).add("core-service", "platform-mbean");
        envProperties.get(ADDRESS).add("type", "runtime");
        envProperties.get(NAME).set("system-properties");
        steps.add(envProperties);

        operation.get(STEPS).set(steps);

        dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
            @Override
            public void onFailure(Throwable caught) {
                bootstrap.setProperty(BootstrapContext.STANDALONE, "false");
                bootstrap.setlastError(caught);
                Log.error(caught.getMessage());
                callback.onSuccess(Boolean.FALSE);
            }

            @Override
            public void onSuccess(DMRResponse result) {

                ModelNode response = result.get();
                List<Property> propertyList = response.get(RESULT).asPropertyList();
                boolean outcome = true;

                for(Property step : propertyList)
                {
                    ModelNode stepResult = step.getValue();
                    if(step.getName().equals("step-1"))
                    {
                        boolean isServer = stepResult.get(RESULT).asString().equals("Server");
                        bootstrap.setProperty(BootstrapContext.STANDALONE, Boolean.valueOf(isServer).toString());
                    }
                    else
                    {
                        List<Property> properties = stepResult.get(RESULT).asPropertyList();
                        for(Property prop : properties)
                        {
                            if(prop.getName().equals("user.language"))
                            {
                                final String preferredLang = prop.getValue().asString();
                                if(!preferredLang.equals(Preferences.get("as7_ui_locale", "en")))
                                {
                                    Preferences.set("as7_ui_locale", preferredLang);
                                    bootstrap.setlastError(new Throwable(
                                        "The user.language has changed. Please reload the web interface!"
                                    ));
                                    outcome = false;
                                }

                                break;
                            }
                        }
                    }
                }

                callback.onSuccess(outcome);

            }
        });

    }
}

@@ -1147,128 +1147,29 @@
 ent.
-Console;%0Aimport org.jboss.as.console.client.core.BootstrapContext;%0Aimport org.jboss.as.console.client.shared.Preferences
+core.BootstrapContext
 ;%0Aim
@@ -1454,309 +1454,28 @@
 oss.
-as.console.client.shared.properties.PropertyRecord;%0Aimport org.jboss.as.console.client.standalone.StandaloneServer;%0Aimport org.jboss.ballroom.client.widgets.window.Feedback;%0Aimport org.jboss.dmr.client.ModelNode;%0Aimport org.jboss.dmr.client.Property;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List
+dmr.client.ModelNode
 ;%0A%0Ai
@@ -1974,19 +1974,16 @@
  %7B%0A%0A
-%0A%0A%0A
         
 Mode
@@ -1982,338 +1982,32 @@
     
-ModelNode operation = new ModelNode();%0A        operation.get(OP).set(COMPOSITE);%0A        operation.get(ADDRESS).setEmptyList();%0A%0A        List%3CModelNode%3E steps = new ArrayList%3CModelNode%3E();%0A%0A        ModelNode execModeOp = new ModelNode();%0A        execModeOp.get(OP).set(READ_ATTRIBUTE_OPERATION);%0A        execModeOp.get(NAME).set(%22
+// :read-attribute(name=
 proc
@@ -2014,19 +2014,17 @@
 ess-type
-%22);
+)
 %0A       
@@ -2028,87 +2028,13 @@
     
-execModeOp.get(ADDRESS).setEmptyList();%0A        steps.add(execModeOp);%0A%0A       
+final
  Mod
@@ -2040,29 +2040,25 @@
 delNode 
-envPr
 oper
+a
 ti
-es
+on
  = new M
@@ -2073,37 +2073,33 @@
 ();%0A        
-envPr
 oper
+a
 ti
-es
+on
 .get(OP).set
@@ -2138,226 +2138,49 @@
     
-envProperties.get(ADDRESS).add(%22core-service%22, %22platform-mbean%22);%0A        envProperties.get(ADDRESS).add(%22type%22, %22runtime%22);%0A        envProperties.get(NAME).set(%22system-properties%22);%0A        steps.add(envProperties
+operation.get(NAME).set(%22process-type%22
 );%0A
-%0A
     
@@ -2201,24 +2201,30 @@
 get(
-STEPS).set(steps
+ADDRESS).setEmptyList(
 );%0A%0A
@@ -2776,377 +2776,35 @@
     
-List%3CProperty%3E propertyList = response.get(RESULT).asPropertyList();%0A                boolean outcome = true;%0A%0A                for(Property step : propertyList)%0A                %7B%0A                    ModelNode stepResult = step.getValue();%0A                    if(step.getName().equals(%22step-1%22))%0A                    %7B%0A                        boolean isServer = stepResult
+boolean isServer = response
 .get
@@ -2853,32 +2853,24 @@
             
- 
-       
 bootstrap.se
@@ -2950,1102 +2950,8 @@
 ());
-%0A                    %7D%0A                    else%0A                    %7B%0A                        List%3CProperty%3E properties = stepResult.get(RESULT).asPropertyList();%0A                        for(Property prop : properties)%0A                        %7B%0A                            if(prop.getName().equals(%22user.language%22))%0A                            %7B%0A                                final String preferredLang = prop.getValue().asString();%0A                                if(!preferredLang.equals(Preferences.get(%22as7_ui_locale%22, %22en%22)))%0A                                %7B%0A                                    Preferences.set(%22as7_ui_locale%22, preferredLang);%0A                                    bootstrap.setlastError(new Throwable(%0A                                        %22The user.language has changed. Please reload the web interface!%22%0A                                    ));%0A                                    outcome = false;%0A                                %7D%0A%0A                                break;%0A                            %7D%0A                        %7D%0A                    %7D%0A                %7D
 %0A%0A  
@@ -2987,15 +2987,20 @@
 ess(
-outcome
+Boolean.TRUE
 );%0A%0A

rollback the -Duser.lang switch, it breaks the domain mode