/*!
 * Protocol class
 * @version 1.0
 * @author M.F.Endenburg
 * @copyright (c) Denbel Systems, 2008
 */

// load namespace
Denbel.load( 'rpc.Protocol' );
Denbel.load( 'rpc.ProtocolMessage' );
Denbel.load( 'rpc.ProtocolParameter' );

/**
 * constructor
 * @param object configuration
 * @return void
 */
Denbel.rpc.Protocol = function( config )
{
    this.cfg = config;
    this.init();
};

// prototype
Denbel.rpc.Protocol.prototype =
{
    // fields
    cfg: null,
    
    /**
     * initializer
     * @return bool
     */
    init: function()
    {
      if( !this.cfg.url )
      {
          this.cfg.url = Denbel.Website.getDomain() + '/proxy.php';
      }
      
      return true;
    },
    
    /**
     * Creates a protocol message
     * @param string initial message value
     * @return Denbel.rpc.ProtocolMessage
     */
    createMessage: function( value )
    {
        return new Denbel.rpc.ProtocolMessage( value );
    },
    
    /**
     * Calls the service
     * @param Denbel.rpc.ProtocolMessage
     * @param object callback literal
     * @return void
     */
    callService: function( message, callback )
    {
        return;
    },
    
    /**
     * Converts this object to its string representation
     * @return string
     */
    toString: function()
    {
        return 'Denbel.rpc.Protocol';
    }
};

// --------------------------------------------------------------------------- //

/**
 * constructor
 * @param string value
 * @return void
 */
Denbel.rpc.ProtocolMessage = function( value )
{
    this.value = value;
    this.params = new Array();
    
    if( value )
    {
        this.createAndAddParameter( 'action', value );
    }
};

// prototype
Denbel.rpc.ProtocolMessage.prototype =
{
    // fields
    value: null,
    params: null,

    /**
     * Adds a parameter to the message
     * @param Denbel.rpc.ProtocolParameter
     * @return int the index at where the parameter was inserted
     */
    addParameter: function( param )
    {
       if( !param )
       {
           return null;
       }
       
       if( !this.params || !YAHOO.lang.isArray( this.params ) )
       {
           this.params = new Array();
       }
       
       this.params.push( param );
       return ( this.params.length - 1 );
    },
    
    /**
     * Create and add parameter
     * @param string name
     * @param mixed value
     * @return int
     */
    createAndAddParameter: function( name, value )
    {
        var p = new Denbel.rpc.ProtocolParameter( name, value );
        return this.addParameter( p );
    },
    
    /**
     * Adds a complete form to the message
     * @param mixed string ID of the Form element or an HTMLFormElement
     * @return void
     */
    addForm: function( form )
    {
        var form = YAHOO.util.Dom.get( form );
        
        if( !form )
        {
            return null;
        }
        
        var p = null;
        
        for( var i = 0; i < form.elements.length; i++ )
        {
            p = new Denbel.rpc.ProtocolParameter( form.elements[i].name, form.elements[i].value );
            this.addParameter( p );
        }
    },
    
/**
 * Returns an Array of all parameters
 * @return Array
 */
    getParameters: function()
    {
        if( !this.params || !YAHOO.lang.isArray( this.params ) )
        {
            return new Array();
        }

        return this.params;
    },
    
    /**
     * Converts this object to its string representation
     * @return string
     */
    toString: function()
    {
        return 'Denbel.rpc.ProtocolMessage';
    }
};

// --------------------------------------------------------------------------- //

/**
 * constructor
 * @return void
 */
Denbel.rpc.ProtocolParameter = function( name, value )
{
    this.name = name;
    this.value = value;
};

// prototype
Denbel.rpc.ProtocolParameter.prototype =
{
    // fields
    value: null,
    name: null,
    
    /**
     * Returns the name of this parameter
     * @return string
     */
    getName: function()
    {
        return this.name;
    },
    
    /**
     * Returns the value of this parameter
     * @return mixed
     */
    getValue: function()
    {
        return this.value;
    },
    
    /**
     * Converts this object to its string representation
     * @return string
     */
    toString: function()
    {
        return 'Denbel.rpc.ProtocolParameter';
    }
};
