/* Copyright (c) 2011 Artjom Popov */

#import "UTApplication.h"
#import "UTScannerController.h"
#import "UTHistoryStory.h"
#import "UTHistoryController.h"
#import "UTHistoryManager.h"

#define UTApplicationPhoneNumberAlertViewTag 2000
#define UTApplicationPhoneNumberTextFieldTag 2001

@implementation UTApplication

+ (UTApplication *)sharedApplication
{
	return (UTApplication *)[super sharedApplication];
}

@synthesize controller = m_controller;
@synthesize window = m_window;
@synthesize openURL = m_openURL;
@synthesize url = m_url;

#pragma mark AVAudioPlayerDelegate

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
	//NSLog(@"Audioplayer did finish %d", flag);
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
	//NSLog(@"Audioplayer did fail");
}

#pragma mark UTScannerControllerDelegate

- (BOOL)scannerController:(UTScannerController *)scannerController shouldFinishPickingResult:(UTScannerResult *)result
{
	return YES;
}

- (void)scannerController:(UTScannerController *)scannerController didFinishPickingResult:(NSString *)result
{	
	if(self->m_controller.selectedIndex == 0) {
		[self->m_beeper play];
		
		NSInteger loc = [result rangeOfString:@"#"].location;
		NSString *description = [result substringFromIndex:loc + 1];
		NSString *urlString = [NSString stringWithFormat:@"webcal%@", [result substringWithRange:NSMakeRange(4, loc - 4)]];
		NSURL *url = [[[NSURL alloc] initWithString:urlString] autorelease];
		
		UTHistoryStory *story = [[UTHistoryStory alloc] initWithURL:url description:description result:result];
		[[UTHistoryManager sharedManager] addStory:story];
		
		if(!self->m_openURL) self->m_openURL = YES;
		self.url = url;
		self->m_controller.selectedIndex = 1;
	}
}

- (void)scannerControllerDidCancel:(UTScannerController *)scannerController
{
	[scannerController dismissModalViewControllerAnimated:YES];
}



#pragma mark UIApplicationDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
	[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
	
	NSMutableArray *controllers = [NSMutableArray array];
	
	UTScannerController *scannerController = [[[UTScannerController alloc] init] autorelease];
	scannerController.delegate = self;
	scannerController.tabBarItem = [[UITabBarItem alloc]
							 initWithTitle:NSLocalizedString(@"Tab.Scanner", nil)
							 image:[UIImage imageNamed:@"camera.png"]
							 tag:0];
	[controllers addObject:scannerController];
	
	UTHistoryController *historyController = [[[UTHistoryController alloc] init] autorelease];
	historyController.tabBarItem = [[UITabBarItem alloc]
							 initWithTitle:NSLocalizedString(@"Tab.History", nil)
							 image:[UIImage imageNamed:@"barcode.png"]
							 tag:1];
	UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:historyController] autorelease];	
	[controllers addObject:navigationController];
	
	
	self->m_controller.viewControllers = controllers;
	
    [self->m_window addSubview:self->m_controller.view];
    [self->m_window makeKeyAndVisible];
	
	return YES;
}


#pragma mark NSObject

- (id)init
{
	self = [super init];
	
	if(self) {
		self->m_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
		self->m_controller = [[UITabBarController alloc] init];
		
		self.delegate = self;
		
		self->m_beeper = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Beep" ofType:@"wav"]] error:nil];
		self->m_beeper.delegate = self;
	}
	
	return self;
}

- (void)dealloc 
{
    [self->m_controller release];
    [self->m_window release];
	
	[self->m_beeper stop];
	[self->m_beeper release];
	
    [super dealloc];
}

@end
