//
//  ApexMobileAppDelegate.m
//  Apex Mobile
//
//  Created by Ray on 14-2-24.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//


#import "ApexMobileAppDelegate.h"
#import "ApexMobileDB.h"
#import "ApexMobileNetwork.h"
#import "AESCrypt.h"
@implementation ApexMobileAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [GMSServices provideAPIKey:@"AIzaSyBqS2pF7m1DlR5zwhAX_rBSD_9bTnpjYbQ"];
    if (! [ApexMobileDB initializeDb])
        // TODO: alert the user!
        DebugLog(@"couldn't init db");
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
                UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
                [[UIApplication sharedApplication]  registerUserNotificationSettings:settings];
            [[UIApplication sharedApplication] registerForRemoteNotifications ];
    }
    else
    {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIUserNotificationTypeBadge |UIUserNotificationTypeSound | UIUserNotificationTypeAlert];
    }


    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    self.user = [AESCrypt decrypt:[defaults stringForKey:@"user"] password:@"usai"];
    
  
    self.password= [AESCrypt decrypt:[defaults stringForKey:@"password"] password:@"usai"];

    
    return YES;
}
-(void)Logout
{
    self.user = nil;
    self.password=nil;
    self.sessionid=nil;
    [ApexMobileNetwork Logout];
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    [defaults setBool:false forKey:@"autologin"];
    
    
    [defaults synchronize];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] cancelAllLocalNotifications ];
    //self.duid = nil;
}
- (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
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications ];
    
    // 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:.
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken {
    NSString * sss=[NSString stringWithFormat:@"%@",pToken];
    self.duid = [[[[NSString stringWithFormat:@"%@",pToken] substringWithRange:NSMakeRange(0, 72)] substringWithRange:NSMakeRange(1, 71)] stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"regisger success:%@",pToken);
    
    //注册成功，将deviceToken保存到应用服务器数据库中
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    // 处理推送消息
    NSLog(@"userinfo:%@",userInfo);
    
    NSLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
  
    NSDictionary* objaps = [userInfo objectForKey:@"aps"];
    [UIApplication sharedApplication].applicationIconBadgeNumber = [[objaps valueForKey:@"badge"] integerValue];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"new notification" object:nil];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Registfail%@",error);
}
@end
