//
//  AppDelegate.m
//  XYSW
//
//  Created by Jim on 2021/9/7.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import <MOBFoundation/MobSDK.h>
#import <MOBFoundation/MobSDK+Privacy.h>
#import <MobPush/MobPush.h>
#import "MonitorNetworkStatus.h"
#import "YHBaseNavigationController.h"
#import "LoginViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

+ (AppDelegate *)shareAppDelegate{
    return (AppDelegate *)[[UIApplication sharedApplication]delegate];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window setRootViewController:self.navigationCtrl];
    [self.window makeKeyAndVisible];
   
   //监听网络状态
   [[MonitorNetworkStatus sharedManager] YHProject_startNotifyWithDelegate:self];
   
   [self regiesterMoBPush:launchOptions];
   
   // 判断进主页还是进登录
   if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"login"] intValue] == 1) {
       [AppDelegate  startMainViewController];
   }else{
       [AppDelegate  startLoginViewController];
   }
    
    return YES;
}

+(void)startLoginViewController{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [[AppDelegate shareAppDelegate].navigationCtrl pushViewController:loginVC animated:NO];
    [AppDelegate shareAppDelegate].navigationCtrl.viewControllers = @[loginVC];
    
}

+(void)startMainViewController{
    ViewController *mainVC = [[ViewController alloc] init];
    [[AppDelegate shareAppDelegate].navigationCtrl pushViewController:mainVC animated:NO];
    [AppDelegate shareAppDelegate].navigationCtrl.viewControllers = @[mainVC];
}

#pragma mark --- 注册推送
-(void)regiesterMoBPush:(NSDictionary *)dic{
    // 注意：上传隐私协议接口，具体查看官方文档(http://www.mob.com/wiki/detailed?wiki=MobTechprivacypushios&id=136)
    [MobSDK uploadPrivacyPermissionStatus:YES onResult:^(BOOL success) {
        NSLog(@"-------------->上传结果：%d",success);
    }];
    
    // 设置推送环境
#ifdef DEBUG
    [MobPush setAPNsForProduction:NO];
#else
    [MobPush setAPNsForProduction:YES];
#endif
    
    [MobSDK registerAppKey:KMBPushAppKey appSecret:KMBPushAppSecret];
    
    //MobPush推送设置（获得角标、声音、弹框提醒权限）
    MPushNotificationConfiguration *configuration = [[MPushNotificationConfiguration alloc] init];
    configuration.types = MPushAuthorizationOptionsBadge | MPushAuthorizationOptionsSound | MPushAuthorizationOptionsAlert;
    [MobPush setupNotification:configuration];
    
    [MobPush getRegistrationID:^(NSString *registrationID, NSError *error) {
        NSLog(@"registrationID = %@--error = %@", registrationID, error);
    }];
    
    // 监听推送通知 MobPush整合了系统iOS10以上及以下不同方式获取推送统一通过此监听获取，开发者通过原始方式亦不影响。
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessage:) name:MobPushDidReceiveMessageNotification object:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    //程序进入前台时,清除角标，但不清空通知栏消息(开发者根据业务需求，自行调用)
    //注意：不建议在进入后台通知(applicationDidEnterBackground:)中调用此方法，原因进入后台将角标清空结果无法通过网络同步到服务器
    [MobPush clearBadge];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    
}

// 收到推送通知回调
- (void)didReceiveMessage:(NSNotification *)notification
{
    MPushMessage *message = notification.object;
    
    NSLog(@"message:%@", message.convertDictionary);
    
    switch (message.messageType)
    {
        case MPushMessageTypeCustom:
        {// 自定义消息回调
           
        }
            break;
        case MPushMessageTypeAPNs:
        {// APNs回调
            
        }
            break;
        case MPushMessageTypeLocal:
        {// 本地通知回调
            NSString *body = message.notification.body;
            NSString *title = message.notification.title;
            NSString *subtitle = message.notification.subTitle;
            NSInteger badge = message.notification.badge;
            NSString *sound = message.notification.sound;
            NSLog(@"收到本地通知:{\nbody:%@，\ntitle:%@,\nsubtitle:%@,\nbadge:%ld,\nsound:%@,\n}",body, title, subtitle, (long)badge, sound);
        }
            break;
        case MPushMessageTypeClicked:
        {// 点击通知回调
            
            // 测试
            if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
            { // 前台
              
            }
            else
            { // 后台
               
            }
        }
        default:
            break;
    }
}


#pragma mark - NetworkStatusNotifyManagerDelegate
- (void)networkStatusNotifyManagerStatusChanged:(BOOL)reachability {
    
    _networkStatus = reachability ? NetWorkStatusReachability : NetWorkStatusNotReachability;
    NSLog(@"网络状态===%@",_networkStatus? @"有网络" : @"无网络");
    
}

#pragma mark --- GET方法
- (UINavigationController *)navigationCtrl{
    
    if (!_navigationCtrl) {
        
        _navigationCtrl = [[YHBaseNavigationController alloc]init];
        [_navigationCtrl setNavigationBarHidden:YES];
        
        DLog(@"%@",_navigationCtrl);
    }
    return _navigationCtrl;
    
}


@end
