package fm.last.moji.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fm.last.moji.MojiDeviceStatus;
import fm.last.moji.tracker.Tracker;

class GetDeviceStatusesCommand implements MojiCommand {

  private final String domain;
  private List<MojiDeviceStatus> deviceStatuses;

  GetDeviceStatusesCommand(String domain) {
    this.domain = domain;
    deviceStatuses = Collections.emptyList();
  }

  @Override
  public void executeWithTracker(Tracker tracker) throws IOException {
    Map<String, String> valueMap = tracker.getDeviceStatuses(domain);
    if (!valueMap.isEmpty()) {
      Map<String, Map<String, String>> attributesByDevice = new HashMap<String, Map<String, String>>();
      for (Map.Entry<String, String> entry : valueMap.entrySet()) {
        String key = entry.getKey();
        // ignoring the attribute of number of devices
        if (!key.equalsIgnoreCase("devices")) {
          String deviceName = key.substring(0, key.indexOf("_"));
          String attributeName = key.substring(key.indexOf("_") + 1).toLowerCase();
          Map<String, String> attributes = attributesByDevice.get(deviceName);
          if (attributes == null) {
            attributes = new HashMap<String, String>(DeviceStatusField.values().length);
            attributesByDevice.put(deviceName, attributes);
          }
          attributes.put(attributeName, entry.getValue());
        }
      }
      List<MojiDeviceStatus> statuses = new ArrayList<MojiDeviceStatus>(attributesByDevice.size());
      for (Map.Entry<String, Map<String, String>> deviceAttributes : attributesByDevice.entrySet()) {
        statuses.add(new MojiDeviceStatusImpl(deviceAttributes.getKey(), deviceAttributes.getValue()));
      }
      deviceStatuses = Collections.unmodifiableList(statuses);
    }
  }

  List<MojiDeviceStatus> getStatuses() {
    return deviceStatuses;
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("GetDeviceStatusesCommand [domain=");
    builder.append(domain);
    builder.append(", deviceStatuses=");
    builder.append(deviceStatuses);
    builder.append("]");
    return builder.toString();
  }

}

@@ -179,16 +179,74 @@
 l.Map;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import f
@@ -370,16 +370,110 @@
 mand %7B%0A%0A
+  private static final Logger log = LoggerFactory.getLogger(GetDeviceStatusesCommand.class);%0A%0A
   privat
@@ -889,25 +889,25 @@
 tring%3E%3E 
-attribu
+parame
 te
+r
 sByDevic
@@ -1037,19 +1037,29 @@
  String 
-key
+parameterName
  = entry
@@ -1065,24 +1065,64 @@
 y.getKey();%0A
+        boolean parameterAdded = false;%0A
         // i
@@ -1133,25 +1133,25 @@
 ing the 
-attribu
+parame
 te
+r
  of numb
@@ -1177,19 +1177,25 @@
    if (!
-key
+%22devices%22
 .equalsI
@@ -1208,18 +1208,154 @@
 ase(
-%22devices%22)
+parameterName)) %7B%0A          int delimiterPosition = parameterName.indexOf(%22_%22);%0A          if (parameterName.length() %3E 2 && delimiterPosition %3E= 0
 ) %7B%0A
@@ -1360,24 +1360,26 @@
 %7B%0A          
+ 
+ 
 String devic
@@ -1378,35 +1378,45 @@
 ng deviceName = 
-key
+parameterName
 .substring(0, ke
@@ -1413,19 +1413,29 @@
 ring(0, 
-key
+parameterName
 .indexOf
@@ -1456,48 +1456,63 @@
     
-String attribu
+  parame
 te
+r
 Name = 
-key.substring(key
+parameterName.substring(parameterName
 .ind
@@ -1547,24 +1547,26 @@
 ;%0A          
+ 
+ 
 Map%3CString, 
@@ -1577,30 +1577,30 @@
 ng%3E 
-attribu
+parame
 te
+r
 s = 
-attribu
+parame
 te
+r
 sByD
@@ -1636,21 +1636,23 @@
     
+  
 if (
-attribu
+parame
 te
+r
 s ==
@@ -1672,25 +1672,27 @@
         
-attribu
+  parame
 te
+r
 s = new 
@@ -1763,25 +1763,27 @@
         
-attribu
+  parame
 te
+r
 sByDevic
@@ -1804,21 +1804,23 @@
 me, 
-attribu
+parame
 te
+r
 s);%0A
+  
     
@@ -1841,32 +1841,34 @@
     
-attribu
+  parame
 te
+r
 s.put(
-attribu
+parame
 te
+r
 Name
@@ -1884,24 +1884,179 @@
 etValue());%0A
+            parameterAdded = true;%0A          %7D%0A        %7D%0A        if (!parameterAdded) %7B%0A          log.debug(%22Ignoring attribute named: %22 + parameterName);%0A
         %7D%0A  
@@ -2133,25 +2133,25 @@
 Status%3E(
-attribu
+parame
 te
+r
 sByDevic
@@ -2221,30 +2221,30 @@
 vice
-Attribu
+Parame
 te
+r
 s : 
-attribu
+parame
 te
+r
 sByD
@@ -2311,33 +2311,33 @@
 sImpl(device
-Attribu
+Parame
 te
+r
 s.getKey(), 
@@ -2346,17 +2346,17 @@
 vice
-Attribu
+Parame
 te
+r
 s.ge

Check parameter names