//
//  RNCCMembers.m
//  RNContextCenter
//
//  Created by Shaffiulla Khan on 07/01/19.
//  Copyright © 2019 Impekable LLC. All rights reserved.
//

#import "RNCCMembers.h"
#import "RNCCClient.h"
#import "RNCCChannels.h"
#import "RNCCPaginator.h"
#import <React/RCTUtils.h>
#import "RCTConvert+ContextCenter.h"

@interface RNCCMembers()
@end

@implementation RNCCMembers

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

- (void)fetchMembersFromChannelSid:(NSString *)sid completion:(void (^)(TCHResult *result, TCHMembers *members))completion {
    [RNCCChannels fetchChannelForSid:sid completion:^(TCHResult *result, TCHChannel *channel) {
        completion(result, [channel members]);
    }];
}

#pragma mark
RCT_REMAP_METHOD(fetchMembers, channelSid:(NSString *)channelSid resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    [self fetchMembersFromChannelSid:channelSid completion:^(TCHResult *result, TCHMembers *members) {
        if (result.isSuccessful) {
            [members membersWithCompletion:^(TCHResult *result, TCHMemberPaginator *paginator) {
                if (result.isSuccessful) {
                    NSString *uuid = [RNCCPaginator setPaginator:paginator];
                    resolve(@{@"sid":uuid, @"type": @"Member",@"paginator": [RCTConvert memberPaginator:paginator]});
                }
                else {
                    reject(@"fetchMembersError", @"Error occured while attempting to get the members.", result.error);
                }
            }];
        }
        else {
            reject(@"fetchMembersError", @"Error occured while attempting to get the members.", result.error);
        }
    }];
}

RCT_REMAP_METHOD(addMember, channelSid:(NSString *)channelSid identity:(NSString *)identity add_resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    [self fetchMembersFromChannelSid:channelSid completion:^(TCHResult *result, TCHMembers *members) {
        if (result.isSuccessful) {
            [members addByIdentity:identity completion:^(TCHResult *result) {
                if (result.isSuccessful) {
                    resolve(@[@TRUE]);
                }
                else {
                    reject(@"addMemberError", @"Error occured while attempting to add a user to the channel.", result.error);
                }
            }];
        }
        else {
            reject(@"addMemberError", @"Error occured while attempting to add a user to the channel.", result.error);
        }
    }];
}

RCT_REMAP_METHOD(inviteMember, channelSid:(NSString *)channelSid identity:(NSString *)identity invite_resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    [self fetchMembersFromChannelSid:channelSid completion:^(TCHResult *result, TCHMembers *members) {
        if (result.isSuccessful) {
            [members inviteByIdentity:identity completion:^(TCHResult *result) {
                if (result.isSuccessful) {
                    resolve(@[@TRUE]);
                }
                else {
                    reject(@"inviteMemberError", @"Error occured while attempting to inviate a user to the channel.", result.error);
                }
            }];
        }
        else {
            reject(@"inviteMemberError", @"Error occured while attempting to inviate a user to the channel.", result.error);
        }
    }];
}

RCT_REMAP_METHOD(removeMember, channelSid:(NSString *)channelSid identity:(NSString *)identity remove_resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    [RNCCChannels fetchChannelForSid:channelSid completion:^(TCHResult *result, TCHChannel *channel) {
        TCHMember *member = [channel memberWithIdentity:identity];
        [[channel members] removeMember:member completion:^(TCHResult *result) {
            if (result.isSuccessful) {
                resolve(@[@TRUE]);
            }
            else {
                reject(@"removeMemberError", @"Error occured while attempting to remove a user from the channel.", result.error);
            }
        }];
    }];
}

@end
