//
//  AppDelegate.m
//  SLAiELTS
//
//  Created by Gusont on 2023/2/27.
//

#import "AppDelegate.h"
#import "IQKeyboardManager.h"
#import "SLLoginVCViewController.h"
#import "SLBaseTabBarController.h"
#import "SLRoleLabelViewController.h"

@interface AppDelegate ()

@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.backgroundColor = [UIColor whiteColor];
    UIViewController *launchScreenVc = [[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil] instantiateInitialViewController];
    self.window.rootViewController = launchScreenVc;
    [SLHttpCenter SharedInstance].serverUrl = @"http://dlchat.zheke.com";
//    [SLHttpCenter SharedInstance].serverUrl = @"http://10.0.0.14:8088";
    NSString *userPhone = [[NSUserDefaults standardUserDefaults] objectForKey:@"SLUserPhone"];
    NSString *userPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"SLUserPassword"];
    if (userPassword.length) {
        NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"phone" : userPhone, @"password" : userPassword, @"code" : @"0", @"deviceBrand" : @"iPhone", @"deviceModel" : [NSString deviceModel]}];
        [self cacheLoginWhit:parameter];
    }else {
        [self setLoginVcWithRootWindow];
    }
    [self buildIQkeyBoard];
    return YES;
}

- (void)cacheLoginWhit:(NSDictionary *)parameter {
    [[SLHttpCenter SharedInstance] postWithUrl:@"/api/Token/LoginApp" parameter:parameter success:^(id responseObject) {
        NSDictionary *dataDict = [responseObject objectForKey:@"data"];
        SLLoginInfo *loginModel = [SLLoginInfo mj_objectWithKeyValues:dataDict];
        [SLGlobalInfo SharedInstance].loginInfo = loginModel;
        [SLUserModel requestUserRoleModel];
        if (!loginModel.isAiData) {
            [self setLoginVcWithRootWindow];
        }else {
            [self setTabWithRootWindow];
        }
    } failure:^(SPRequestError *error) {
        [self setLoginVcWithRootWindow];
    }];
}

- (void)setTabWithRootWindow {
    SLBaseTabBarController *tabBarContr = [[SLBaseTabBarController alloc] init];
    self.window.rootViewController = [[SLBaseNavigationController alloc] initWithRootViewController:tabBarContr];;
    [self.window makeKeyAndVisible];
}

- (void)setLoginVcWithRootWindow {
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"SLUserPassword"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    UIViewController *vc = [SLLoginVCViewController loadViewControllewWithNib];
    self.window.rootViewController = [[SLBaseNavigationController alloc] initWithRootViewController:vc];;
    [self.window makeKeyAndVisible];
}

#pragma MARK- IQ键盘初始化
-(void)buildIQkeyBoard
{
    IQKeyboardManager *manager =  [IQKeyboardManager sharedManager];
    manager.enable = YES;//是否启用键盘管理
    manager.shouldResignOnTouchOutside = YES;//点击可编辑之外，是否自动隐藏键盘
    manager.enableAutoToolbar = NO;//控制是否显示键盘上的工具条
    manager.keyboardDistanceFromTextField = 20.f;
    manager.layoutIfNeededOnUpdate = YES;
}

@end
