import { bool } from 'prop-types';
import { Image, NativeModules } from 'react-native';
const { MyModule } = NativeModules;
const { CustomMethods } = NativeModules

import { Platform } from 'react-native';




export function AddMarker(lnglat: String) {
  if (Platform.OS === 'ios') {
    CustomMethods.AddMarker(lnglat)
  } else {
    MyModule.AddMarker(lnglat)
  }

}

export function RemoveAllMarkers() {
  if (Platform.OS === 'ios') {
    CustomMethods.removeAllMarkers()
  } else {
    MyModule.RemoveAllMarkers()
  }
}

export function RemoveAllPolyLines() {
  if (Platform.OS === 'ios') {
    CustomMethods.removeAllPolyLines()
  } else {
    MyModule.RemoveAllPolyLines()
  }
}




export function AddCustomMarker(lngLatWithImageName: String) {
  if (Platform.OS === 'ios') {
    CustomMethods.AddMarker(lngLatWithImageName)
  } else {
    MyModule.AddCustomMarker(lngLatWithImageName)
  }

}

export function AddCustomMarkerWithBase64(lngLatWithImageName: String, base64Str: String) {
  if (Platform.OS === 'ios') {
     CustomMethods.AddCustomMarkerW(lngLatWithImageName , base64Str)
  } else {
    MyModule.AddCustomMarkerWithImage(lngLatWithImageName , base64Str)
  }

}

export function SetCameraAnimation(lnglat: String, zoom: String) {
  if (Platform.OS === 'ios') {
     CustomMethods.SetCameraAnim(lnglat)
  } else {
    MyModule.SetCameraAnimation(lnglat, zoom)
  }

}

export function SetPolyLine(CurrentLatLng: String, destinationLatLng: String[] , color: String, width: String, apiKey: String) {
  // const stringArray = ['24.820288;67.124412', '24.821821;67.124654', '24.828937;67.122995' , '24.838133;67.120794' , '24.835834;67.105957' 
  // , '24.834383;67.100016' , '24.839994;67.098116' , '24.866864;67.083189'];

  var tasks = [];
  for (var i = 0; i < destinationLatLng.length; i++) {
    var lat = destinationLatLng[i].split(";")[0];
    var lng = destinationLatLng[i].split(";")[1];

    tasks.push({ "lat": lat, "lng": lng, id: i + 1 });

  }

  var rider_current_location = {

    "lat": CurrentLatLng.split(";")[0],
    "lng": CurrentLatLng.split(";")[1],
    "id": 1

  };



  //console.log(d)
  const url = `https://api1.tplmaps.com:8888/route/optimal?isRouteGeometry=true&isRoundTrip=false&elementCount=${tasks.length}&apikey=${apiKey}`;


  fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      "rider_current_location": rider_current_location,
      "tasks": tasks
    }),
  })
    .then((response) => response.json())
    .then((responseData) => {
    
      const route = responseData.route;
      
      const latLngArray = route.map((coord: String) => `${coord[0]};${coord[1]}`);
      const readableArray: any[] = latLngArray;

      if (Platform.OS === 'ios') {

        CustomMethods.passStringArray(readableArray , color , width);
      }else{
        MyModule.passStringArray(readableArray , color , width);
      }
    })
    .catch((error) => {
      console.log(error)
    });


  
}

export function CameraCallback() {
  MyModule.CameraCallback('Register callback')
    .then((result: any) => {
      console.warn(result)
    })
    .catch((error: any) => {
      console.warn(error)
    });

}