package org.jboss.as.console.client.shared.dispatch;

import org.jboss.as.console.client.shared.state.ReloadState;
import org.jboss.dmr.client.ModelNode;
import org.jboss.dmr.client.Property;

import javax.inject.Inject;
import java.util.List;

/**
 * @author Heiko Braun
 * @date 1/17/12
 */
public class DomainResponseProcessor implements ResponseProcessor {

    private static final String RESPONSE_HEADERS = "response-headers";
    private static final String PROCESS_STATE = "process-state";

    private static final String RESTART_REQUIRED = "restart-required";
    private static final String RELOAD_REQUIRED = "reload-required";
    private static final String SERVER_GROUPS = "server-groups";
    private static final String RESPONSE = "response";

    private ReloadState reloadState;

    @Inject
    public DomainResponseProcessor(ReloadState reloadState) {
        this.reloadState = reloadState;
    }

    @Override
    public void process(ModelNode response) {

        parseServerState(response, reloadState);

        reloadState.propagateChanges();

    }

    private static boolean parseServerState(ModelNode response, ReloadState reloadState) {
        boolean staleModel = false;

        if(response.hasDefined(SERVER_GROUPS))
        {
            List<Property> serverGroups = response.get(SERVER_GROUPS).asPropertyList();
            for(Property serverGroup : serverGroups)
            {
                //System.out.println("\n\n");
                ModelNode serverGroupValue = serverGroup.getValue();
                //System.out.println("server-group: "+serverGroup.getName());

                //  -- Server Group --

                List<Property> hosts = serverGroupValue.get("host").asPropertyList();
                for(Property host : hosts)
                {
                    // -- Host  --

                    ModelNode hostValue = host.getValue();
                    //System.out.println("host: "+host.getName());
                    parseHost(host.getName(), hostValue, reloadState);
                }
            }
        }

        return staleModel;
    }

    private static void parseHost(final String hostName, ModelNode hostValue, ReloadState reloadState) {

        //System.out.println(hostValue);

        List<Property> servers = hostValue.asPropertyList();

        for(Property server : servers)
        {

            // -- Server  --
            //System.out.println("server: "+ server.getName());

            ModelNode serverResponse = server.getValue().get(RESPONSE);

            if(serverResponse.hasDefined(RESPONSE_HEADERS))
            {
                List<Property> headers = serverResponse.get(RESPONSE_HEADERS).asPropertyList();
                for(Property header : headers)
                {
                    if(PROCESS_STATE.equals(header.getName()))
                    {
                        String headerValue = header.getValue().asString();

                        if(RESTART_REQUIRED.equals(headerValue))
                        {
                            reloadState.setRestartRequired(
                                    "Host: "+hostName+", server: "+server.getName(),
                                    true
                            );
                        }
                        else if(RELOAD_REQUIRED.equals(headerValue))
                        {
                            reloadState.setReloadRequired(
                                    "Host: "+hostName+", server: "+server.getName(),
                                    true
                            );
                        }
                    }
                }
            }
        }
    }
}

@@ -1640,25 +1640,102 @@
 er Group --%0A
-%0A
+                if(serverGroupValue.hasDefined(%22host%22))%0A                %7B%0A    
             
@@ -1816,32 +1816,36 @@
                 
+ 
+   
 for(Property hos
@@ -1863,32 +1863,36 @@
                 
+    
 %7B%0A              
@@ -1893,24 +1893,28 @@
             
+ 
+   
 // -- Host  
@@ -1917,16 +1917,20 @@
 st  --%0A%0A
+    
         
@@ -1992,32 +1992,36 @@
                 
+    
 //System.out.pri
@@ -2051,16 +2051,20 @@
 ame());%0A
+    
         
@@ -2126,16 +2126,38 @@
 State);%0A
+                    %7D%0A
         

Fix AS7-5881: Compatibility of the domain overview with 7.1