#import <React/RCTViewManager.h>
#import <MapKit/MapKit.h>
#import "OdpModule.h"
#import "Headers/extend.h"
#import "EventEmitter.m"

@interface CtpOdpViewManager : RCTViewManager <RCTBridgeModule, OnExtendDelegate >


  @property (strong, nonatomic) Extend * extend;

  - (void) enableExtend: (bool) enabled;
  @property (weak, nonatomic) IBOutlet UIPickerView *destinationPicker;
  @property (weak, nonatomic) IBOutlet UISwitch *claritySwitch;
  @property (weak, nonatomic) IBOutlet UISwitch *vicinitySwitch;
  @property (weak, nonatomic) IBOutlet UISwitch *mobilitySwitch;
  @property (weak, nonatomic) IBOutlet UILabel *regionLabel;
  @property (weak, nonatomic) IBOutlet UILabel *waypointLabel;
  @property (weak, nonatomic) IBOutlet UILabel *destinationLabel;
  @property (weak, nonatomic) IBOutlet UILabel *routeLengthLabel;
  @property (weak, nonatomic) IBOutlet UILabel *routeNodeCountLabel;
  @property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
  @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
  @property (weak, nonatomic) IBOutlet UILabel *bearingLabel;
  @property (weak, nonatomic) IBOutlet UILabel *floorLabel;
  @property (weak, nonatomic) IBOutlet UIButton *masterSwitch;
  @property (weak, nonatomic) IBOutlet UIControl *pickerTapControl;


@end

@implementation CtpOdpViewManager
{
  bool hasListeners;
  CTPEventEmitter *eventEmitter;
}

RCT_EXPORT_MODULE(CtpOdpView)

- (UIView *)view
{
  return [[UIView alloc] init];
}

RCT_EXPORT_METHOD(start:(NSString *) args)
{
  NSLog(@"Apple Start Function");
  eventEmitter = [CTPEventEmitter allocWithZone: nil];
  self.extend = [Extend sharedExtendWithKey:@"Aw8AAAAAAAAAAAAAB_EQgAAAAAAH526AAAABjcz8gTOSGNRz5n_zeh6fqGzVpqMQTcWa6rvA2C_s7-PzmEzERIBb0xNK1qFWBg9q5GgyMPjrnopRBhDlMaQLhZ1GzhhM72FXlGLxx8sEa4cS-9tLRLH5zbaTNNDeUgrgtKu23mY4UieyiVio-_4jOec-siieHeH760m9hfxbMVA" url:@"https://portal.tendegrees.net" delegate:self];

      self.mobilitySwitch.on = true;
      self.claritySwitch.on = true;
      self.vicinitySwitch.on = true;
}

static id ObjectOrNull(id object){
  return object ?: [NSNull null];
}

// - (void) onReady:(bool)ready
// {
//   NSDictionary *eventData = @{
//     @"ready": @(ready),
//   };

//   // [eventEmitter sendEvent:@"onReady" body:eventData];
// }

- (void) onKeyValidation:(bool)validated
{
    
    if (![self.extend isServiceStarted] && validated)
    {
      NSLog(@"On Key Validation");
      [self.extend setVicinityEnabled:true];
      [self.extend start];
       
    }else{
      NSLog(@"Failed to Validate Key");
    }
  
  NSDictionary *eventData = @{
    @"validated": @(validated),
  };

  // [eventEmitter sendEvent:@"onKeyValidation" body:eventData];
}

