/*
 * @author: ucla-cs
 * This class represents SignedInfo Object
 * This keeps information about the ContentObject Signature
 */

var ContentType = {DATA:0, ENCR:1, GONE:2, KEY:3, LINK:4, NACK:5};

var ContentTypeValue = {0:0x0C04C0, 1:0x10D091,2:0x18E344,3:0x28463F,4:0x2C834A,5:0x34008A};
var ContentTypeValueReverse = {0x0C04C0:0, 0x10D091:1,0x18E344:2,0x28463F:3,0x2C834A:4,0x34008A:5};


var SignedInfo = function SignedInfo(_publisher,_timestamp,_type,_locator,_freshnessSeconds,_finalBlockID){

	//TODO, Check types
    
    this.Publisher = _publisher; //PublisherPublicKeyDigest
    this.Timestamp=_timestamp; // CCN Time
    this.Type=_type; // ContentType
    this.Locator =_locator;//KeyLocator
    this.FreshnessSeconds =_freshnessSeconds; // Integer
    this.FinalBlockID=_finalBlockID; //byte array

};


SignedInfo.prototype.decode = function( decoder){

		decoder.readStartElement( this.getElementLabel() );
		
		if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
			this.Publisher = new PublisherPublicKeyDigest();
			this.Publisher.decode(decoder);
		}

		if (decoder.peekStartElement(CCNProtocolDTags.Timestamp)) {
			this.Timestamp = decoder.readDateTime(CCNProtocolDTags.Timestamp);
		}

		if (decoder.peekStartElement(CCNProtocolDTags.Type)) {
			binType = decoder.readBinaryElement(CCNProtocolDTags.Type);//byte [] 
		
		
			//TODO Implement Type of Key Reading
			
			//this.Type = valueToType(binType);
			
			
			//TODO Implement Type of Key Reading
			
			/*
			if (null == this.Type) {
				throw new Exception("Cannot parse signedInfo type: bytes.");
			}
			
		} else {
			this.Type = ContentType.DATA; // default*/
		}
		
		if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
			this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
		}
		
		if (decoder.peekStartElement(CCNProtocolDTags.FinalBlockID)) {
			this.FinalBlockID = decoder.readBinaryElement(CCNProtocolDTags.FinalBlockID);
		}
		
		if (decoder.peekStartElement(CCNProtocolDTags.KeyLocator)) {
			this.Locator = new KeyLocator();
			this.Locator.decode(decoder);
		}
				
		decoder.readEndElement();
};

SignedInfo.prototype.encode = function( encoder)  {
		if (!this.validate()) {
			throw new Exception("Cannot encode : field values missing.");
		}
		encoder.writeStartElement(this.getElementLabel());
		
		if (null!=this.Publisher) {
			this.Publisher.encode(encoder);
		}

		if (null!=this.Timestamp) {
			encoder.writeDateTime(CCNProtocolDTags.Timestamp, this.Timestamp);
		}
		
		if (null!=this.Type && this.Type !=0) {
			
			encoder.writeElement(CCNProtocolDTags.Type, this.Type);
		}
		
		if (null!=this.FreshnessSeconds) {
			encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.FreshnessSeconds);
		}

		if (null!=this.FinalBlockID) {
			encoder.writeElement(CCNProtocolDTags.FinalBlockID, this.FinalBlockID);
		}

		if (null!=this.Locator) {
			this.Locator.encode(encoder);
		}

		encoder.writeEndElement();   		
};
	
SignedInfo.prototype.getElementLabel = function() { 
	return CCNProtocolDTags.SignedInfo;
};

SignedInfo.prototype.validate = function() {
		// We don't do partial matches any more, even though encoder/decoder
		// is still pretty generous.
		if (null ==this.Publisher || null==this.Timestamp ||null== this.Locator)
			return false;
		return true;
};

