/*
  Copyright (C) 2005-2006 by Domenico De Felice

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

  Authors: Domenico De Felice, <dfdcod [at] gmail [dot] com>
*/

Tratto.xmpp = { };

Tratto.xmpp.init = function() {
    Tratto.xmpp.dispatcher.init();
};

Tratto.xmpp.received = function(command) {
    var uuid = String(command.@uuid);
    
    var index;
    if ((index = this.dispatcher._sent.indexOf(uuid)) != -1) {
        this.dispatcher._sent.splice(index, 1);
        return;
    }

    var command_name = command.name().localName;

    switch(command_name) {
    case 'updates':
        for each (var update in command.ns_app::*) {
            var update_name = update.name().localName;

            switch (update_name) {
            case 'insert':
                var parent_id = update.@into;

                var parent = _(parent_id);

                var new_element;
                for each (new_element in update.ns_svg::*) {
                    new_element = toDOM(new_element);
                }

                _a(parent, new_element);

                break;

            case 'remove':
                var element = _(update.@what);

                _r(element);

                break;

            case 'change':
                var ele = _(update.@what);

                var attr = update.@attr;

                var value = update.@value;

                _s(ele, attr, value);

                break;

            default:
                throw new Error('Unknown update "' + update_name + '".');
                break;
            }
        }
        break;
    default:
        throw new Error('Unknown command "' + command_name + '".');
        break;
    }
};


// ===========================================
// Dispatcher

Tratto.xmpp.dispatcher = {
    _period: 1500,

    _sent: [],

    _updates: [],

    init: function() {
        window.setInterval(this._dispatch, this._period);
    },

    _dispatch: function() {
        var upds = Tratto.xmpp.dispatcher._updates;
        Tratto.xmpp.dispatcher._updates = [];

        if (upds.length == 0) return;

        var uuid, attr, command, str_xml = "";

        for each (var update in upds) {
            command = <{update.command}/>;

            for (attr in update) {
                if ((attr != "type") &&
                    (attr != "content")) {
                    command["@" + attr] = update[attr];
                }
            }
            if (update.content) command.content = update.content;
            str_xml += command.toXMLString();
        }

        uuid = createUUID();
        str_xml = "<updates uuid=\"" + uuid + "\">" + str_xml + "</updates>";

        Tratto.xmpp.dispatcher._sent.push(uuid);

        XMPP4Moz.sendCommand(str_xml);
    },

    dispatch: function(update) {
        if (update.command == "insert" || update.command == "remove") {
            this._updates.push(update);
            return;
        }

        var c;
        for each (c in this._updates) {
            if (c.command == "change" &&
                c.what == update.what &&
                c.attr == update.attr) {
                c.value = update.value;
                return;
            }
        }
        this._updates.push(update);
    }
};

