//
//  AppDelegate.m
//  Temperature
//
//  Created by RD on 2022/10/21.
//

#import "AppDelegate.h"
#import "RDSHomeVC.h"
#import "RDSRootControl.h"
#import <MMKV/MMKV.h>
#import <WebKit/WebKit.h>

@interface AppDelegate ()
@property (nonatomic, strong) WKWebView *webView;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [RDSRootControl shareControl].rootViewController;
    [self.window makeKeyAndVisible];
    self.window.backgroundColor = RDSViewBgColor;
    
    // 初始化MMKV
//    [MMKV initializeMMKV:nil];
    
    // 设置全局userAgent
    self.webView = [[WKWebView alloc] init];
    [_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        NSString *newUserAgent = [result stringByAppendingFormat:@"/%@", @"yongxulvjian"];
        [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":newUserAgent}];
    }];
    
    [RDSDemoApiHelper rds_appInitSuccess:^(id responseObject) {
        
    } failure:^(NSError *error) {
        
    }];
    
    // 自动登录
    if(!kNULLString(TheDataManager.token)){
        [RDSDemoApiHelper rds_refreshTokenSuccess:^(id responseObject) {
            if ([responseObject[@"code"] intValue] == 0) {
                [RDSRootControl shareControl].isLoginSuccess = YES;
                NSDictionary *data = responseObject[@"data"];
                TheDataManager.token = [NSString stringWithFormat:@"%@ %@",data[@"token_type"], data[@"access_token"]];
            } else{
            }
        } failure:^(NSError *error) {
        }];
    }

    
    [RDSHudShower setupHUD];
    // 启动图片延时1秒
    [NSThread sleepForTimeInterval:1];
    
    
//    NSString *language = [[NSUserDefaults standardUserDefaults] valueForKey:@"AppleLanguages"];
//    DDLog(@"系统语言:%@",language);
//    [[NSUserDefaults standardUserDefaults] setObject:@[@"zh-Hans"] forKey:@"AppleLanguages"];
//    [[NSUserDefaults standardUserDefaults] synchronize];
    
    return YES;
}




@end
