//
//  RNTMapManager.m
//  AwesomeProject3
//
//  Created by Macbook Pro on 27/06/2023.
//

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <React/RCTViewManager.h>
#import "TPLMapVieww.h"
#import "AwesomeProject3-Bridging-Header.h"
#import "MyObjectiveCClass.h"
#import <React/RCTBridge.h>
#import "CustomEventEmitter.h"






@import TPLMaps;

@interface RNTMapManager : RCTViewManager <TPLMapViewDelegate>
//@property (nonatomic, strong) CustomEventEmitter *eventEmitter;
//@property (nonatomic, strong) RNTMapManager *rntMapManager;

@end

@implementation RNTMapManager

TPLMapView  *mapView;

RCT_EXPORT_MODULE(RNTMap)
-(UIView *) view
{
  
  mapView = [[TPLMapView alloc] init];
//  self.eventEmitter = [CustomEventEmitter new];
//  self.rntMapManager = [RNTMapManager new];

  //The map view delegate
  mapView.delegate = self;

  //Show user location on map. Read documentation for more info
  mapView.showsUserLocation = true;

  //Show user location button on map. Read documentation for more info
  mapView.myLocationButtonEnabled = false;

  //Show map zoom controls
  mapView.zoomControlsEnabled = true;

  //Show compass on map
  //mapView.showsCompass = true;

  //Show POI's in map
  mapView.showsPointsOfInterest = true;

  //Show Buildings on map
  mapView.showsBuildings = true;

  //Enum provide MapView theme Day | Night.
  mapView.mapTheme = DAY;

  //Wether pan gesture is enabled or disabled on map.
  mapView.scrollEnabled = true;

  //Wether tilt gesture is enabled or disabled on map.
  mapView.tiltEnabled = true;

  //Wether rotate gesture is enabled or disabled on map.
  mapView.rotateEnabled = true;

  //Whether all gestures should be enabled (default) or disabled.
  mapView.allGesturesEnabled = true;
  mapView.trafficEnabled = false;
    mapView.zoomControlsEnabled = false;


//  TPLMapView *myObject = [[TPLMapView  alloc] init];
//  [myObject animateToZoom:(15.F)];
  
  


  
  return  mapView;
  
  
}


- (void)mapView:(nonnull TPLMapView *)_mapView
    regionDidChangeAnimated:(BOOL)animated{

        
  CLLocationDegrees latitude = _mapView.cameraPosition.location.latitude;
  NSString *lat = [NSString stringWithFormat:@"%f", latitude];
  
  CLLocationDegrees longi = _mapView.cameraPosition.location.longitude;
  NSString *longg = [NSString stringWithFormat:@"%f", longi];
  
  
  NSString *latlng = [lat stringByAppendingString:[NSString stringWithFormat:@";%@", longg]];

    CustomEventEmitter *yourModule = [CustomEventEmitter allocWithZone:nil];
       [yourModule emitPushDeliveredEvent:latlng];
       


    }


//RCT_EXPORT_METHOD(sendStringToRN:(NSString *)string) {
//    [self.eventEmitter sendStringToRN:string];
//}

@end


@implementation MyObjectiveCClass


  

// WORKING
- (void)myMethod {
  [mapView animateToZoom:(10.F)];
}


// WORKING
- (void)AddMarker :(NSString *)str {

  
  NSArray *components = [str componentsSeparatedByString:@";"];

  double lat = [components[1] doubleValue];
  
  double longg = [components[0] doubleValue];

  CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:lat longitude:longg];


  //Initilize Marker with set of coordinate
  Marker * annotationPin = [Marker markerWithPosition:newLoc.coordinate];
  
  [mapView addMarker:(annotationPin)];

}


// WORKING
- (void)AddCustomMarker :(NSString *)str {

  NSArray *components = [str componentsSeparatedByString:@";"];

  double lat = [components[1] doubleValue];
  
  double longg = [components[0] doubleValue];

  CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:lat longitude:longg];


  CLLocationCoordinate2D corrd = CLLocationCoordinate2DMake(newLoc.coordinate.latitude, newLoc.coordinate.longitude);

  Marker *markerItems = [Marker markerWithPosition:(corrd)];

  if (components[2] == @"rider") {
    markerItems.icon = [UIImage imageNamed:@"rider.png"];
    [mapView addMarker:(markerItems)];
  }else{
    markerItems.icon = [UIImage imageNamed:@"other.png"];
    [mapView addMarker:(markerItems)];
  }
}


