package nl.sense_os.commonsense.client.utility;

import nl.sense_os.commonsense.shared.TagModel;

import com.extjs.gxt.ui.client.data.ModelKeyProvider;
import com.extjs.gxt.ui.client.data.TreeModel;

/**
 * Key provider to help grids and trees distinguish between different sensors.
 */
public class SenseKeyProvider<M extends TreeModel> implements ModelKeyProvider<M> {

    private static final String TAG = "SenseKeyProvider";

    @Override
    public String getKey(M model) {
        String key = "";

        while (model != null) {
            if (false == model.getProperties().containsKey("tagType")) {
                Log.e(TAG, "No tagType?!?!");
                for (String property : model.getPropertyNames()) {
                    Log.e(TAG, "  " + model.get(property));
                }
                model = (M) model.getParent();
                continue;
            }
            key += getPartialKey(model);
            model = (M) model.getParent();
        }
        return key;
    }

    private String getPartialKey(M model) {
        int tagType = model.get("tagType");
        switch (tagType) {
            case TagModel.TYPE_DEVICE :
                return "D_" + model.<String> get("id") + ": " + model.<String> get("uuid") + "; ";
            case TagModel.TYPE_GROUP :
                return "G_" + model.<String> get("id") + ": " + model.<String> get("text") + "; ";
            case TagModel.TYPE_SENSOR :
                return "S_" + model.<String> get("id") + ": " + model.<String> get("text") + "; ";
            case TagModel.TYPE_SERVICE :
                return "X_" + model.<String> get("id") + ": " + model.<String> get("text") + "; ";
            case TagModel.TYPE_USER :
                return "U_" + model.<String> get("id") + ": " + model.<String> get("text") + "; ";
            case TagModel.TYPE_CATEGORY :
                return "C_" + model.<String> get("text") + "; ";
            default :
                Log.d(TAG, "Unexpected tagType");
                return "error";
        }
    }
}

@@ -80,19 +80,172 @@
 .shared.
-Tag
+DeviceModel;%0Aimport nl.sense_os.commonsense.shared.GroupModel;%0Aimport nl.sense_os.commonsense.shared.SensorModel;%0Aimport nl.sense_os.commonsense.shared.User
 Model;%0A%0A
@@ -573,24 +573,59 @@
 Provider%22;%0A%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     @Overrid
@@ -687,17 +687,16 @@
 y = %22%22;%0A
-%0A
         
@@ -723,359 +723,8 @@
 ) %7B%0A
-            if (false == model.getProperties().containsKey(%22tagType%22)) %7B%0A                Log.e(TAG, %22No tagType?!?!%22);%0A                for (String property : model.getPropertyNames()) %7B%0A                    Log.e(TAG, %22  %22 + model.get(property));%0A                %7D%0A                model = (M) model.getParent();%0A                continue;%0A            %7D%0A
     
@@ -876,32 +876,33 @@
 lKey(M model) %7B%0A
+%0A
         int tagT
@@ -898,199 +898,162 @@
    i
-nt tagType = model.get(%22tagType%22);%0A        switch (tagType) %7B%0A            case TagModel.TYPE_DEVICE :%0A                return %22D_%22 + model.%3CString%3E get(%22id%22) + %22: %22 + model.%3CString%3E get(%22uuid%22
+f (model instanceof DeviceModel) %7B%0A            DeviceModel device = (DeviceModel) model;%0A            return %22D_%22 + device.getId() + %22: %22 + device.getUuid(
 ) + 
@@ -1070,319 +1070,279 @@
     
-    case TagModel.TYPE_GROUP :%0A                return %22G_%22 + model.%3CString%3E get(%22id%22) + %22: %22 + model.%3CString%3E get(%22text%22) + %22; %22;%0A            case TagModel.TYPE_SENSOR :%0A                return %22S_%22 + model.%3CString%3E get(%22id%22) + %22: %22 + model.%3CString%3E get(%22text%22) + %22; %22;%0A            case TagModel.TYPE_SERVICE :%0A 
+%7D else if (model instanceof GroupModel) %7B%0A            GroupModel group = (GroupModel) model;%0A            return %22G_%22 + group.getId() + %22: %22 + group.getName() + %22; %22;%0A        %7D else if (model instanceof SensorModel) %7B%0A            SensorModel sensor = (SensorModel) model;%0A
     
@@ -1349,27 +1349,24 @@
         
-   
 return %22
 X_%22 + mo
@@ -1361,266 +1361,167 @@
 rn %22
-X
+S
 _%22 + 
-model.%3CString%3E get(%22id%22) + %22: %22 + model.%3CString%3E get(%22text%22) + %22; %22;%0A            case TagModel.TYPE_USER :%0A                return %22U_%22 + model.%3CString%3E get(%22id%22) + %22: %22 + model.%3CString%3E get(%22text%22) + %22; %22;%0A            case TagModel.TYPE_CATEGORY :%0A    
+sensor.getId() + %22: %22 + sensor.getDisplayName() + %22; %22;%0A        %7D else if (model instanceof UserModel) %7B%0A            UserModel user = (UserModel) model;%0A
     
@@ -1540,39 +1540,49 @@
 rn %22
-C
+U
 _%22 + 
-model.%3CString%3E get(%22text%22
+user.getId() + %22: %22 + user.getName(
 ) + 
@@ -1599,21 +1599,16 @@
     
-    default :
+%7D else %7B
 %0A   
@@ -1620,17 +1620,13 @@
     
-    
 Log.
-d
+e
 (TAG
@@ -1643,16 +1643,24 @@
 ted 
-tagType%22
+class: %22 + model
 );%0A 
@@ -1674,25 +1674,26 @@
     
-    
 return 
-%22error
+model + %22; 
 %22;%0A 

stop using tagtype property, use instanceof instead