//
//  ExtSdkPushManagerWrapper.m
//  im_flutter_sdk
//
//  Created by 东海 on 2020/5/7.
//

#import "ExtSdkPushManagerWrapper.h"
#import "ExtSdkMethodTypeObjc.h"
#import "ExtSdkToJson.h"

@implementation ExtSdkPushManagerWrapper

+ (nonnull instancetype)getInstance {
    static ExtSdkPushManagerWrapper *instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
      instance = [[ExtSdkPushManagerWrapper alloc] init];
    });
    return instance;
}

- (void)initSDK {
}

- (void)getImPushConfig:(NSDictionary *)param
         withMethodType:(NSString *)aChannelName
                 result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    AgoraChatPushOptions *options = AgoraChatClient.sharedClient.pushManager.pushOptions;
    [weakSelf onResult:result
        withMethodType:aChannelName
             withError:nil
            withParams:[options toJsonObject]];
}

- (void)getImPushConfigFromServer:(NSDictionary *)param
                   withMethodType:(NSString *)aChannelName
                           result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    [AgoraChatClient.sharedClient.pushManager
        getPushNotificationOptionsFromServerWithCompletion:^(
            AgoraChatPushOptions *aOptions, AgoraChatError *aError) {
          [weakSelf onResult:result
              withMethodType:aChannelName
                   withError:aError
                  withParams:[aOptions toJsonObject]];
        }];
}

- (void)updatePushNickname:(NSDictionary *)param
            withMethodType:(NSString *)aChannelName
                    result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    NSString *nickname = param[@"nickname"];
    [AgoraChatClient.sharedClient.pushManager
        updatePushDisplayName:nickname
                   completion:^(NSString *_Nonnull aDisplayName,
                                AgoraChatError *_Nonnull aError) {
                     [weakSelf onResult:result
                         withMethodType:aChannelName
                              withError:aError
                             withParams:aDisplayName];
                   }];
}

- (void)updateImPushStyle:(NSDictionary *)param
           withMethodType:(NSString *)aChannelName
                   result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;

    AgoraChatPushDisplayStyle pushStyle = [param[@"pushStyle"] intValue];

    dispatch_async(
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
          AgoraChatError *aError = [AgoraChatClient.sharedClient.pushManager
              updatePushDisplayStyle:pushStyle];
          [weakSelf onResult:result
              withMethodType:aChannelName
                   withError:aError
                  withParams:@(!aError)];
        });
}

- (void)updateGroupPushService:(NSDictionary *)param
                withMethodType:(NSString *)aChannelName
                        result:(nonnull id<ExtSdkCallbackObjc>)result {
    //    __weak typeof(self) weakSelf = self;
    //    NSString *groupId = param[@"group_id"];
    //    bool enablePush = [param[@"enablePush"] boolValue];

    //    [AgoraChatClient.sharedClient.pushManager
    //        updatePushServiceForGroups:@[ groupId ]
    //                       disablePush:!enablePush
    //                        completion:^(AgoraChatError *_Nonnull aError) {
    //                          AgoraChatGroup *aGroup = [AgoraChatGroup groupWithId:groupId];
    //                          [weakSelf onResult:result
    //                              withMethodType:aChannelName
    //                                   withError:aError
    //                                  withParams:[aGroup toJsonObject]];
    //                        }];
}

- (void)bindDeviceToken:(NSDictionary *)param
         withMethodType:(NSString *)aChannelName
                 result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    NSString *deviceToken = param[@"token"];
    dispatch_async(
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
          AgoraChatError *error = [AgoraChatClient.sharedClient bindDeviceToken:deviceToken];
          [weakSelf onResult:result
              withMethodType:aChannelName
                   withError:error
                  withParams:nil];
        });
}

- (void)enablePush:(NSDictionary *)param
    withMethodType:(NSString *)aChannelName
            result:(nonnull id<ExtSdkCallbackObjc>)result {
    //    AgoraChatError *error = [AgoraChatClient.sharedClient.pushManager
    //    enableOfflinePush]; [self onResult:result
    //        withMethodType:aChannelName
    //             withError:error
    //            withParams:nil];
}

