/* Copyright (c) 2011 Artjom Popov */

#import "UTHistoryStory.h"

@implementation UTHistoryStory

- (id)initWithURL:(NSURL *)url description:(NSString *)description result:(NSString *)result
{
	self = [super init];
	
	if(self) {		
		self->m_URL = [url retain];
		self->m_description = [description retain];
		NSString *path = [NSString stringWithFormat:@"https://chart.googleapis.com/chart?cht=qr&chs=250x250&chl=%@&chld=H", result];
		self->m_imageURL = [[NSURL alloc] initWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
		NSLog(@"self->m_imageURL %@", [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
		self->m_id = [[NSString stringWithFormat:@"%@||%@||%@", self->m_URL, self->m_description, self->m_imageURL] retain];
	}
	
	return self;
}

- (id)initWithId:(NSString *)id
{
	self = [super init];
	
	if(self) {		
		NSArray *components = [id componentsSeparatedByString:@"||"];
		self->m_id = [id retain];
		if(components.count == 3) {
			self->m_URL = [[NSURL alloc] initWithString:[components objectAtIndex:0]];
			self->m_description = [[components objectAtIndex:1] retain];
			self->m_imageURL = [[NSURL alloc] initWithString:[components objectAtIndex:2]];
			NSLog(@"self->m_imageURL 22 %@", self->m_imageURL);
		} else {
			[self release];
			self = nil;
		}
	}

	return self;
}

@synthesize id = m_id;
@synthesize URL = m_URL;
@synthesize description = m_description;
@synthesize imageURL = m_imageURL;

#pragma mark NSObject

- (BOOL)isEqual:(UTHistoryStory *)story
{
	return [story.id isEqual:self->m_id];
}

- (void)dealloc
{
	[self->m_URL release];
	[self->m_imageURL release];
	[self->m_id release];
	[self->m_description release];
	
	[super dealloc];
}

@end
