//
//  AliPushManager.m
//  RNYikeLibrary
//
//  Created by winfredxu on 2018/12/15.
//  Copyright © 2018 Facebook. All rights reserved.
//

#import "AliPushManager.h"
#import <CloudPushSDK/CloudPushSDK.h>
#import <UserNotifications/UserNotifications.h>

@interface AliPushManager () <UNUserNotificationCenterDelegate>
@end

@implementation AliPushManager

RCT_EXPORT_MODULE()

static AliPushManager * sharedInstance = nil;

+ (BOOL)requiresMainQueueSetup {
    return YES;
}

+ (id)allocWithZone:(NSZone *)zone {
    static AliPushManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [super allocWithZone:zone];
    });
    return sharedInstance;
}

//copy返回单例本身
- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (instancetype)init
{
    
    if (!(self = [super init])) {
        NSLog(@"init AliyunPushManager error");
    }
    sharedInstance = self;
    return self;
    
}

+ (AliPushManager *)sharedInstance
{
    @synchronized(self) {
        if (sharedInstance == nil){
            sharedInstance = [[self alloc] init];
        }
    }
    return sharedInstance;
}

RCT_EXPORT_METHOD(bindAccount:(NSString *)account
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    [CloudPushSDK bindAccount:account withCallback:^(CloudPushCallbackResult *res) {
        if (res.success) {
            resolve(@"");
        } else {
            reject([NSString stringWithFormat:@"%ld",(long)res.error.code], res.error.localizedDescription,res.error);
        }
    }];
}

- (NSArray<NSString *> *)supportedEvents
{
    return @[@"aliyunPushReceived"];
}

@end