- (void)disablePush:(NSDictionary *)param
     withMethodType:(NSString *)aChannelName
             result:(nonnull id<ExtSdkCallbackObjc>)result {
    //    int startTime = [param[@"start"] intValue];
    //    int endTime = [param[@"end"] intValue];
    //    AgoraChatError *error =
    //        [AgoraChatClient.sharedClient.pushManager
    //        disableOfflinePushStart:startTime
    //                                                               end:endTime];
    //    [self onResult:result
    //        withMethodType:aChannelName
    //             withError:error
    //            withParams:nil];
}

- (void)getNoPushGroups:(NSDictionary *)param
         withMethodType:(NSString *)aChannelName
                 result:(nonnull id<ExtSdkCallbackObjc>)result {
    //    NSArray<NSString *> *groups =
    //        [AgoraChatClient.sharedClient.pushManager noPushGroups];
    //    [self onResult:result
    //        withMethodType:aChannelName
    //             withError:nil
    //            withParams:groups];
}

- (void)updateUserPushService:(NSDictionary *)param
               withMethodType:(NSString *)aChannelName
                       result:(nonnull id<ExtSdkCallbackObjc>)result {
    //    __weak typeof(self) weakSelf = self;
    //    NSArray *userIds = param[@"user_ids"];
    //    bool noPush = [param[@"noPush"] boolValue];
    //    [AgoraChatClient.sharedClient.pushManager
    //        updatePushServiceForUsers:userIds
    //                      disablePush:noPush
    //                       completion:^(AgoraChatError *_Nonnull aError) {
    //                         [weakSelf onResult:result
    //                             withMethodType:aChannelName
    //                                  withError:aError
    //                                 withParams:nil];
    //                       }];
}

- (void)getNoPushUsers:(NSDictionary *)param
        withMethodType:(NSString *)aChannelName
                result:(nonnull id<ExtSdkCallbackObjc>)result {
    //    NSArray<NSString *> *userIds =
    //        [AgoraChatClient.sharedClient.pushManager noPushUIds];
    //    [self onResult:result
    //        withMethodType:aChannelName
    //             withError:nil
    //            withParams:userIds];
}

- (void)reportPushAction:(NSDictionary *)param
          withMethodType:(NSString *)aChannelName
                  result:(nonnull id<ExtSdkCallbackObjc>)result {
    // TODO:
}

- (void)setConversationSilentMode:(NSDictionary *)param
                   withMethodType:(NSString *)aChannelName
                           result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    NSString *conversaionId = param[@"conversationId"];
    AgoraChatConversationType type =
        [AgoraChatConversation typeFromInt:[param[@"conversationType"] intValue]];
    AgoraChatSilentModeParam *silmentParam =
        [AgoraChatSilentModeParam fromJsonObject:param[@"param"]];
    [AgoraChatClient.sharedClient.pushManager
        setSilentModeForConversation:conversaionId
                    conversationType:type
                              params:silmentParam
                          completion:^(AgoraChatSilentModeResult *_Nullable aResult,
                                       AgoraChatError *_Nullable aError) {
                            [weakSelf onResult:result
                                withMethodType:aChannelName
                                     withError:aError
                                    withParams:nil];
                          }];
}

- (void)removeConversationSilentMode:(NSDictionary *)param
                      withMethodType:(NSString *)aChannelName
                              result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    NSString *conversaionId = param[@"conversationId"];
    AgoraChatConversationType type =
        [AgoraChatConversation typeFromInt:[param[@"conversationType"] intValue]];
    [AgoraChatClient.sharedClient.pushManager
        clearRemindTypeForConversation:conversaionId
                      conversationType:type
                            completion:^(AgoraChatSilentModeResult *_Nullable aResult,
                                         AgoraChatError *_Nullable aError) {
                              [weakSelf onResult:result
                                  withMethodType:aChannelName
                                       withError:aError
                                      withParams:nil];
                            }];
}

- (void)fetchConversationSilentMode:(NSDictionary *)param
                     withMethodType:(NSString *)aChannelName
                             result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    NSString *conversaionId = param[@"conversationId"];
    AgoraChatConversationType type =
        [AgoraChatConversation typeFromInt:[param[@"conversationType"] intValue]];
    [AgoraChatClient.sharedClient.pushManager
        getSilentModeForConversation:conversaionId
                    conversationType:type
                          completion:^(AgoraChatSilentModeResult *_Nullable aResult,
                                       AgoraChatError *_Nullable aError) {
                            [weakSelf onResult:result
                                withMethodType:aChannelName
                                     withError:aError
                                    withParams:[aResult toJsonObject]];
                          }];
}

