//
//  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{
//    {
//        aps =     {
//            alert =         {
//                "action-loc-key" = "";
//                body = "\U60a8\U6709\U4e00\U6761\U65b0\U7684\U6d88\U606f";
//                "launch-image" = "";
//                "loc-args" = "<null>";
//                "loc-key" = "";
//                title = "\U6d88\U606f\U901a\U77e5";
//                "title-loc-args" = "<null>";
//                "title-loc-key" = "";
//            };
//        };
//        message = "{\"type\":1,\"body\":{\"session_id\":\"6e0f33004cb612a72341048b1eddbd3735e92dc4\",\"msg_id\":\"18fe84c6-25ea-41c7-9a70-1fe3e5c0b9ce\",\"version\":1,\"send_time\":\"2020-09-11 16:13:49\",\"msg_seq\":1599812029575,\"msg_from\":\"78f40cb49e0f54dd654de836c58f5202\",\"msg_to\":\"ce0819db8391f1e7258a71cc9e2c3235\",\"msg_scope\":1,\"msg_type\":1,\"msg\":{\"msg\":\"\U968f\U673a\U6570:9265\"}}}";
//    }
    
    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];
}
@end
