/*
################################################################################
    JavaScript Client for XMPP (jc4xmpp)

    Copyright (C) 2012  Dirk Woestefeld

    License:
       LGPL v3: http://www.gnu.org/licenses/lgpl.html
       
       A Copy of the License can be found in the directory of this project.    

################################################################################
*/



/**
 * XMPP Ping (xep-0199)
 *  <br/>
 * @module PingPong
 * @param {Main} main
 * @requires jc4xmpp
 **/
jc4xmpp.Module.PingPong = function(main) {
    this.ns = "urn:xmpp:ping";
    this._nsMap = {};
    this._nsMap[this.ns] = 2;
        
    /**
     * @private
     * @expose
     */
    this.$CTRL = function(cmd,data){
        switch(cmd){
            case "get_disco_feature":
                return this._nsMap;
            break;
            case "set_feature_state":
                this._nsMap[data["name"]] = data["state"];
            break;
            case "get_feature_state":
                return this._nsMap[data];
            break;
            default:
                if(LOGGING) Logger.debug(cmd + " not handled by PingPong");
            break;
            
        }
        return undefined;
    };
    
    this.$HANDLE_FEATURE = function(stanza, ns) {
        if (ns === this.ns) {
            stanza.convertToResult({});
            delete stanza.data.ping;
            this.$sendStanza(stanza);
        }
    };
    
    /**
     * Send a xmpp-ping to the specified JID.
     * 
     * @method module:PingPong#sendPing
     * @param {JID} jid
     * @param {loadStateCallback} clb - Callback to get the result for the ping.
     * @expose
     */
    this.sendPing = function(jid,clb){
        var iq = new jc4xmpp.Classes.Stanza({"iq":{"@to=":jid, "@type":'get',"ping":{"@xmlns":this.ns}}});
        var rp = {
            clb:clb,
            parse: function(pkg){
                this.clb(pkg.from(),pkg.getErrorData());
            }
        };        
        this.sendStanze(iq,rp.parse.bind(this));
    };
};
