// MobileTerminalAppDelegate.m
// MobileTerminal

#import "MobileTerminalAppDelegate.h"
#import "MobileTerminalViewController.h"

#import "Preferences/Settings.h"
#import "Preferences/MenuSettings.h"

// Automatically generated by build rules
#import "svnversion.h"


@implementation MobileTerminalAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize terminalViewController;
@synthesize preferencesViewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
  Settings* settings = [Settings sharedInstance];
  settings.svnVersion = SVN_VERSION;
  
  [[UIApplication sharedApplication] setStatusBarHidden:YES];
  
  NSMutableArray* viewControllers = [[NSMutableArray alloc] init];
  [viewControllers addObject:terminalViewController];  
  [navigationController setViewControllers:viewControllers animated:NO];
  [viewControllers release];
    
  [window setRootViewController:navigationController];
  [window makeKeyAndVisible];
  inPreferences = FALSE;
}

static const NSTimeInterval kAnimationDuration = 1.00f;

- (void)preferencesButtonPressed:(UIView *)sender
{
  inPreferences = TRUE;
  if (isiPad){
    UINavigationController *preferencesNavigationController = [[UINavigationController alloc] initWithRootViewController:preferencesViewController];
    UIPopoverController *preferencesPopover = [[UIPopoverController alloc] initWithContentViewController:preferencesNavigationController];
    [preferencesPopover presentPopoverFromRect:sender.frame inView:sender.superview permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
      preferencesPopover.delegate = self;
      [preferencesNavigationController release];
  } else {
    [navigationController setNavigationBarHidden:NO];
    [navigationController pushViewController:preferencesViewController animated:YES];
  }
}

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    [popoverController release];
}

- (void)rootViewDidAppear
{
  if (inPreferences) {
    [[Settings sharedInstance] persist];
  }
  inPreferences = TRUE;

  // This must be invoked after the animation to show the root view completes
  [navigationController setNavigationBarHidden:YES];  
}

@end
