/* Copyright (c) 2011 Artjom Popov */

#import "UTHistoryStory.h"
#import "UTMetadataController.h"
#import "UIColor+Additions.h"
#import "UIImage+Additions.h"
#import "UTDerivedImage.h"

#define UTMetadataControllerImageViewTag 43501
#define UTMetadataControllerTableViewTag 43503

@implementation UTMetadataController

- (UIImageView *)imageView
{
	return (UIImageView *)[self.view viewWithTag:UTMetadataControllerImageViewTag];
}

- (UITableView *)tableView
{
	return (UITableView *)[self.view viewWithTag:UTMetadataControllerTableViewTag];
}

- (IBAction)done:(id)sender
{
	[self dismissModalViewControllerAnimated:YES];
}

#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Default"];
	
	if(!cell) {	
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Default"] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;	
		cell.accessoryType = UITableViewCellAccessoryNone;	
	}
	
	UTDerivedImage *webImage = nil;
	CGFloat scale = ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) ?  [[UIScreen mainScreen] scale] : 1.0F;
	CGSize size = CGSizeMake(90.0F * scale, 90.0F * scale);
	NSURL *url = self->m_story.imageURL;
	
	if (!webImage && url) {
		webImage = [[[UTDerivedImage alloc] initWithURL:url revision:@"0" size:size] autorelease];
		[self->m_webImages setObject:webImage forKey:url];
		[webImage load];
	}
	
	self.imageView.image = webImage.image;
	cell.textLabel.font = [UIFont systemFontOfSize:14.0F];
	cell.textLabel.text = NSLocalizedString(@"Metadata.Add", nil);
	cell.textLabel.numberOfLines = 2;
	
	return cell;
}

#pragma mark UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[[UIApplication sharedApplication] openURL:self->m_story.URL];
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	return 45.0F;
}




#pragma mark UIViewController

- (void)loadView 
{

	UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0F, 0.0F, 320.0F, 480.0F)];
	view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
	view.backgroundColor = [UIColor groupTableViewBackgroundColor];
	
	NSString *title = self->m_story.description;
	UIFont *font = [UIFont boldSystemFontOfSize:12.0F];
	
	UILabel *titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(130.0F, 50.0F, 190.0F, 30.0F)] autorelease];
	titleLabel.font = font;
	titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	titleLabel.text = title;
	titleLabel.numberOfLines = 0;
	titleLabel.backgroundColor = [UIColor clearColor];
	titleLabel.textAlignment = UITextAlignmentLeft;
	[view addSubview:titleLabel];
	
	UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(20.0F, 20.0F, 90.0F, 90.0F)] autorelease];
	imageView.backgroundColor = [UIColor whiteColor];
	imageView.tag = UTMetadataControllerImageViewTag;
	[view addSubview:imageView];
	
	UITableView *tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0.0F, 150.0F, 320.0F, 480.0F) style:UITableViewStyleGrouped] autorelease];
	tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
	tableView.clipsToBounds = NO;
	tableView.dataSource = self;
	tableView.delegate = self;
	tableView.scrollEnabled = NO;
	{
		UIView *background = [[[UIView alloc] initWithFrame:tableView.frame] autorelease];
		background.backgroundColor = [UIColor groupTableViewBackgroundColor];
		tableView.backgroundView = background;
	}
	
	tableView.tag = UTMetadataControllerTableViewTag;
	[view addSubview:tableView];
	
	self.view = view;
	[view release];
}



#pragma mark NSObject

- (id)initWithStory:(UTHistoryStory *)story
{
	self = [super init];
	
	if(self) {
		self->m_webImages = [[NSMutableDictionary alloc] init];
		self->m_story = [story retain];
		
		self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Metadata.Done", nil) style:UIBarButtonItemStylePlain target:self action:@selector(done:)] autorelease];
		
	}
	
	
	return self;
}

- (void)dealloc 
{
	[self->m_story release];
	[self->m_webImages release];
	
    [super dealloc];
}

@end
