//
//  CEAppDelegate.m
//  DemoCustomiOSApi
//
//  Created by CUSTOM S.p.A. on 19/07/13.
//  Copyright (c) 2013 CUSTOM S.p.A.. All rights reserved.
//
//************************************************************************************
//*                                                                                  *
//* This document contains programming examples.                                     *
//*                                                                                  *
//* CUSTOM S.p.A. grants you a nonexclusive copyright license to use                 *
//* all programming code examples from which you can generate similar                *
//* function tailored to your own specific needs.                                    *
//*                                                                                  *
//* All sample code is provided by CUSTOM S.p.A. for illustrative purposes           *
//* only. These examples have not been thoroughly tested under all conditions.       *
//* CUSTOM S.p.A., therefore, cannot guarantee or imply reliability,                 *
//* serviceability, or function of these programs.                                   *
//*                                                                                  *
//* In no event shall CUSTOM S.p.A. be liable for any direct, indirect,              *
//* incidental, special, exemplary, or consequential damages (including, but not     *
//* limited to, procurement of substitute goods or services; loss of use, data,      *
//* or profits; or business interruption) however caused and on any theory of        *
//* liability, whether in contract, strict liability, or tort (including negligence  *
//* or otherwise) arising in any way out of the use of this software, even if        *
//* advised of the possibility of such damage.                                       *
//*                                                                                  *
//* All programs contained herein are provided to you "as is" without any            *
//* warranties of any kind.                                                          *
//* The implied warranties of non-infringement, merchantability and fitness for a    *
//* particular purpose are expressly disclaimed.                                     *
//*                                                                                  *
//************************************************************************************


#import "CEAppDelegate.h"

#import "CEFirstViewController.h"
#import "CEDevicesViewController.h"
#import "CEPrintPictureViewController.h"
#import "CEPrintTextViewController.h"
#import "CEPrinterStatusViewController.h"

@implementation CEAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[CEFirstViewController alloc] initWithNibName:@"CEFirstViewController" bundle:nil];
    UIViewController *viewController2 = [[CEDevicesViewController alloc] initWithNibName:@"CEDevicesViewController" bundle:nil];
    UIViewController *viewController3 = [[CEPrintPictureViewController alloc] initWithNibName:@"CEPrintPictureViewController" bundle:nil];
    UIViewController *viewController4 = [[CEPrintTextViewController alloc] initWithNibName:@"CEPrintTextViewController" bundle:nil];
    UIViewController *viewController5 = [[CEPrinterStatusViewController alloc] initWithNibName:@"CEPrinterStatusViewController" bundle:nil];
    
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4, viewController5];
    self.tabBarController.delegate = self;
    self.tabBarController.moreNavigationController.delegate = self;
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
    CEFirstViewController* mainView = [CEFirstViewController getInstance];
    
    if (mainView.currentCustomPrn != nil)
    {
        [mainView.currentCustomPrn Close:nil];
        mainView.currentCustomPrn = nil;
    }

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    
    //Force to 1st viewController
    self.tabBarController.selectedIndex = 0;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    int index = self.tabBarController.selectedIndex;
    if (index == -1)
    {
        
    }
    else
    {
        //Check if is the Status
        if (index == 4)
        {
            [CEPrinterStatusViewController SetVisible:true];
        }
        else
        {
            [CEPrinterStatusViewController SetVisible:false];
        }
    }
}

- (void) navigationController:(UINavigationController*)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated
{
    int index = self.tabBarController.selectedIndex;
    if (index == -1)
    {
        
    }
    else
    {
        //Check if is the Status
        if (index == 4)
        {
            [CEPrinterStatusViewController SetVisible:true];
        }
        else
        {
            [CEPrinterStatusViewController SetVisible:false];
        }
    }
}



// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
}


@end
