//
//  AppDelegate.m
//  唔即云相册
//
//  Created by 余衡武 on 2021/12/8.
//

#import "AppDelegate.h"
#import <CoreMotion/CoreMotion.h>
#import "ShearDeviceUDPManager.h"

@interface AppDelegate (){
    CMMotionManager *cmManager;
}

@end

@implementation AppDelegate

+ (AppDelegate*)sharedAppDelegate
{
    static AppDelegate *appDelegate = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (appDelegate == nil) {
            appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        }
    });
    
    return appDelegate;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //设置默认值
    self.couldPhone_W_PHONE = 720.0;
    self.couldPhone_H_PHONE = 1280.0;
    
    [[UIButton appearance] setExclusiveTouch:YES];
    
    cmManager = [[CMMotionManager alloc] init];
    if (cmManager.isAccelerometerAvailable){
        HLog(@"\n-------摇一摇可用-----");
    }else{
        HLog(@"\n-------摇一摇不可用-----");
    }
    cmManager.accelerometerUpdateInterval = 0.1;
    [cmManager startAccelerometerUpdates];
    [cmManager startDeviceMotionUpdatesToQueue:[NSOperationQueue new] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
//        CMRotationRate rotationRate = motion.rotationRate;
//        CGFloat rotationRatex = rotationRate.x;
//        CGFloat rotationRatey = rotationRate.y;
//        CGFloat rotationRatez = rotationRate.z;
        
        CMAcceleration accrleration = motion.gravity;
        CGFloat rotationGravityz = accrleration.z;
        
        //HLog(@"rotationRatey: %f",rotationRatey)
        //HLog(@"rotationGravityz: %f",rotationGravityz)
        
        //if (rotationRatey > 7){
        if (rotationGravityz > 0.85){
            
            BOOL haveOpenMask = [HWDataManager getBoolWithKey:Consn_Fanzhuan_Exit_app_Open];
            if (haveOpenMask){
                exit(0);/*强制退出app*/
            }
        }
    }];
    
//    [[ShearDeviceUDPManager shareInstance] startShearchDevice];
//    [[ShearDeviceUDPManager shareInstance] shearchDeviceLoop];
    
    return YES;
}

/// 在这里写支持的旋转方向，为了防止横屏方向，应用启动时候界面变为横屏模式
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskPortrait;
}


#pragma mark - UISceneSession lifecycle


- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}


- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

- (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.
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        NSString *orderNumber = [HWDataManager getStringWithKey:Const_AirpayOrWXorderNum];
        if (orderNumber && ![orderNumber isEqualToString:@""])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:NotNameAirpayOrWXorderNum object:orderNumber];
        }
    });
}



@end
