#import "ManageTunesController.h"
#import "ImportABCController.h"
#import "WebServerController.h"
#import "WebViewController.h"
#import "TuneViewController.h"
#import "BluetoothController.h"
#import "AppDelegate.h"

@implementation ManageTunesController

#define kCellIdentifier @"com.jhlabs.Tunebook.ManageCellIdentifier"

enum {
  kImportTunes = 0,
  kDeleteTunes,
#if !TARGET_IPAD
  kNewTune,
#endif
  kNumRows,
};

#define kShareTunes   0

-(void)viewDidLoad
{
//	self = [super init];
//	if ( self != nil ) {
#if BLUETOOTH
    gameKitAvailable = NSClassFromString( @"GKPeerPickerController" ) != nil;
#else
    gameKitAvailable = false;
#endif
  tableView.scrollsToTop = NO;
//	}
//	return self;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return YES;
}

- (void)didReceiveMemoryWarning
{
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview.
	// Release anything that's not essential, such as cached data.
}

-(void)dealloc
{
	[super dealloc];
}

#pragma mark UITableView delegate methods

//- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
//{
//	return UITableViewCellAccessoryDisclosureIndicator;
//}

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
#if !TARGET_IPAD
	UINavigationController *navigationController = appDelegate.navigationController;
#endif

	[tv deselectRowAtIndexPath:indexPath animated:YES];

  int row = indexPath.row;
  if ( indexPath.section == 0 ) {
    if ( row == kImportTunes ) {
      AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
      NetworkStatus netStatus = [appDelegate.reachability currentReachabilityStatus];
      if ( netStatus == NotReachable ) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You can't browse the Web because you have no Internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
      } else {
        WebViewController *controller = [[WebViewController alloc] initWithNibName:@"WebView" bundle:nil];
#if TARGET_IPAD
        [appDelegate.tuneViewController dismissPopoverController];
        [appDelegate.tuneViewController presentModalViewController:controller animated:YES];
#else
        [navigationController pushViewController:controller animated:YES];
#endif
      }
    } else if ( row == kDeleteTunes ) {
      UIActionSheet *sheet = [[UIActionSheet alloc]
        initWithTitle:@"Delete Tunes"
        delegate:self
        cancelButtonTitle:@"Don't Delete"
        destructiveButtonTitle:@"Delete"
        otherButtonTitles:nil
      ];
#if TARGET_IPAD
      [sheet showInView:appDelegate.tuneViewController.view];
#else
      [sheet showInView:appDelegate.topViewController.view];
#endif
#if !TARGET_IPAD
    } else if ( row == kNewTune ) {
      Tune *tune = [[Tune alloc] init];
      tune.title = @"New Tune";
      tune.key = @"D";
      tune.dots = @"X: 1\nT: New Tune\nL: 1/8\nM:4/4\nK: D\n|";
      TuneViewController *controller = [[TuneViewController alloc] initWithTune:tune];
      AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
      [appDelegate.navigationController pushViewController:controller animated:YES];
      [controller setEditing:YES];
      [controller autorelease];
#endif
    }
  } else {
    if ( row == kShareTunes ) {
      AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
      NetworkStatus netStatus = [appDelegate.reachability currentReachabilityStatus];
      if ( netStatus == NotReachable ) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Couldn't start a Web server because you have no Internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
      } else {
        WebServerController *controller = [[WebServerController alloc] initWithNibName:@"WebServer" bundle:nil];
#if TARGET_IPAD
        controller.modalPresentationStyle = UIModalPresentationFormSheet;
        [appDelegate.tuneViewController dismissPopoverController];
        [appDelegate.tuneViewController presentModalViewController:controller animated:YES];
#else
        [navigationController pushViewController:controller animated:YES];
#endif
      }
    } else {
#if BLUETOOTH
      BluetoothController *controller = [[BluetoothController alloc] initWithNibName:@"Bluetooth" bundle:nil];
      [navigationController pushViewController:controller animated:YES];
#endif
    }
  }
}

#pragma mark UITableView data source methods

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#if BLUETOOTH
  return section == 0 ? kNumRows : (gameKitAvailable ? 2 : 1);
#else
  return section == 0 ? kNumRows : 1;
#endif
}

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
	return section == 0 ? @"Manage Tunes" : @"Sharing";
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:kCellIdentifier];
	if (cell == nil)
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:kCellIdentifier] autorelease];

  int row = indexPath.row;
  if ( indexPath.section == 0 ) {
    if ( row == kImportTunes )
#if TARGET_IPAD
      cell.text = @"Import Tunes from Web";
#else
      cell.text = @"Import Tunes";
#endif
    else if ( row == kDeleteTunes )
      cell.text = @"Delete All Tunes";
#if !TARGET_IPAD
    else if ( row == kNewTune )
      cell.text = @"Add New Tune";
#endif
  } else {
    if ( row == kShareTunes )
      cell.text = @"Start Web Server";
    else
      cell.text = @"Bluetooth";
  }
  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	return cell;
}

#pragma mark UIActionSheetDelegate methods

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  if ( buttonIndex == 0 ) {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate deleteAllTunes];
  }
}

@end
