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

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

@interface RNCCPaginator()
@end

@implementation RNCCPaginator
@synthesize paginators;

#pragma mark Shared / Singleton
+ (id)shared {
    static RNCCPaginator *shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared = [[self alloc] init];
        shared.paginators = [[NSMutableDictionary alloc] init];
    });
    return shared;
}

+ (NSString*)setPaginator:(id)paginator {
    RNCCPaginator *_paginator = [RNCCPaginator shared];
    NSString *uuid = [[NSUUID UUID] UUIDString];
    [_paginator.paginators setValue:paginator forKey:uuid];
    return uuid;
}

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

RCT_REMAP_METHOD(requestNextPage, sid:(NSString*)sid nextPage_resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
    NSMutableDictionary *_paginators = [[RNCCPaginator shared] paginators];
    [[_paginators objectForKey:sid] requestNextPageWithCompletion:^(TCHResult *result, TCHChannelDescriptorPaginator *paginator) {
        if (result.isSuccessful) {
            resolve(@{ @"sid":[RNCCPaginator setPaginator:paginator], @"type": @"Channel",@"paginator": [RCTConvert channelDescriptorPaginator:paginator]});
        }
        else {
            reject(@"requestNextPageError", @"Error occured while attempting to request the next page.", result.error);
        }
    }];
}

RCT_REMAP_METHOD(requestNextPageChannelDescriptors, sid:(NSString*)sid nextPage_channel_resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
    NSMutableDictionary *_paginators = [[RNCCPaginator shared] paginators];
    [[_paginators objectForKey:sid] requestNextPageWithCompletion:^(TCHResult *result, TCHChannelDescriptorPaginator *paginator) {
        if (result.isSuccessful) {
            resolve(@{ @"sid":[RNCCPaginator setPaginator:paginator], @"type": @"ChannelDescriptor",@"paginator": [RCTConvert channelDescriptorPaginator:paginator]});
        }
        else {
            reject(@"requestNextPageChannelDescriptorsError", @"Error occured while attempting to request the next page.", result.error);
        }
    }];
}

RCT_REMAP_METHOD(requestNextPageMembers, sid:(NSString*)sid nextPage_members_resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
    NSMutableDictionary *_paginators = [[RNCCPaginator shared] paginators];
    [[_paginators objectForKey:sid] requestNextPageWithCompletion:^(TCHResult *result, TCHMemberPaginator *paginator) {
        if (result.isSuccessful) {
            resolve(@{ @"sid":[RNCCPaginator setPaginator:paginator], @"type": @"ChannelMember",@"paginator": [RCTConvert memberPaginator:paginator]});
        }
        else {
            reject(@"requestNextPageMembersError", @"Error occured while attempting to request the next page.", result.error);
        }
    }];
}


@end
