//
//  HandshakeRequest.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 <UIKit/UIKit.h>
#import "HandshakeRequest.h"

NSString * const HandshakeRequest_KEY_SESSION_TOKEN = @"tk";
NSString * const HandshakeRequest_KEY_API = @"api";
NSString * const HandshakeRequest_KEY_COMPRESSION_THRESHOLD = @"ct";
NSString * const HandshakeRequest_KEY_RECONNECTION_TOKEN = @"rt";
NSString * const HandshakeRequest_KEY_CLIENT_TYPE = @"cl";
NSString * const HandshakeRequest_KEY_MAX_MESSAGE_SIZE = @"ms";

@implementation HandshakeRequest


-(id)initWithApiVersion:(NSString *)apiVersion reconnectionToken:(NSString *)reconnectionToken clientDetails:(NSMutableString *)clientDetails
{
	if ((self = [super initWithId:BaseRequest_Handshake]))
    {
		[_sfso putUtfString:HandshakeRequest_KEY_API value:apiVersion];
        NSString *clientType;
        
        if([clientDetails length] == 0)
        {
            #if TARGET_OS_EMBEDDED || TARGET_IPHONE_SIMULATOR
                //get device system version 
                clientType = [NSString stringWithFormat:@"IOS version: %@", [[UIDevice currentDevice] systemVersion]]; 
            #else
                //piece together the version of the OS 
                //int maj, min, fix;
                NSProcessInfo *pinfo = [NSProcessInfo processInfo];
                NSString *version = [pinfo operatingSystemVersionString];
                if([[pinfo operatingSystemName] isEqualToString: @"NSMACHOperatingSystem"])
                {
                    clientType = [NSString stringWithFormat:@"Mac OSX: %@", version];
                }
                else
                {
                    clientType = [NSString stringWithFormat:@"%@ %@", [pinfo operatingSystemName], version];
                }
            #endif
        }
        else
        {
            clientType = (NSString *) clientDetails;
        }
        
		[_sfso putUtfString:HandshakeRequest_KEY_CLIENT_TYPE value:clientType];
		
		if (reconnectionToken != nil)
        {
			[_sfso putUtfString:HandshakeRequest_KEY_RECONNECTION_TOKEN value:reconnectionToken];
		}
	}
	
	return self;
}

@end
