//
//  ExtSdkUserInfoManagerWrapper.m
//  im_flutter_sdk
//
//  Created by liujinliang on 2021/4/26.
//

#import "ExtSdkUserInfoManagerWrapper.h"
#import "ExtSdkClientWrapper.h"
#import "ExtSdkMethodTypeObjc.h"
#import "ExtSdkToJson.h"

@interface ExtSdkUserInfoManagerWrapper ()

@end

@implementation ExtSdkUserInfoManagerWrapper

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

- (void)updateOwnUserInfo:(NSDictionary *)param
           withMethodType:(NSString *)aChannelName
                   result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    AgoraChatUserInfo *userInfo = [AgoraChatUserInfo fromJsonObject:param[@"userInfo"]];
    [AgoraChatClient.sharedClient.userInfoManager
        updateOwnUserInfo:userInfo
               completion:^(AgoraChatUserInfo *aUserInfo, AgoraChatError *aError) {
                 NSDictionary *objDic = [aUserInfo toJsonObject];

                 [weakSelf onResult:result
                     withMethodType:aChannelName
                          withError:aError
                         withParams:objDic];
               }];
}

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

    int typeValue = [param[@"userInfoType"] intValue];
    AgoraChatUserInfoType userInfoType = [self userInfoTypeFromInt:typeValue];
    NSString *userInfoValue = param[@"userInfoValue"];

    [AgoraChatClient.sharedClient.userInfoManager
        updateOwnUserInfo:userInfoValue
                 withType:userInfoType
               completion:^(AgoraChatUserInfo *aUserInfo, AgoraChatError *aError) {
                 NSDictionary *objDic = [aUserInfo toJsonObject];
                 [weakSelf onResult:result
                     withMethodType:aChannelName
                          withError:aError
                         withParams:objDic];
               }];
}

- (void)fetchUserInfoById:(NSDictionary *)param
           withMethodType:(NSString *)aChannelName
                   result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    NSArray *userIds = param[@"userIds"];

    [AgoraChatClient.sharedClient.userInfoManager
        fetchUserInfoById:userIds
               completion:^(NSDictionary *aUserDatas, AgoraChatError *aError) {
                 NSMutableDictionary *dic = NSMutableDictionary.new;
                 [aUserDatas enumerateKeysAndObjectsUsingBlock:^(
                                 id _Nonnull key, id _Nonnull obj,
                                 BOOL *_Nonnull stop) {
                   dic[key] = [(AgoraChatUserInfo *)obj toJsonObject];
                 }];

                 [weakSelf onResult:result
                     withMethodType:aChannelName
                          withError:aError
                         withParams:[dic copy]];
               }];
}

- (void)fetchUserInfoByIdWithType:(NSDictionary *)param
                   withMethodType:(NSString *)aChannelName
                           result:(nonnull id<ExtSdkCallbackObjc>)result {
    __weak typeof(self) weakSelf = self;
    NSArray *userIds = param[@"userIds"];
    NSArray<NSNumber *> *userInfoTypes = param[@"userInfoTypes"];

    [AgoraChatClient.sharedClient.userInfoManager
        fetchUserInfoById:userIds
                     type:userInfoTypes
               completion:^(NSDictionary *aUserDatas, AgoraChatError *aError) {
                 NSMutableDictionary *dic = NSMutableDictionary.new;
                 [aUserDatas enumerateKeysAndObjectsUsingBlock:^(
                                 id _Nonnull key, id _Nonnull obj,
                                 BOOL *_Nonnull stop) {
                   dic[key] = [(AgoraChatUserInfo *)obj toJsonObject];
                 }];

                 [weakSelf onResult:result
                     withMethodType:aChannelName
                          withError:aError
                         withParams:dic];
               }];
}

- (AgoraChatUserInfoType)userInfoTypeFromInt:(int)typeValue {
    AgoraChatUserInfoType userInfoType;

    switch (typeValue) {
    case 0:
        userInfoType = AgoraChatUserInfoTypeNickName;
        break;
    case 1:
        userInfoType = AgoraChatUserInfoTypeAvatarURL;
        break;
    case 2:
        userInfoType = AgoraChatUserInfoTypePhone;
        break;
    case 3:
        userInfoType = AgoraChatUserInfoTypeMail;
        break;
    case 4:
        userInfoType = AgoraChatUserInfoTypeGender;
        break;
    case 5:
        userInfoType = AgoraChatUserInfoTypeSign;
        break;
    case 6:
        userInfoType = AgoraChatUserInfoTypeBirth;
        break;
    case 7:
        userInfoType = AgoraChatUserInfoTypeExt;
        break;
    default:
        userInfoType = AgoraChatUserInfoTypeNickName;
        break;
    }

    return userInfoType;
}

@end
