//
//  AppDelegate.m
//  superCleaner
//
//  Created by Mazy on 2020/7/2.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "AppDelegate.h"
#import "ICHomeViewController.h"
#import "ICNavigationController.h"
#import "ICGuideViewController.h"
#import "superCleaner-Swift.h"
#import "Data.h"
#import "KeychainItemManager.h"

#import <mach/mach.h>
#import <sys/mount.h>

#define kAPP_Version @"app_version"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    Data * data = [[Data alloc] init];
    [data InitPlist];
    [KeychainItemManager ReadUUIDAndNickName];
    
    [ICPurchaseManager launchWithSwiftyStoreKitWithSuccess:^{
    }];
    
    [self observationNetwork];
    
    [UMConfigure initWithAppkey:@"5f2a6bd6d3093221547530b2" channel:@"App Store"];
    [UMConfigure setLogEnabled:true];
    
    [MobClick event:@"app_start"];
    
    self.window = [[UIWindow alloc] initWithFrame: UIScreen.mainScreen.bounds];
    self.window.backgroundColor = [UIColor whiteColor]; // 窗口背景色
    ICHomeViewController *homeVC = [[UIStoryboard storyboardWithName:@"Main" bundle: nil] instantiateViewControllerWithIdentifier:@"ICHomeViewController"];
    //ICGuideViewController
    ICGuideViewController *guideVC = [[UIStoryboard storyboardWithName:@"Main" bundle: nil] instantiateViewControllerWithIdentifier:@"ICGuideViewController"];
    ICNavigationController *navc = [[ICNavigationController alloc] initWithRootViewController:homeVC];
    
    //当前运行的版本
    NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
    //本地保存的版本号
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *localVersion = [userDefault objectForKey:kAPP_Version];
    
    [[ICUserManager shared] getServiceInfo];
    
    if ([currentVersion isEqualToString:localVersion]) {
        //已经运行过该版本
        self.window.rootViewController = navc;
    } else {
        //同意 更新储存版本 切换rootVC
        [userDefault setObject:currentVersion forKey:kAPP_Version];
        self.window.rootViewController = guideVC;
    }
    
    [self.window makeKeyAndVisible]; // 让当前窗口成为主窗口
    
    NSMutableDictionary *dict = @{@"event": @"guazinovel", @"action": @"app_start", @"value": @""}.mutableCopy;
    [[ZTNetworkTools shared] postWithLogCollection:logCollection parameters:dict success:^(id  _Nullable __autoreleasing * _Nullable response) {
    } failure:^(NSError * _Nonnull error) {
    }];
    
    return YES;
}

/// 监听网络状态
- (void)observationNetwork {
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"%s:%d %s",__FILE__,__LINE__,"为知网络");
                ICUserManager.shared.network = @"";
                break;
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"%s:%d %s",__FILE__,__LINE__,"无网络");
                ICUserManager.shared.network = @"";
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"%s:%d %s",__FILE__,__LINE__,"WiFi网络");
                ICUserManager.shared.network = @"WiFi";
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"%s:%d %s",__FILE__,__LINE__,"移动网络");
                ICUserManager.shared.network = @"WWAN";
                break;
            default:
                break;
        }
    }];
    [manager startMonitoring];
}

@end
