//
//  BookmarksViewController.m
//  Bookmarks
//
//  Created by Lieven Dekeyser on 10/12/09.
//  Copyright 2009 Lieven Dekeyser. All rights reserved.
//

#import "BookmarksViewController.h"
#import "BrowserViewController.h"

static BookmarksViewController * sCurrentInstance = nil;

@implementation BookmarksViewController


+ (BookmarksViewController *)currentInstance
{
	return sCurrentInstance;
}

- (id)init
{
	if (self = [super initWithStyle:UITableViewStylePlain])
	{
		self.title = @"My Bookmarks";
		
		NSDictionary * googleBookmark = [NSDictionary dictionaryWithObjectsAndKeys:
			@"Google", @"name",
			@"http://www.google.com", @"link",
			nil];
		
		mBookmarks = [[NSMutableArray alloc] initWithObjects:googleBookmark, nil];
		
		mHistory = [[NSMutableArray alloc] init];
		
		sCurrentInstance = self;
	}
	return self;
}

- (void)dealloc
{
	if (self == sCurrentInstance) sCurrentInstance = nil;
	
	[mBookmarks release];
	[mHistory release];
	[super dealloc];
}


- (void)addHistoryItem:(NSDictionary *)inHistoryItem
{
	[mHistory insertObject:inHistoryItem atIndex:0];
	[self.tableView reloadData];
}


#pragma mark Table view methods

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


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	switch (section)
	{
		case 0:
		{
			return [mBookmarks count];
		}
		case 1:
		{
			return [mHistory count];
		}
		default:
		{
			return 0;
		}
	}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	switch (section)
	{
		case 0:
		{
			return @"Bookmarks";
		}
		case 1:
		{
			return @"History";
		}
		default:
		{
			return nil;
		}
	}
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	
	NSArray * links = indexPath.section == 0 ? mBookmarks : mHistory;
	NSDictionary * link = [links objectAtIndex:indexPath.row];
	
	cell.textLabel.text = [link objectForKey:@"name"];
	cell.detailTextLabel.text = [link objectForKey:@"link"];
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	
	NSArray * links = indexPath.section == 0 ? mBookmarks : mHistory;
	NSDictionary * link = [links objectAtIndex:indexPath.row];
	
	BrowserViewController * browser = [[[BrowserViewController alloc] initWithURLString:[link objectForKey:@"link"]] autorelease];
	[self.navigationController pushViewController:browser animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

@end

