/* Copyright (c) 2011 Artjom Popov */

#import "UTApplication.h"
#import "UTDerivedImage.h"
#import "UTHistoryCell.h"
#import "UTHistoryStory.h"
#import "UTHistoryController.h"
#import "UTHistoryManager.h"
#import "UTMetadataController.h"

#define UTHistoryControllerTableView 1000

static NSString *UTHistoryControllerTableViewCell = @"TableCell";

@implementation UTHistoryController

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


- (void)historyDidChange:(NSNotification *)notification
{
	[self.tableView reloadData];
}

- (IBAction)edit:(id)sender 
{ 
    BOOL editing = !self.tableView.editing; 
    if(editing) {
		self.navigationItem.rightBarButtonItem.title = NSLocalizedString(@"History.Done", nil);
		self.navigationItem.rightBarButtonItem.style = UIBarButtonItemStyleDone;
	} else {
		self.navigationItem.rightBarButtonItem.title = NSLocalizedString(@"History.Edit", nil);
		self.navigationItem.rightBarButtonItem.style = UIBarButtonItemStylePlain;
		self.navigationItem.rightBarButtonItem.enabled = [UTHistoryManager sharedManager].stories.count > 0 ? YES : NO;
	}
    [self.tableView setEditing:editing animated:YES];  
}



#pragma mark UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UTHistoryCell *cell = (UTHistoryCell *)[tableView dequeueReusableCellWithIdentifier:UTHistoryControllerTableViewCell];
	UTHistoryStory *story = [[UTHistoryManager sharedManager].stories objectAtIndex:indexPath.row];
	
	if(!cell) {	
		cell = [[[UTHistoryCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:UTHistoryControllerTableViewCell] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;	
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;	
	}
	
	cell.textLabel.font = [UIFont systemFontOfSize:14.0F];
	cell.textLabel.text = story.description;
	cell.textLabel.numberOfLines = 2;

	NSURL *url = story.imageURL;
	
	UTDerivedImage *webImage = [self->m_webImages objectForKey:url];
	CGFloat scale = ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) ?  [[UIScreen mainScreen] scale] : 1.0F;
	CGSize size = CGSizeMake(90.0F * scale, 90.0F * scale);

	if (!webImage && url) {
		webImage = [[[UTDerivedImage alloc] initWithURL:url revision:@"0" size:size] autorelease];
		[self->m_webImages setObject:webImage forKey:url];
		[webImage load];
	}

	cell.imageView.image = webImage.image;
	
	return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [UTHistoryManager sharedManager].stories.count;
}

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


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(editingStyle == UITableViewCellEditingStyleDelete) {
		UTHistoryStory *story = [[UTHistoryManager sharedManager].stories objectAtIndex:indexPath.row];
		[[UTHistoryManager sharedManager] removeStory:story];
		[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
	}
}

#pragma mark UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	UTHistoryStory *story = [[UTHistoryManager sharedManager].stories objectAtIndex:indexPath.row];
	
	UTMetadataController *metadataController = [[UTMetadataController alloc] initWithStory:story];
	[self.navigationController presentModalViewController:[[[UINavigationController alloc] initWithRootViewController:metadataController] autorelease] animated:YES];
	[metadataController release];
	
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark UIViewController

- (void)loadView
{
	UIView *view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
	view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
	view.backgroundColor = [UIColor whiteColor];
	

	UITableView *tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0.0F, 0.0F, 320.0F, 480.0F)] autorelease];
	tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	tableView.dataSource = self;
	tableView.delegate = self;
	tableView.backgroundColor = [UIColor clearColor];
	tableView.rowHeight = 90.0F;
	tableView.tag = UTHistoryControllerTableView;
	[view addSubview:tableView];

	self.view = view;
}

- (void)viewWillAppear:(BOOL)animated
{
	self.navigationItem.rightBarButtonItem.enabled = [UTHistoryManager sharedManager].stories.count > 0 ? YES : NO;
		

	
	[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
//	UTApplication *app = [UTApplication sharedApplication];
//	[[UTApplication sharedApplication] openURL:app.url];
	
	[super viewDidAppear:animated];
}


#pragma mark NSObject

- (id)init
{
	self = [super init];
	
	if(self) {
		self->m_webImages = [[NSMutableDictionary alloc] init];
		UIBarButtonItem *editButton = self.editButtonItem;  
		[editButton setTarget:self]; 
		[editButton setAction:@selector(edit:)]; 
		self.navigationItem.rightBarButtonItem = editButton;
		self.navigationItem.rightBarButtonItem.title = NSLocalizedString(@"History.Edit", nil);	
		
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(historyDidChange:) name:UTHistoryManagerDidChangeNotification object:nil];

	}
	
	return self;
}



- (void)dealloc 
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[self->m_webImages release];
	
    [super dealloc];
}


@end
