/* Copyright (c) 2010 Indilo Wireless OÜ */

#import "TKCommentController.h"
#import "TKQualifiersController.h"
#import "UIColor+Additions.h"

#define TKQualifiersControllerTableViewTag 1000

@implementation TKQualifiersController

@dynamic tableView;

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

- (IBAction)next:(id)sender
{
	[self.navigationController pushViewController:[[[TKCommentController alloc] init] autorelease] animated:YES];
}

#pragma mark UITableViewDataSource

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return self->m_qualifiers.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSString *key = [self->m_statuses.allKeys objectAtIndex:indexPath.row];
	BOOL status = [[self->m_statuses objectForKey:key] boolValue];
	
	UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
	
	if (!cell) 
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;
	}
	
	cell.textLabel.text = [self->m_qualifiers objectForKey:key];
	cell.accessoryType = status ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
	
	return cell;
}

#pragma mark UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSString *key = [self->m_statuses.allKeys objectAtIndex:indexPath.row];
	BOOL status = [[self->m_statuses objectForKey:key] boolValue];
	
	for (NSString *qualifierKey in self->m_statuses.allKeys) {
		if ([qualifierKey isEqualToString:key]) {
			if (!self->m_allowMultipleSelections) {
				[self->m_statuses setObject:[NSNumber numberWithBool:1] forKey:qualifierKey];
			} else {
				[self->m_statuses setObject:[NSNumber numberWithBool:!status] forKey:qualifierKey];
			}
		} else {
			if (!self->m_allowMultipleSelections) {
				[self->m_statuses setObject:[NSNumber numberWithBool:0] forKey:qualifierKey];
			}
		}

	}
	
	[tableView reloadData];
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark UIView

- (void)loadView
{
	UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0F, 0.0F, 320.0F, 480.0F)];
	
	view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	view.backgroundColor = [UIColor themeBackgroundColor];
	
	UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0F, 0.0F, 320.0F, 480.0F) style:UITableViewStyleGrouped];
	tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
	tableView.backgroundColor = [UIColor clearColor];
	tableView.dataSource = self;
	tableView.delegate = self;
	tableView.tag = TKQualifiersControllerTableViewTag;
	[view addSubview:tableView];
	[tableView release];
	
	self.view = view;
	[view release];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	self.navigationItem.hidesBackButton = NO;
	self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil) style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Next", nil) style:UIBarButtonItemStylePlain target:self action:@selector(next:)] autorelease];
}

#pragma mark NSObject

- (id)init
{
	self = [super init];
	
	if(self) {
		self.navigationItem.title = NSLocalizedString(@"Event.Qualifiers", nil);
		
		// TESTING!
		NSMutableDictionary *statuses = [NSMutableDictionary dictionary];
		self->m_qualifiers = [[NSDictionary dictionaryWithObjectsAndKeys:@"Qualifier 1", @"q1", @"Qualifier 2", @"q2", @"Qualifier 3", @"q3", nil] retain];
		
		// TODO: Default selections
		for (NSString *key in self->m_qualifiers.allKeys) {
			[statuses setObject:[NSNumber numberWithBool:0] forKey:key];
		}
		
		self->m_statuses = [statuses retain];
		self->m_allowMultipleSelections = YES;
	}
	
	return self;
}

- (void)dealloc 
{
	[self->m_qualifiers release];
	[self->m_statuses release];
    [super dealloc];
}

@end
