//
//  AppDelegate.m
//  Gilliatte
//
//  Created by Lead on 11/08/16.
//  Copyright © 2016 Lead infosoft. All rights reserved.
//

#import "AppDelegate.h"
#import "SlideMenuController.h"
#import "SideMenuVC.h"
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
#import "EnterDailyReportVC.h"


@interface AppDelegate (){
    EnterDailyReportVC *EnterDailyReport;
}
@property (retain, nonatomic) UIViewController *mainViewControler;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self AccessPermission];
    
    NSString *Token = @"39bf333d934c975660d70acc2c18d982a6ed792b85832862d884d6837d5bc745";
    NSString *UUID = @"BC504150-ACA9-4974-BE79-B9FD099A0CD7";
    [[NSUserDefaults standardUserDefaults] setObject:UUID forKey:@"UUID"];  //For UUID
    [[NSUserDefaults standardUserDefaults]setObject:Token forKey:@"PUSH_TOKEN"];
    [[NSUserDefaults standardUserDefaults] setBool:0 forKey:@"OneTimeCurrentDay"];//For Set One Time Current Date
    
    //Set Nil To UserDefault
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"PROJECT_ID"];
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"JOB_ID"];
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"SUB_ID"];
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"EMPLOYEE_ID"];
    
 //   [self PushNotification];
    
    NSData *GetUserData;
    if ((GetUserData = [[NSUserDefaults standardUserDefaults] objectForKey:@"USER_PROFILE"])) {
        
        EnterDailyReportVC *loginController;
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            loginController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"EnterDailyReportVC"];
        }
        else{
            loginController = [[UIStoryboard storyboardWithName:@"MainStory_iPad" bundle:nil] instantiateViewControllerWithIdentifier:@"EnterDailyReportVC"];
        }
        
        UINavigationController *Navigation = [[UINavigationController alloc]initWithRootViewController:loginController];
        
        UIStoryboard *Storybord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        SideMenuVC *LeftMenu = (SideMenuVC *)[Storybord instantiateViewControllerWithIdentifier:@"SideMenuVC"];
        
        SlideMenuController *slideMenuController = [[SlideMenuController alloc] initWithMainViewController:Navigation leftMenuViewController:LeftMenu];
        
        slideMenuController.option.shadowOpacity = 0;
        slideMenuController.option.contentViewOpacity = 0.8f;
        slideMenuController.option.contentViewScale = 1.0f;
        slideMenuController.option.hideStatusBar = YES;
        //    slideMenuController.option.hideStatusBar = YES;
        slideMenuController.option.shadowRadius = 3;
        slideMenuController.SlideTapGestuer = @"Tap";
        self.window.rootViewController = slideMenuController;
        
    }
    else{
        
        UINavigationController *Navigation = (UINavigationController *)self.window.rootViewController;
        
        UIStoryboard *Storybord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        
        SideMenuVC *LeftMenu = (SideMenuVC *)[Storybord instantiateViewControllerWithIdentifier:@"SideMenuVC"];
        
        SlideMenuController *slideMenuController = [[SlideMenuController alloc] initWithMainViewController:Navigation leftMenuViewController:LeftMenu];
        
        slideMenuController.option.shadowOpacity = 0;
        slideMenuController.option.contentViewOpacity = 0.8f;
        slideMenuController.option.contentViewScale = 1.0f;
        slideMenuController.option.hideStatusBar = YES;
        //    slideMenuController.option.hideStatusBar = YES;
        slideMenuController.option.shadowRadius = 3;
        slideMenuController.SlideTapGestuer = @"Tap";
        self.window.rootViewController = slideMenuController;
    }
    
    [self.window makeKeyWindow];
    return YES;
}

-(void)AccessPermission{
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusNotDetermined) {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {
                NSLog(@"You Are Successfully Authorized.");
            }
            if (status == PHAuthorizationStatusNotDetermined) {
                NSLog(@"You Not Authorized.");
            }
            if (status == PHAuthorizationStatusDenied) {
                NSLog(@"You Denied.");
            }
            if (status == PHAuthorizationStatusRestricted) {
                NSLog(@"You Restricted.");
            }
        }];
    }
}

- (void)PushNotification{
    NSString *UUID = [[[UIDevice currentDevice]identifierForVendor]UUIDString];
    [[NSUserDefaults standardUserDefaults] setObject:UUID forKey:@"UUID"];
    //For UUID

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)];
    }
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    NSString *strTocken = [NSString stringWithFormat:@"Device Token=%@",deviceToken];
    strTocken=[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    strTocken = [strTocken stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"Token:%@", strTocken);
    
    [[NSUserDefaults standardUserDefaults]setObject:strTocken forKey:@"PUSH_TOKEN"];  //Push Token
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    id aps1 = [userInfo objectForKey:@"aps"];
    application.applicationIconBadgeNumber = (int)[aps1 valueForKey:@"badge"];
    if ( application.applicationState == UIApplicationStateActive )
    {
        NSLog(@"%@",userInfo);
    }
    else
    {
        
    }
    
}

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

@end