- (void)setSilentModeForAll:(NSDictionary *)param
             withMethodType:(NSString *)aChannelName
                     result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    AgoraChatSilentModeParam *silmentParam =
        [AgoraChatSilentModeParam fromJsonObject:param[@"param"]];
    [AgoraChatClient.sharedClient.pushManager
        setSilentModeForAll:silmentParam
                 completion:^(AgoraChatSilentModeResult *_Nullable aResult,
                              AgoraChatError *_Nullable aError) {
                   [weakSelf onResult:result
                       withMethodType:aChannelName
                            withError:aError
                           withParams:nil];
                 }];
}

- (void)fetchSilentModeForAll:(NSDictionary *)param
               withMethodType:(NSString *)aChannelName
                       result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    [AgoraChatClient.sharedClient.pushManager
        getSilentModeForAllWithCompletion:^(
            AgoraChatSilentModeResult *_Nullable aResult, AgoraChatError *_Nullable aError) {
          [weakSelf onResult:result
              withMethodType:aChannelName
                   withError:aError
                  withParams:[aResult toJsonObject]];
        }];
}

- (void)fetchSilentModeForConversations:(NSDictionary *)param
                         withMethodType:(NSString *)aChannelName
                                 result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    NSMutableArray *conversations = [NSMutableArray array];
    NSArray *convs = param[@"convs"];
    for (int i = 0; i < convs.count; ++i) {
        AgoraChatConversation *conversation =
            [self getConversation:[convs objectAtIndex:i]];
        [conversations addObject:conversation];
    }
    [AgoraChatClient.sharedClient.pushManager
        getSilentModeForConversations:conversations
                           completion:^(
                               NSDictionary<NSString *, AgoraChatSilentModeResult *>
                                   *_Nullable aResult,
                               AgoraChatError *_Nullable aError) {
                             NSMutableDictionary *tmpDict =
                                 [NSMutableDictionary dictionary];
                             for (NSString *conversationId in aResult.allKeys) {
                                 tmpDict[conversationId] =
                                     [aResult[conversationId] toJsonObject];
                             }
                             [weakSelf onResult:result
                                 withMethodType:aChannelName
                                      withError:aError
                                     withParams:tmpDict];
                           }];
}

- (void)setPreferredNotificationLanguage:(NSDictionary *)param
                          withMethodType:(NSString *)aChannelName
                                  result:
                                      (nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    NSString *code = param[@"code"];
    [AgoraChatClient.sharedClient.pushManager
        setPreferredNotificationLanguage:code
                              completion:^(AgoraChatError *_Nullable aError) {
                                [weakSelf onResult:result
                                    withMethodType:aChannelName
                                         withError:aError
                                        withParams:nil];
                              }];
}

- (void)fetchPreferredNotificationLanguage:(NSDictionary *)param
                            withMethodType:(NSString *)aChannelName
                                    result:
                                        (nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    [AgoraChatClient.sharedClient.pushManager
        getPreferredNotificationLanguageCompletion:^(
            NSString *_Nullable aLaguangeCode, AgoraChatError *_Nullable aError) {
          [weakSelf onResult:result
              withMethodType:aChannelName
                   withError:aError
                  withParams:aLaguangeCode];
        }];
}

- (void)setPushTemplate:(NSDictionary *)param
         withMethodType:(NSString *)aChannelName
                 result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    NSString *pushTemplateName = param[@"templateName"];
    [AgoraChatClient.sharedClient.pushManager
        setPushTemplate:pushTemplateName
             completion:^(AgoraChatError *_Nullable aError) {
               [weakSelf onResult:result
                   withMethodType:aChannelName
                        withError:aError
                       withParams:nil];
             }];
}

- (void)getPushTemplate:(NSDictionary *)param
         withMethodType:(NSString *)aChannelName
                 result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    [AgoraChatClient.sharedClient.pushManager
        getPushTemplate:^(NSString *_Nullable aPushTemplateName,
                          AgoraChatError *_Nullable aError) {
          [weakSelf onResult:result
              withMethodType:aChannelName
                   withError:aError
                  withParams:@{@"templateName" : aPushTemplateName}];
        }];
}

@end
