//
//  PacketHeader.m
//  SFS2X
//
//  Original development by Infosfer Game Technologies Ltd. | http://www.infosfer.com.
//
//  Maintained and developed by A51 Integrated.
//  Copyright 2012 A51 Integrated | http://a51integrated.com. All rights reserved.
//

#import "PacketHeader.h"


@implementation PacketHeader

@synthesize expectedLen = _expectedLen;
@synthesize binary = _binary;
@synthesize compressed = _compressed;
@synthesize encrypted = _encrypted;
@synthesize blueBoxed = _blueBoxed;
@synthesize bigSized = _bigSized;


-(id)initWithParams:(BOOL)encrypted compressed:(BOOL)compressed blueBoxed:(BOOL)blueBoxed bigSized:(BOOL)bigSized {
	
	self = [super init];
	
	_expectedLen = -1;
	_binary = YES;
	_encrypted = encrypted;
	_compressed = compressed;
	_blueBoxed = blueBoxed;
	_bigSized = bigSized;
	
	return self;
}

+(PacketHeader *)fromBinary:(NSInteger)headerByte {
	
	PacketHeader *newInstance = [[PacketHeader alloc] initWithParams:(headerByte & 0x40) > 0 compressed:(headerByte & 0x20) > 0 
																 blueBoxed:(headerByte & 0x10) > 0 bigSized:(headerByte & 0x8) > 0];
	return newInstance;
	
}

-(NSInteger)encode {
	
	int headerByte = 0;
	
	if(_binary) {
		headerByte += 0x80;
	}
	if(_encrypted) {
		headerByte += 0x40;
	}
	if(_compressed) {
		headerByte += 0x20;
	}
	if(_blueBoxed) {
		headerByte += 0x10;
	}		
	if(_bigSized) {
		headerByte += 0x08;
	}
	
	return headerByte;
}

-(NSString *)boolToString:(BOOL)input {
	return input ? @"true" : @"false";
}

-(NSString *)description {
	
	NSString *buf = @"---------------------------------------------\n";
	
	buf = [buf stringByAppendingFormat:@"Binary: \t%@\n", [self boolToString:_binary]];
	buf = [buf stringByAppendingFormat:@"Compressed: \t%@\n", [self boolToString:_compressed]];
	buf = [buf stringByAppendingFormat:@"Encrypted: \t%@\n", [self boolToString:_encrypted]];
	buf = [buf stringByAppendingFormat:@"BlueBoxed: \t%@\n", [self boolToString:_blueBoxed]];
	buf = [buf stringByAppendingFormat:@"BigSized: \t%@\n", [self boolToString:_binary]];
	buf = [buf stringByAppendingString:@"---------------------------------------------\n"];
	
	return buf;
}

@end