- (void) onNavigationUpdate:(TDNavOutput *)navOutput{
  NSDictionary *eventData = @{
    @"latitude": ObjectOrNull(@(navOutput.latitude)),
    @"longitude": ObjectOrNull(@(navOutput.longitude)),
    @"bearing": ObjectOrNull(@(navOutput.bearing)),
    @"viewHeading": ObjectOrNull(@(navOutput.viewHeading)),
    @"floor": ObjectOrNull(@(navOutput.floor)),
    @"accuracyRadius": ObjectOrNull(@(navOutput.accuracyRadius)),
    @"utc": ObjectOrNull(@(navOutput.utc)),
    @"x": ObjectOrNull(@(navOutput.x)),
    @"y": ObjectOrNull(@(navOutput.y)),
    @"xFom": ObjectOrNull(@(navOutput.xFom)),
    @"yFom": ObjectOrNull(@(navOutput.yFom)),
    @"usedSystems": ObjectOrNull(@(navOutput.usedSystems)),
    @"dynamicState": ObjectOrNull(@(navOutput.dynamicState)),
    @"elevationState": ObjectOrNull(@(navOutput.elevationState)),
    @"pdrSpeedLimitExceeded": ObjectOrNull(@(navOutput.pdrSpeedLimitExceeded))
  };
  
  [eventEmitter sendEvent:@"onNavigationUpdate" body:eventData];
}

- (void) onFacilityUpdateName:(NSString *)name onFacilityUpdateLocation:(Location *)location onFacilityUpdateRegion:(Region *)region onFacilityUpdateFloorPlans:(NSArray *)plans onFacilityUpdateCategories:(NSArray *)categories {
  
    NSDictionary *locationDict = @{
      @"latitude": ObjectOrNull(@(location.latLong.latitude)),
      @"longitude": ObjectOrNull(@(location.latLong.longitude)),
      @"x": ObjectOrNull(@(location.x)),
      @"y": ObjectOrNull(@(location.y)),
      @"z": ObjectOrNull(@(location.z)),
      @"altitude": ObjectOrNull(@(location.altitude)),
      @"isGeo": ObjectOrNull(@(location.isGeo)),
      
    };
  
  
    NSDictionary *regionDict = @{
      @"floor": ObjectOrNull(@(region.floor)),
      @"id": ObjectOrNull(@(region.uid)),
      @"name": ObjectOrNull(region.name),
      @"icon": ObjectOrNull(region.icon),
      @"type": ObjectOrNull(region.type),
      @"points": ObjectOrNull(region.locations),
      @"distanceToUser": ObjectOrNull(@(region.distanceToUser)),
      @"userOnSameFloor": ObjectOrNull(@(region.userOnSameFloor)),
      @"className": ObjectOrNull(region.type),
    };
  
  NSDictionary *eventData = @{
    @"name": name,
    @"location": locationDict,
    @"region": regionDict,
    @"floorPlans": ObjectOrNull(plans),
  };
  
  // [eventEmitter sendEvent:@"onFacilityUpdate" body:eventData];
  
}




RCT_CUSTOM_VIEW_PROPERTY(color, NSString, UIView)
{
  [view setBackgroundColor:[self hexStringToColor:json]];
}

- hexStringToColor:(NSString *)stringToConvert
{
  NSString *noHashString = [stringToConvert stringByReplacingOccurrencesOfString:@"#" withString:@""];
  NSScanner *stringScanner = [NSScanner scannerWithString:noHashString];

  unsigned hex;
  if (![stringScanner scanHexInt:&hex]) return nil;
  int r = (hex >> 16) & 0xFF;
  int g = (hex >> 8) & 0xFF;
  int b = (hex) & 0xFF;

  NSLog(@"Apple Start Function");
  eventEmitter = [CTPEventEmitter allocWithZone: nil];
  self.extend = [Extend sharedExtendWithKey:@"Aw8AAAAAAAAAAAAAB_EQgAAAAAAH526AAAABjcz8gTOSGNRz5n_zeh6fqGzVpqMQTcWa6rvA2C_s7-PzmEzERIBb0xNK1qFWBg9q5GgyMPjrnopRBhDlMaQLhZ1GzhhM72FXlGLxx8sEa4cS-9tLRLH5zbaTNNDeUgrgtKu23mY4UieyiVio-_4jOec-siieHeH760m9hfxbMVA" url:@"https://portal.tendegrees.net" delegate:self];

      self.mobilitySwitch.on = true;
      self.claritySwitch.on = true;
      self.vicinitySwitch.on = true;



  return [UIColor colorWithRed:1.0f green:0.0f blue:1.0f alpha:1.0f];
}

@end
