// //
// //  MapViewController.m
// //  sample
// //
// //  Created by Tony on 8/17/23.
// //

// #import <Foundation/Foundation.h>

// #import "MapViewController.h"
// #import "Headers/JMapCoreKit.h"
// #import "Headers/JMapController.h"
// #import "Headers/JMap.h"
// #import "MapViewController.h"
// // #import "EventEmitter.h"



// @implementation MapViewController
// {
//   bool hasListeners;
//   CTPEventEmitter *eventEmitter;
    
// }
// - (void)viewDidLoad {
//     [super viewDidLoad];

    
//     NSLog(@"Loading Map View Controller");

//     JMapOptions *options = [[JMapOptions alloc] init];
//     options.customerId = 450;
//     options.venueId = 2344;
//     options.host = @"https://api.jibestream.com";
//     options.clientId = @"5cabbd60-ace7-427e-a677-bc1da0074217";
//     options.clientSecret = @"hga/7X2YTwSaCQRZRdKSFzgqaFAc298yAD+qbycH90I=";
//     options.stage = self.canvas;
//     options.clearCacheData = true;
//     options.autoReloadCache = [NSNumber numberWithBool:true];
    
//     //Initialize JMap
//     _jMap = [[JMap alloc] initWithOptions:options];
//     _jMap.delegate = self;

//     eventEmitter = [CTPEventEmitter allocWithZone: nil];
//     self.extend = [Extend sharedExtendWithKey:@"Aw8AAAAAAAAAAAAAB_EQgAAAAAAH526AAAABjcz8gTOSGNRz5n_zeh6fqGzVpqMQTcWa6rvA2C_s7-PzmEzERIBb0xNK1qFWBg9q5GgyMPjrnopRBhDlMaQLhZ1GzhhM72FXlGLxx8sEa4cS-9tLRLH5zbaTNNDeUgrgtKu23mY4UieyiVio-_4jOec-siieHeH760m9hfxbMVA" url:@"https://v2.tendegrees.net" delegate:self];
//     self.mobilitySwitch.on = true;
//     self.claritySwitch.on = true;
//     self.vicinitySwitch.on = true;
// }

// - (void)jmapInitialized:(JMapError *)error {
//     if (error) {
//         NSLog(@"Error: %@", error.message);
//     } else {
//         self.controller = _jMap.controller;
//         [self setupUIKit];
//       NSLog(@"JMap Controller Initializes");
// //        [self drawWayfindingPath];
//     }
// }

// - (void)didReceiveMemoryWarning {
//     [super didReceiveMemoryWarning];
//     // Dispose of any resources that can be recreated.
// }

// - (void)setupUIKit {
//     // Initialize UI Kit
//     self.mapUIKit = [[JMapUI alloc] initWithController:self.controller andViewController:self andOptions:nil];
//     self.mapUIKit.delegate = self;
    
//     // Call to show map elements
//     [self loadUIElements];
// }

// - (void)loadUIElements {
//     // Call to load floor selector
//     // [self loadFloorSelector];
    
//     // Call to load search bar
//     [self loadSearchBar];
    
//     // Call to load compass
//     [self loadCompass];
// }

// - (void)loadSearchBar {
//     // Load Search Bar
//     NSDictionary * searchOption = @{@"placeholder": @"Search", @"maxResults": @3, @"position": @0.1};
//     [self.mapUIKit renderSearchWithOptions:searchOption];
// }

// - (void)loadCompass {
//     // Load compass
//     NSDictionary *compassOption = @{@"resetRotationOnTap": @1, @"position": @[@0.05, @0.95], @"width": @75, @"height": @75};
//     [self.mapUIKit renderCompassWithOptions:compassOption];
// }

// - (void)canvas:(nonnull JMapCanvas *)canvas scrollViewDidRotateByDegrees:(float)degrees {
//     [self.mapUIKit updateCompass:degrees];
// }

// - (void)onSelect:(id)item {
//     // Handle search bar selection
//     if ([item isKindOfClass:[JMapDestination class]]) {
//         JMapDestination *dest = (JMapDestination *)item;
        
//         // Check to see if selected destination is on current viewing map
//         if (dest.locations.firstObject.mapId.integerValue != self.controller.currentMap.id.integerValue) {
//             JMapMap *map = [self.controller.activeVenue.maps getById:dest.locations.firstObject.mapId.integerValue];
//             // Call to change map to the destination map
//             [self.controller showMap:map completionHandler:^(JMapError * _Nullable error) {
//                 [self highlightShapesAtDestination:dest];
//                 [self drawWayfindingPath:dest];
//             }];
//         } else {
//             [self highlightShapesAtDestination:dest];
//             [self drawWayfindingPath:dest];
//         }
//     }
// }

// - (void)highlightShapesAtDestination:(JMapDestination *)dest {
//     // Get shapes from destination
//     [self.controller getUnitsFromDestination:dest completionHandler:^(NSArray<Shape *> * _Nonnull shapes, JMapError * _Nullable error) {
        
//         // Get bounds of the shape
//         CGRect bounds = shapes[0].pathFrame;
//         [self.controller fitBoundsInView:bounds];
        
//         // Set highlight color
//         JMapStyle *highlight = [[JMapStyle alloc] init];
//         [highlight setFillColor:[UIColor blueColor]];
        
//         // Apply color to shape
//         [self.controller styleShapes:shapes withStyling:highlight];
//     }];
// }

