//
//  AppDelegate.m
//  Core SDK
//
//  Created by Dominik on 19/03/2015.
//  Copyright (c) 2015 worldnet. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [[WTPSTerminal singleton] initialize];
    [[WTPSTerminal singleton] registerCoreAPISettingsListener:self];
    [[WTPSTerminal singleton] registerCoreAPIReportingListener:self];
    [[WTPSTerminal singleton] registerLogListener:self];
    [[WTPSTerminal singleton] setMode:TEST];
    [[WTPSTerminal singleton] setLogLevel:LEVEL_FULL];

    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.
}

- (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.
}

- (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:.
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    NSLog(@"url: %@", [url scheme]);
    return YES;
}

- (id<CoreAPISettingsListener>) getCurrentViewController{
    UINavigationController* selectedNavigationController = (UINavigationController*)((UITabBarController*)self.window.rootViewController).selectedViewController;
    UIViewController *selectedViewController = [selectedNavigationController.viewControllers objectAtIndex:0];
    return (id<CoreAPISettingsListener>)(selectedViewController);
}

#pragma mark ------------- CoreAPIListener --------------


/*!
 * This method is triggered after error has occurred in the SDK.
 * @param error contains typedef enum of an error.
 */
-(void)onError:(CoreError) error withDescription:(NSString*) message{
    [[self getCurrentViewController] onError:error withDescription:message];
}
/*!
 * Sends back terminal settings object in response to init method.
 * @param settings holds the settings
 */
-(void)onSettingsRetrieved:(CoreSettings*)settings{
    UITabBarController* tabBarController = (UITabBarController*)self.window.rootViewController.navigationController;
    NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[tabBarController viewControllers]];
    if([settings.features objectForKey:@"payconexSdk"] != nil){
        if([tbViewControllers count] == 5){
            [tbViewControllers removeObjectAtIndex:4];
            [tbViewControllers removeObjectAtIndex:3];
            [tbViewControllers removeObjectAtIndex:1];
            [tabBarController setViewControllers:tbViewControllers];
        }
        [[WTPSTerminal singleton] setTerminalId:@"220614969941"];
        [[WTPSTerminal singleton] setSecret:@"0e8d38a08f2d4919a5a27ec83a6e8d76"];
    }
    if ([[self getCurrentViewController] respondsToSelector:@selector(onSettingsRetrieved:)]){
        [[self getCurrentViewController] onSettingsRetrieved:settings];
    }
}

-(void)onLogMessage:(NSString*) message{
    
}

-(void)onLoginUrlRetrieved:(NSString *)url{

}

-(void)onTransactionListResponse:(CoreTransactions*)transaction{
    
}

- (void) onRequestCloseBatchResponse:(CoreCloseBatchResponse*) response{
    [[UiHelper singleton] showSuccess:@"New Transaction Batch Time" withMessage:[response batchTime]];
}

-(void) onTransactionReportRetrieved:(CoreTransactionReports*)respose{
    NSLog(@"onTransactionReportRetrieved");
}

@end
