//
//  AppDelegate.m
//  CYSdkDemo
//
//  Created by ls on 2019/7/30.
//  Copyright © 2019 ls. All rights reserved.
//

#import "AppDelegate.h"
#import "HomeViewController.h"
#import "EcgViewController.h"
#import "UserArchive.h"
#import "MagicalRecord.h"
#import "UserModel+CoreDataProperties.h"
#import "UserModel+CoreDataClass.h"
//#import <IQKeyboardManager/IQKeyboardManager.h>
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //注册键盘事件
//    [IQKeyboardManager sharedManager].enable = YES;
//    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
//    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    
#ifdef ECGSDK
    [MagicalRecord setupCoreDataStackWithStoreNamed:@"creYond"];
    UserArchive* autoUser = [UserArchive aDecoderUser];
    BOOL isAuto = autoUser.isAutoLogin;
    
    if(isAuto)
    {
        //查找所有age属性为3的Person记录。
        NSArray *personsAgeEuqals3 = [UserModel MR_findByAttribute:@"userName" withValue:autoUser.username];
        if(personsAgeEuqals3.count == 0)
        {
            [XDTopHud showWarning:@"输入的帐号不存在"];
            HomeViewController* controller = [[HomeViewController alloc] init];
            UINavigationController* Nav = [[UINavigationController alloc] initWithRootViewController:controller];
            self.window.rootViewController = Nav;
        }
        else
        {
            //配置患者信息
            UserModel* user = personsAgeEuqals3.firstObject;
            NSString* appId = CYAppid;
            CYUserConfig* config = [[CYUserConfig alloc] init];
            config.height = user.userHeight.intValue;
            config.weight = user.userWeight.floatValue;
            config.phone = user.userPhone;
            config.age = user.userAge.intValue;
            if([user.userSex isEqualToString:@"男"])
            {
                config.sex = MAN;
            }
            else
            {
                config.sex = WOMAN;
            }
            //初始化SDK
            [CYEcgKit initWithAppID:appId userName:user.userName userConfig:config];
            EcgViewController* controller = [[EcgViewController alloc] init];
            UINavigationController* Nav = [[UINavigationController alloc] initWithRootViewController:controller];
            self.window.rootViewController = Nav;
        }
    }
    else
    {
        HomeViewController* controller = [[HomeViewController alloc] init];
        UINavigationController* Nav = [[UINavigationController alloc] initWithRootViewController:controller];
        self.window.rootViewController = Nav;
    }
    
    
#else
    EcgViewController* controller = [[EcgViewController alloc] init];
    UINavigationController* Nav = [[UINavigationController alloc] initWithRootViewController:controller];
    self.window.rootViewController = Nav;
#endif
    
    //屏幕常亮
    if(![[[NSUserDefaults standardUserDefaults] objectForKey:@"IDLETIMER"] isEqualToString:@"off"])
    {
        [UIApplication sharedApplication].idleTimerDisabled = YES;
        [[NSUserDefaults standardUserDefaults] setObject:@"on" forKey:@"IDLETIMER"];
    }
    else
    {
        [UIApplication sharedApplication].idleTimerDisabled = NO;
    }
    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 invalidate graphics rendering callbacks. 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 active 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 {
#ifdef THREESDK
    [[CYEcgKit sharedInstance] endRedcord];

#else
    [[CYBaseKit sharedInstance] endRedcord];

#endif
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