// - (void)canvas:(JMapCanvas *)canvas detectedTapOnElement:(id)element atPoint:(CGPoint)point {
//     if ([element isKindOfClass:[Shape class]]) {
//         Shape *shape = element;
        
//         // Get data from shape
//         JMapWaypoint *waypoint = [self.controller getWaypointsFromShape:shape][0];
//         JMapDestination *destination = [self.controller getDestinationsFromShape:shape][0];
        
//         if (destination) {
//             // Add styling for popup
//             JMapStyle *btnStyle = [[JMapStyle alloc] init];
//             [btnStyle setFillColor:[UIColor blueColor]];
            
//             JMapStyle *popupStyle = [[JMapStyle alloc] init];
//             [popupStyle setFillColor:[UIColor whiteColor]];
            
//             UIFont *titleFont = [UIFont systemFontOfSize:18];
//             UIColor *titleFontColor = [UIColor blackColor];
            
//             UIFont *subFont = [UIFont systemFontOfSize:12];
//             UIColor *subFontColor = [UIColor grayColor];
            
//             // Add options to popup
//             NSDictionary *popupOption = @{@"coordinates": [NSValue valueWithCGPoint:point], @"titleText": destination.name, @"subText": destination.description, @"titleFont": titleFont, @"subFont": subFont, @"showActionButton": @1, @"actionButtonText": @"Action", @"actionButtonFont": [UIFont systemFontOfSize:15], @"actionButtonStyle": btnStyle, @"popupStyle": popupStyle, @"meta": waypoint, @"titleFontColor": titleFontColor, @"subFontColor": subFontColor, @"actionButtonFontColor": [UIColor whiteColor]};
            
//             [self.mapUIKit renderPopupWithOptions:popupOption];
//         }
//     }
// }


// - (void)drawWayfindingPath:(JMapDestination *)destination {
    
    
// //    JMapPoint * centerPoint = [[JMapPoint alloc] initWithX:0 Y:0 Z:0];
//     JMapPoint *point = [[JMapPoint alloc] initWithX:self.controller.userLocation.position.x Y:self.controller.userLocation.position.y Z:self.controller.userLocation.mapId];
    
//     JMapWaypoint * userStartPoint = [self.controller getNearestWaypointWithJMapPoint:point];

// //    NSArray <JMapWaypoint*>* destinationWayPoint = [self.controller.currentMap.waypoints getByDestination:destination];
// //
// //
// //    NSArray <JMapPathPerFloor *> *paths = [self.controller  wayfindBetweenWaypoint:userStartPoint andWaypoint:destinationWayPoint.firstObject withAccessibility:100 withObstacle:nil];
// //
// //    [self.controller drawWayfindingPaths:paths];
// //    [self.controller.userLocation snapToWayfindingPathWithThreshold:20];
//     // Get all destinations on current map
//     NSArray <JMapDestination *>* destinations = [self.controller.activeVenue.destinations getByMap:self.controller.currentMap];

//     if (destinations.count > 1) {
//         // Get 2 destinations
//         JMapDestination * d1 = destinations[0];
//         JMapDestination * d2 = destinations[destinations.count - 1];

//         // Extract waypoints
// //        NSArray <JMapWaypoint*>* waypoints1 = [self.controller.currentMap.waypoints getByDestination:d1];
//         NSArray <JMapWaypoint*>* waypoints2 = [self.controller.currentMap.waypoints getByDestination:destination];


// //        if(waypoints1.count > 0 && waypoints2.count > 0) {
// //        JMapWaypoint *wp1 = navPoint;//waypoints1.firstObject;
//             JMapWaypoint *wp2 = waypoints2.firstObject;

//             NSArray <JMapPathPerFloor *> *paths = [self.controller  wayfindBetweenWaypoint:userStartPoint andWaypoint:wp2 withAccessibility:100 withObstacle:nil];

//             [self.controller drawWayfindingPaths:paths];

//             // assuming user location at start waypoint. Get dimensions of the start waypoint
// //            double userX = wp1.coordinates.firstObject.doubleValue;
// //            double userY = wp1.coordinates.lastObject.doubleValue;

// //            CGPoint point = CGPointMake(userX, userY);
// //            [self.controller updateUserLocation:point floorMap:self.controller.currentMap orientation:@0 confidenceRadius:@50];
//             //set snap to wayfind threshold
//             [self.controller.userLocation snapToWayfindingPathWithThreshold:20];


// //        }
//     }
// }




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

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

//   NSLog(@"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 = @{
//     @"lat": ObjectOrNull(@(navOutput.latitude)),
//     @"long": 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))
//   };

//     CGPoint convertPoint = CGPointMake(navOutput.longitude, navOutput.latitude);
    
//     // Pass those values into the utility class
//     convertPoint = [JMapUtils convertPoint:convertPoint fromProjection:@"EPSG:4326" toProjection:@"jmap:local" inVenue:self.controller.activeVenue];
    
//     // Use the converted point to update user location
//     // Note: instead of using controller.currentMap please refer to the map that you get back from the data. Typically your IPS will provide a "level" sequence which will match up with our Floor level
//   [self.controller updateUserLocation:convertPoint floorMap:self.controller.currentMap orientation:0 confidenceRadius:@100];


  
//   [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];
  
// }

// - (void) dataLoadEvent: (NSString *)data
// {
//     NSLog(@"On Region Change");
//     NSLog(data);
    
// }


// @end
