/*global MashupPlatform StyledElements NGSI*/

(function () {
 
    "use strict";

    var layout,
        ngsi,
        form,
        currentData,
        error,
        info;

    var create_ngsi_connection = function create_ngsi_connection() {
        ngsi = new NGSI.Connection(MashupPlatform.prefs.get('ngsi_server'), {
            use_user_fiware_token: true,
        });
    };

    var init = function init() {
        layout = new StyledElements.BorderLayout({'class': 'loading'});
        var fields = {
            "entity" : {
                label: 'Entity Id',
                type: 'select',
                initialEntries: [{
                    label: "-------------------",
                    value: ""
                }],
                required: true
            },
            "attribute" : {
                label: 'Attribute Name',
                type: 'select',
                initialEntries: [{
                    label: "-------------------",
                    value: ""
                }],
                required: true
            },
            "value" : {
                label: 'Attribute Value',
                type: 'text',
                required: true
            }
        };
        form = new StyledElements.Form(fields, {cancelButton: false});
        form.addEventListener("submit", updateEntity);
        form.fieldInterfaces.entity.inputElement.addEventListener('change', onEntityChange);
        form.fieldInterfaces.attribute.inputElement.addEventListener('change', onAttributeChange);
        layout.getCenterContainer().appendChild(form);

        layout.insertInto(document.body);

        //Create the error div
        error = document.createElement('div');
        error.setAttribute('class', 'alert alert-danger div_spaced');
        layout.getCenterContainer().appendChild(error);

        //Create the warn div
        info = document.createElement('div');
        info.setAttribute('class', 'alert alert-success div_spaced');
        layout.getCenterContainer().appendChild(info);

        hiddeStautsDivs();

        layout.repaint();
        create_ngsi_connection();
        doQuery();
    };

    var fail = function fail(msg) {
        error.innerHTML = msg;
        error.classList.remove('hidden');
        info.classList.add('hidden');
    }

    var complete = function complete(msg) {
        info.innerHTML = msg ? msg : 'Complete!';
        error.classList.add('hidden');
        info.classList.remove('hidden');
    }

    var hiddeStautsDivs = function hiddeStautsDivs() {
        info.classList.add('hidden');
        error.classList.add('hidden');
    }

    var onEntityChange = function onEntityChange(select) {
        var attribute_values = currentData[select.getValue()];

        if (attribute_values == null) {
            attribute_values = {};
        }

        var attributes = Object.keys(attribute_values);
        var old_attribute = form.fieldInterfaces.attribute.inputElement.getValue();
        var entries = [];

        for (var i = 0; i < attributes.length; i++) {
            if (['id', 'type'].indexOf(attributes[i]) === -1) {
                entries.push({value: attributes[i]});
            }
        }

        form.fieldInterfaces.attribute.inputElement.clear();
        form.fieldInterfaces.attribute.inputElement.addEntries(entries);
        if (attributes.indexOf(old_attribute) !== -1) {
            form.fieldInterfaces.attribute.inputElement.setValue(old_attribute);
        }
        onAttributeChange(form.fieldInterfaces.attribute.inputElement);
    };

    var onAttributeChange = function onAttributeChange(select) {
        var entity = form.fieldInterfaces.entity.inputElement.getValue();
        var attribute_values = currentData[entity];
        var attribute = form.fieldInterfaces.attribute.inputElement.getValue();

        form.fieldInterfaces.value.inputElement.setValue(attribute_values[attribute]);
    };

    var doQuery = function doQuery() {
        form.disable();
        hiddeStautsDivs();

        var id = {
            isPattern: true,
            id: MashupPlatform.prefs.get('entity_id_pattern')
        };

        var type = MashupPlatform.prefs.get('entity_types');
        if (type !== '') {
            id.type = type;
        }

        ngsi.query([
                id
            ],
            null,
            {
                flat: true,
                onSuccess: onQuerySuccess,
                onFailure: onQueryFail
            }
        );
    };

    var onQuerySuccess = function onQuerySuccess(data) {
        var entries = [];
        currentData = data;

        for (var key in data) {
            entries.push({value: key});
        }

        form.fieldInterfaces['entity'].inputElement.clear();
        form.fieldInterfaces['entity'].inputElement.addEntries(entries);
        onEntityChange(form.fieldInterfaces.entity.inputElement);

        form.enable();
    };

    var onQueryFail = function onQueryFail(e) {
        form.disable();
        fail('Fail querying the server: ' + e);
    }

    var updateEntity = function updateEntity(form, data) {
        form.disable();
        hiddeStautsDivs();

        var attributes =  [{
                'name': data.attribute,
                'contextValue': data.value
            }];

        if ('TimeInstant' in currentData[data.entity]) {
            attributes.push({
                'name': 'TimeInstant',
                'contextValue': (new Date()).toISOString()
            });
        }

        ngsi.updateAttributes([
                {
                    'entity': {'id': data.entity},
                    'attributes': attributes
                }
            ], {
                onSuccess: onUpdateAttributesSuccess,
                onFailure: onUpdateAttributesFail
            }
        );
    };

    var onUpdateAttributesFail = function onUpdateAttributesFail(e) {
        form.enable();
        fail('Fail updating Attribute: ' + e);
    }

    var onUpdateAttributesSuccess = function onUpdateAttributesSuccess() {
        form.enable();
        complete('Attribute updated correctly');
    };

    MashupPlatform.prefs.registerCallback(function (new_values) {
        if ('ngsi_server' in new_values) {
            create_ngsi_connection();
        }

        doQuery();
    }.bind(this));

    MashupPlatform.widget.context.registerCallback(function (newValues) {
        if (layout && "heightInPixels" in newValues) {
             layout.repaint();
        }
    }.bind(this));

    window.addEventListener("DOMContentLoaded", init, false);

})();

@@ -1,10 +1,11 @@
 /*
+ 
 global M
@@ -17,16 +17,17 @@
 Platform
+,
  StyledE
@@ -33,21 +33,23 @@
 Elements
+,
  NGSI
+ 
 */%0A%0A(fun
@@ -63,10 +63,9 @@
 ) %7B%0A
- 
 %0A
+
     
@@ -530,17 +530,16 @@
 %22entity%22
- 
 : %7B%0A    
@@ -806,17 +806,16 @@
 tribute%22
- 
 : %7B%0A    
@@ -1083,17 +1083,16 @@
  %22value%22
- 
 : %7B%0A    
@@ -2298,32 +2298,33 @@
 'hidden');%0A    %7D
+;
 %0A%0A    var comple
@@ -2480,32 +2480,33 @@
 'hidden');%0A    %7D
+;
 %0A%0A    var hiddeS
@@ -2620,32 +2620,33 @@
 'hidden');%0A    %7D
+;
 %0A%0A    var onEnti
@@ -4721,34 +4721,31 @@
 ldInterfaces
-%5B'
+.
 entity
-'%5D
 .inputElemen
@@ -4787,18 +4787,15 @@
 aces
-%5B'
+.
 entity
-'%5D
 .inp
@@ -5040,32 +5040,33 @@
 r: ' + e);%0A    %7D
+;
 %0A%0A    var update
@@ -5962,24 +5962,25 @@
  + e);%0A    %7D
+;
 %0A%0A    var on
@@ -6442,33 +6442,32 @@
 in newValues) %7B%0A
- 
             layo

Fix jshint problems