/*
 *     Copyright (c) 2015 CoNWeT Lab., Universidad Politécnica de Madrid
 *
 *     This file is part of Wirecloud Platform.
 *
 *     Wirecloud Platform is free software: you can redistribute it and/or
 *     modify it under the terms of the GNU Affero General Public License as
 *     published by the Free Software Foundation, either version 3 of the
 *     License, or (at your option) any later version.
 *
 *     Wirecloud is distributed in the hope that it will be useful, but WITHOUT
 *     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 *     License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with Wirecloud Platform.  If not, see
 *     <http://www.gnu.org/licenses/>.
 *
 */

/*global MashupPlatform*/

(function () {

    "use strict";

    var platform = window.parent;
    var Wirecloud = platform.Wirecloud;
    var resource = MashupPlatform.resource;
    var resource_element = resource.workspace.getIWidget(resource.id).content;
    var counter = 1;

    // Workspace wrapper
    var Workspace = function Workspace(workspace) {
        Object.defineProperties(this, {
            'owner': {value: workspace.creator},
            'name': {value: workspace.name},
        });
    };

    MashupPlatform.mashup.addWidget = function addWidget(ref, options) {

        if (ref == null) {
            throw new TypeError('missing widget_ref parameter');
        }

        // default options
        options = Wirecloud.Utils.merge({
            title: null,
            refposition: null,
            permissions: null,
            preferences: {},
            properties: {},
            top: "0px",
            left: "0px",
            width: null, //auto
            height: null
        }, options);

        options.permissions = Wirecloud.Utils.merge({
                close: true,
                rename: false
        }, options.permissions);

        var widget_def = Wirecloud.LocalCatalogue.getResourceId(ref);
        var widget_title = options.title ? options.title : widget_def.title;
        var layout = Wirecloud.activeWorkspace.getActiveDragboard().freeLayout;

        if (options.refposition != null) {
            var current_position = Wirecloud.Utils.getRelativePosition(resource_element, resource.tab.wrapperElement);
            options.left = (current_position.x + options.refposition.left - layout.dragboardLeftMargin) + "px";
            options.top = (current_position.y + options.refposition.bottom - layout.dragboardTopMargin) + "px";
        }

        var iwidgetinfo = {
            id: resource.id + '/' + counter++,
            name: widget_title,
            volatile: true,
            permissions: options.permissions,
            properties: options.properties,
            preferences: options.preferences,
            top: options.top,
            left: options.left,
            width: options.width,
            height: options.height
        };
        var iwidget = new platform.IWidget(widget_def, layout, iwidgetinfo);
        Wirecloud.activeWorkspace.getActiveDragboard().addIWidget(iwidget);

        /*
        var tmp_enpdoint = new Wirecloud.wiring.WidgetTargetEndpoint(resource, {
            name: 'status-' + iwidgetinfo.id,
        });
        iwidget.internal_iwidget.outputs["call-state"].connect(tmp_enpdoint);
        var registered_count = 0;
        tmp_enpdoint.callback = function (state) {
            if (state === 'REGISTERED') {
                iwidget.internal_iwidget.inputs["user-id"].propagate("tmendoza");
                iwidget.internal_iwidget.inputs["call-user"].propagate("anonymous");
            }
        };
        */
        return iwidget;
    };

    var onCreateWorkspaceSuccess = function onCreateWorkspaceSuccess(workspace) {
        this(new Workspace(workspace));
    };

    var createWorkspace = function createWorkspace(options) {
        if (options != null && typeof options.onSuccess === 'function') {
            options.onSuccess = onCreateWorkspaceSuccess.bind(options.onSuccess);
        }

        Wirecloud.createWorkspace(options);
    };

    Object.defineProperties(MashupPlatform.mashup, {
        createWorkspace: {value: createWorkspace}
    });

})();

@@ -1135,16 +1135,285 @@
 r = 1;%0A%0A
+    // Widget wrapper%0A    var Widget = function Widget(real_widget) %7B%0A        Object.defineProperties(this, %7B%0A            'close': %7B%0A                value: function close() %7B%0A                    real_widget.remove();%0A                %7D%0A            %7D%0A        %7D);%0A    %7D;%0A%0A
     // W
@@ -1421,32 +1421,32 @@
 rkspace wrapper%0A
-
     var Workspac
@@ -1643,30 +1643,12 @@
     
-MashupPlatform.mashup.
+var 
 addW
@@ -2915,25 +2915,24 @@
         var 
-i
 widgetinfo =
@@ -3331,17 +3331,16 @@
     var 
-i
 widget =
@@ -3381,17 +3381,16 @@
 layout, 
-i
 widgetin
@@ -3460,17 +3460,16 @@
 IWidget(
-i
 widget);
@@ -4043,23 +4043,34 @@
  return 
-i
+new Widget(
 widget
+)
 ;%0A    %7D;
@@ -4478,24 +4478,24 @@
 s);%0A    %7D;%0A%0A
-
     Object.d
@@ -4531,24 +4531,63 @@
 m.mashup, %7B%0A
+        addWidget: %7Bvalue: addWidget%7D,%0A
         crea

Improve addWidget support