// WORKING
- (void)AddCustomMarkerW :(NSString *)str base64String:(NSString *)base64String {
    
    NSURL *url = [NSURL URLWithString:base64String];

        NSData *imageData = [NSData dataWithContentsOfURL:url];

      NSArray *components = [str componentsSeparatedByString:@";"];

      double lat = [components[1] doubleValue];

      double longg = [components[0] doubleValue];
    
    double rot = [components[2] doubleValue];


      CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:lat longitude:longg];


      CLLocationCoordinate2D corrd = CLLocationCoordinate2DMake(newLoc.coordinate.latitude, newLoc.coordinate.longitude);

        Marker *marker = [Marker markerWithPosition:corrd];

        marker.icon = [UIImage imageWithData:imageData];

    marker.rotation = rot;
    
    
    NSString *firstLine = components[3];
    NSString *secondLine = components[4];
    
    NSString *concatenatedString;
    
    if (secondLine && secondLine.length > 0) {
        concatenatedString = [NSString stringWithFormat:@"%@\n%@", firstLine, secondLine];
    } else {
        concatenatedString = firstLine;
    }
    
   
       
    marker.title = concatenatedString;
   
   

    marker.canShowCallout = true;
   

        [mapView addMarker:(marker)];
}







// WORKING
- (void) SetCameraAnim:(NSString *)latLng {


   NSArray *components = [latLng componentsSeparatedByString:@";"];

   if (components.count > 0)
   {

       double lat = [components[1] doubleValue];

        double longg = [components[0] doubleValue];

        CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:lat longitude:longg];


        //Initilize Marker with set of coordinate
        Marker * annotationPin = [Marker markerWithPosition:newLoc.coordinate];


     
       // float _zoomInFloat = [_zoom floatValue];

        CameraPosition * position = [CameraPosition cameraPositionWithLocation:annotationPin.getPosition zoom:15.0];

        [mapView setCameraPosition:position];

   }

}


// WORKING
// WORKING
- (void)passStringArray:(NSArray *)nsArray colorr:(NSString *)colorr widthh:(NSString *)widthh{
  
    NSMutableArray *locationsArray = [NSMutableArray array];
 
  MutableCoordinateGeometry * geometry = [[MutableCoordinateGeometry alloc] init];

  
  for (int i = 0; i < nsArray.count; i++) {
    
    
   
    NSArray *components = [nsArray[i] componentsSeparatedByString:@";"];
    
    double lat = [components[1] doubleValue];
    
    double longg = [components[0] doubleValue];
      
      CLLocationDegrees latitude = lat;
      CLLocationDegrees longitude = longg;
      
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    [geometry addCoordinate:location.coordinate];
  }
 

    PolyLine * _polyline = [PolyLine polylineWithCoordinates:geometry];


    int wid = [widthh intValue];
  _polyline.outlineWidth = wid;


  _polyline.transparent = false;




  NSString *hexColor = colorr; // Replace with your desired hex color
  unsigned int rgbValue = 0;
  NSScanner *scanner = [NSScanner scannerWithString:hexColor];
  [scanner setScanLocation:1]; // Skip the '#' character
  [scanner scanHexInt:&rgbValue];

  // Create a UIColor object using the RGB values
  UIColor *color = [UIColor colorWithRed:((rgbValue >> 16) & 0xFF)/255.0
                                   green:((rgbValue >> 8) & 0xFF)/255.0
                                    blue:(rgbValue & 0xFF)/255.0
                                   alpha:1.0];

  _polyline.outlineColor = color;

    //The UIColor used to fill the shape’s path

  _polyline.fillColor = color;

    

    //Method to render polyline on map

    [mapView drawPolyLine:_polyline];
    
}



// NOT WORKING
- (void)removeAllPolyLines {

  mapView.removeAllPolylines;
}


// NOT WORKING
- (void)removeAllMarkers{

  mapView.removeAllMarkers;

}




@end

