package nl.sense_os.commonsense.client.utility;

import nl.sense_os.commonsense.shared.Constants;
import nl.sense_os.commonsense.shared.TagModel;
import nl.sense_os.commonsense.shared.UserModel;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.data.TreeModel;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class SenseIconProvider<M extends TreeModel> implements ModelIconProvider<M> {

    private static final String TAG = "SenseIconProvider";

    public static final String GXT_ICONS_PATH = "gxt/images/gxt/icons/";
    public static final String SENSE_ICONS_PATH = "img/icons/16/";

    protected static final AbstractImagePrototype ICON_DEVICE = IconHelper.create(SENSE_ICONS_PATH
            + "phone_Android.png");
    protected static final AbstractImagePrototype ICON_GROUP = IconHelper.create(SENSE_ICONS_PATH
            + "group.png");
    protected static final AbstractImagePrototype ICON_GXT_DONE = IconHelper.create(GXT_ICONS_PATH
            + "done.gif");
    protected static final AbstractImagePrototype ICON_GXT_FOLDER = IconHelper
            .create(GXT_ICONS_PATH + "folder.gif");
    protected static final AbstractImagePrototype ICON_GXT_LEAF = IconHelper.create(GXT_ICONS_PATH
            + "tabs.gif");
    protected static final AbstractImagePrototype ICON_SENSOR_DEVICE = IconHelper
            .create(SenseIconProvider.SENSE_ICONS_PATH + "sense_red.gif");
    protected static final AbstractImagePrototype ICON_SENSOR_ENVIRONMENT = IconHelper
            .create(SenseIconProvider.SENSE_ICONS_PATH + "sense_orange.gif");
    protected static final AbstractImagePrototype ICON_SENSOR_FEED = IconHelper
            .create(SenseIconProvider.SENSE_ICONS_PATH + "rss.gif");
    protected static final AbstractImagePrototype ICON_SENSOR_PUBLIC = IconHelper
            .create(SenseIconProvider.SENSE_ICONS_PATH + "sense_black.gif");
    protected static final AbstractImagePrototype ICON_SENSOR_STATE = IconHelper
            .create(SenseIconProvider.SENSE_ICONS_PATH + "sense_magenta.gif");
    protected static final AbstractImagePrototype ICON_USER = IconHelper.create(SENSE_ICONS_PATH
            + "user.png");
    protected static final AbstractImagePrototype ICON_USER_ME = IconHelper.create(SENSE_ICONS_PATH
            + "user_zorro.png");

    @Override
    public AbstractImagePrototype getIcon(M model) {

        final int tagType = model.<Integer> get("tagType");
        if (tagType == TagModel.TYPE_GROUP) {
            return ICON_GROUP;

        } else if (tagType == TagModel.TYPE_DEVICE) {
            return ICON_DEVICE;

        } else if (tagType == TagModel.TYPE_SENSOR) {
            final int type = Integer.parseInt(model.<String> get("type"));
            switch (type) {
                case 0 :
                    return ICON_SENSOR_FEED;
                case 1 :
                    return ICON_SENSOR_DEVICE;
                case 2 :
                    return ICON_SENSOR_STATE;
                case 3 :
                    return ICON_SENSOR_PUBLIC;
                case 4 :
                    return ICON_SENSOR_ENVIRONMENT;
                default :
                    return ICON_GXT_LEAF;
            }

        } else if (tagType == TagModel.TYPE_SERVICE) {
            return ICON_GXT_LEAF;

        } else if (tagType == TagModel.TYPE_USER) {
            final UserModel me = Registry.<UserModel> get(Constants.REG_USER);
            if (model.equals(me)) {
                return ICON_USER_ME;
            } else {
                return ICON_USER;
            }

        } else if (tagType == TagModel.TYPE_CATEGORY) {
            return ICON_GXT_FOLDER;

        } else {
            Log.e(TAG, "Unexpected tag type in ModelIconProvider: " + tagType);
            return ICON_GXT_DONE;
        }
    }
}

@@ -129,19 +129,123 @@
 .shared.
-Tag
+DeviceModel;%0Aimport nl.sense_os.commonsense.shared.GroupModel;%0Aimport nl.sense_os.commonsense.shared.Sensor
 Model;%0Ai
@@ -2534,28 +2534,27 @@
     
-@Override%0A    public
+public static final
  Abs
@@ -2577,130 +2577,333 @@
 ype 
-getIcon(M model) %7B%0A%0A        final int tagType = model.%3CInteger%3E get(%22tagType
+ICON_LOADING = IconHelper%0A            .create(%22gxt/images/gxt/icons/loading.gif%22);%0A    public static final AbstractImagePrototype ICON_BUTTON_GO = IconHelper%0A            .create(%22gxt/images/gxt/icons/page-next.gif
 %22);%0A
+%0A
     
-    if (tagType == TagModel.TYPE_GROUP
+@Override%0A    public AbstractImagePrototype getIcon(M model) %7B%0A%0A        if (model instanceof GroupModel
 ) %7B%0A
@@ -2957,39 +2957,36 @@
 if (
-tagType == TagModel.TYPE_DEVICE
+model instanceof DeviceModel
 ) %7B%0A
@@ -3041,39 +3041,36 @@
 if (
-tagType == TagModel.TYPE_SENSOR
+model instanceof SensorModel
 ) %7B%0A
@@ -3188,19 +3188,14 @@
     
-    
 case 0
- 
 :%0A  
@@ -3200,36 +3200,32 @@
                 
-  
-  
 return ICON_SENS
@@ -3249,19 +3249,14 @@
     
-    
 case 1
- 
 :%0A  
@@ -3261,36 +3261,32 @@
                 
-  
-  
 return ICON_SENS
@@ -3312,19 +3312,14 @@
     
-    
 case 2
- 
 :%0A  
@@ -3324,36 +3324,32 @@
                 
- 
-   
 return ICON_SENS
@@ -3374,19 +3374,14 @@
     
-    
 case 3
- 
 :%0A  
@@ -3386,36 +3386,32 @@
                 
- 
-   
 return ICON_SENS
@@ -3437,19 +3437,14 @@
     
-    
 case 4
- 
 :%0A  
@@ -3449,36 +3449,32 @@
                 
- 
-   
 return ICON_SENS
@@ -3505,26 +3505,17 @@
     
-    
 default
- 
 :%0A
-    
     
@@ -3586,127 +3586,34 @@
 if (
-tagType == TagModel.TYPE_SERVICE) %7B%0A            return ICON_GXT_LEAF;%0A%0A        %7D else if (tagType == TagModel.TYPE_USER
+model instanceof UserModel
 ) %7B%0A
@@ -3853,180 +3853,67 @@
 lse 
-if (tagType == TagModel.TYPE_CATEGORY) %7B%0A            return ICON_GXT_FOLDER;%0A%0A        %7D else %7B%0A            Log.e(TAG, %22Unexpected tag type in ModelIconProvider: %22 + tagType
+%7B%0A            Log.e(TAG, %22Unexpected model class: %22 + model
 );%0A 

stop using tagtype property, use instanceof instead