//
//  AppDelegate+OffcnIMPush.m
//  OffcnIMSDKiOS
//
//  Created by vockey on 2020/9/1.
//  Copyright © 2020 offcn. All rights reserved.
//

#import "AppDelegate+OffcnIMPush.h"
#import "OffcnIMModel.h"
#import "ZYJMJExtension.h"
#import "DataSafe.h"
#import "OffcnIMZYJFMDBHandler.h"
#import "OffcnIMSocketManager.h"
#import <UIKit/UIKit.h>

@implementation AppDelegate (OffcnIMPush)

+(void)registerOffcnUserNotificationSettingsWithApplication:(UIApplication *)application options:(NSDictionary *)launchOptions{
    //    application.applicationIconBadgeNumber = 0;
    
    if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0){
        // 1.请求授权可以给用户发送通知
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert categories:nil];
        
        [application registerUserNotificationSettings:settings];
    }else{
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    }
    
    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]){
        NSDictionary *remoteUserInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
        if(remoteUserInfo) {

            NSLog(@"remoteUserInfo:%@",remoteUserInfo);
            //APP未启动，点击推送消息，iOS10下还是跟以前一样在此获取
        }
    }
}

+(NSString *)hexStringForData:(NSData *)data{

    if (data == nil) {
        return nil;
    }

    NSMutableString *hexString = [NSMutableString string];
    const unsigned char *p = [data bytes];
    for (int i=0; i < [data length]; i++) {
        [hexString appendFormat:@"%02x", *p++];
    }
    return hexString;
}

+(void)didReceiveRemoteNotification:(NSDictionary *)userInfo{
    
    OffcnIMPushModel *IMModel = [OffcnIMPushModel mj_ZYJobjectWithKeyValues:userInfo];
    
    //存数据库
    OffcnIMBodyModel *body = IMModel.message.body;
    body.isSentSuccess = 1;
    if ([body.msg_from isEqualToString:[OffcnIMSocketManager defaultService].msg_fromId]) {
        body.bymyself = 1;
    }else{
        body.bymyself = 0;
    }
    body.isRead = 0;
    [[OffcnIMZYJFMDBHandler sharedInstance] insertChatInfoToFMDBWithModel:body];
    [[OffcnIMZYJFMDBHandler sharedInstance] chatlistCacheInfo:body callback:nil];
}

+(void)manualSendLocalNotificationWithModel:(OffcnIMModel *)model{
    dispatch_sync(dispatch_get_main_queue(), ^{
        UILocalNotification *backgroudMsg = [[UILocalNotification alloc] init];
        if (backgroudMsg) {
            backgroudMsg.timeZone = [NSTimeZone defaultTimeZone];
            backgroudMsg.alertBody = @"您收到一条新消息";
            backgroudMsg.applicationIconBadgeNumber = 1;
            backgroudMsg.alertAction = @"查看";
            NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
            backgroudMsg.userInfo = infoDic;
            [[UIApplication sharedApplication] presentLocalNotificationNow:backgroudMsg];
        }
    });
}
@end
