//
//  CalendarManager.m
//  RCTAPMBanner
//
//  Created by Vincent Ducastel on 09/08/2017.
//  Copyright © 2017 Facebook. All rights reserved.
//

#import "CalendarManager.h"
#import <React/RCTLog.h>
#import <React/RCTConvert.h>

@implementation CalendarManager

// To export a module named CalendarManager
RCT_EXPORT_MODULE();
// This would name the module AwesomeCalendarManager instead
// RCT_EXPORT_MODULE(AwesomeCalendarManager);

RCT_EXPORT_METHOD(addEvent:(NSString *)name details:(NSDictionary *)details)
{
  NSString *location = [RCTConvert NSString:details[@"location"]];
  NSDate *date = [RCTConvert NSDate:details[@"date"]];
  NSString *description = [RCTConvert NSString:details[@"description"]];
  
  RCTLogInfo(@"Pretending to create an event %@", name);
  RCTLogInfo(@"at %@", location);
  RCTLogInfo(@"when %@", date);
  RCTLogInfo(@"description %@", description);
}

RCT_EXPORT_METHOD(findEvents:(RCTResponseSenderBlock)callback)
{
  NSArray *events = @[@"event 1", @"event 2", @"event 3"];
  callback(@[[NSNull null], events]); //Impossible de passer NSError en premier paramètre, j'ai un null dans le JS, finir ça.
}

RCT_EXPORT_METHOD(findEventsWithResolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
  NSArray *events = @[@"event 4", @"event 5", @"event 6"];
  if (events) {
    resolve(events);
  } else {
    NSError *error = [NSError errorWithDomain:@"" code:500 userInfo:nil];
    reject(@"no_events", @"There were no events", error);
  }
}

//The specified methodQueue will be shared by all of the methods in your module.
//**//
- (dispatch_queue_t)methodQueue
{
  return dispatch_get_main_queue();
}
//- (dispatch_queue_t)methodQueue
//{
//  return dispatch_queue_create("com.facebook.React.AsyncLocalStorageQueue", DISPATCH_QUEUE_SERIAL);
//}
//**//

//A native module can export constants that are immediately available to JavaScript at runtime. This is useful for communicating static data that would otherwise require a round-trip through the bridge.
- (NSDictionary *)constantsToExport
{
  return @{ @"firstDayOfTheWeek": @"Monday" };
}

@end
