/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import "AppDelegate.h"
#import <CoreLocation/CoreLocation.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <AVFoundation/AVFoundation.h>
#import <EventKit/EventKit.h>

@interface AppDelegate ()
@property (nonatomic,copy)CLLocationManager *locationManager;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [self getCameraOpenAuthorisation];
  [self enableLocationService];
  [self enableCalenderAccess];
  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"PokktReactDemo"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}
-(void)enableLocationService
{
  _locationManager = [[CLLocationManager alloc] init];
  [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
  _locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
  _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  
  if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
  {
    [_locationManager requestWhenInUseAuthorization];
    [_locationManager requestAlwaysAuthorization];
  }
}
-(void)getCameraOpenAuthorisation
{
  NSString *mediaType = AVMediaTypeVideo;
  AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
  if(authStatus == AVAuthorizationStatusAuthorized) {
    // do your logic
  } else if(authStatus == AVAuthorizationStatusDenied){
    // denied
  } else if(authStatus == AVAuthorizationStatusRestricted){
    // restricted, normally won't happen
  } else if(authStatus == AVAuthorizationStatusNotDetermined){
    // not determined?!
    [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
      if(granted){
        NSLog(@"Granted access to %@", mediaType);
      } else {
        NSLog(@"Not granted access to %@", mediaType);
      }
    }];
  } else {
    // impossible, unknown authorization status
  }
}
-(void)enableCalenderAccess
{
  EKEventStore *eventStore = [[EKEventStore alloc] init];
  
  if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
  {
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
      dispatch_async(dispatch_get_main_queue(), ^{
        
      });
    }];
  }
  
}
@end
