// noPage
import esp from 'esoftplay/esp';

import { LibCurl } from 'esoftplay/cache/lib/curl/import';
import { LibToastProperty } from 'esoftplay/cache/lib/toast/import';

import * as Location from 'expo-location';
import { Alert, Platform } from 'react-native';

export default class m {
  constructor() {
    if (Platform.OS === 'android' && __DEV__) {
      Alert.alert(esp.lang("market/gps", "alert_emulator"))
    }
  }

  getDirectionDistance(latlong1: string, latlong2: string): Promise<number> {
    return new Promise((r) => {
      const url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + latlong1 + "&destination=" + latlong2 + "&mode=DRIVING&sensor=false&key=" + esp.config('firebase', 'apiKey')
      new LibCurl().custom(url, null, (res) => {
        let distance = 1000
        distance = res?.routes[0]?.legs[0]?.distance?.value || 1000
        r((distance / 1000) || 1)
      })
    })
  }

  getLocationAsync(): Promise<any> {
    return new Promise(async (r, e) => {
      let { status } = await Location.requestForegroundPermissionsAsync()
      // let { status } = await Permissions.askAsync(Permissions.LOCATION);
      if (status !== 'granted') {
        // Alert.alert('Oops","Permission to access location was denied')
        return
      }

      try {
        let location = await Location.getCurrentPositionAsync({
          accuracy: Location.Accuracy.Highest
        });
        r(location)
      } catch (error) {
        e(error)
      }

    })
  };

  getAutoComplete(query: string): Promise<any> {
    let sessiontoken = Math.random()
    let uri = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?key=' + esp.config('firebase', 'apiKey') + '&sessiontoken=' + sessiontoken + '&input=' + encodeURI(query)
    return new Promise((r) => {
      new LibCurl().custom(uri, null, (res) => {
        r(res)
      })
    })
  }

  getPlaceDetail(place_id: string): Promise<any> {
    const uri = 'https://maps.googleapis.com/maps/api/place/details/json?key=' + esp.config('firebase', 'apiKey') + '&place_id=' + place_id + '&fields=geometry'
    return new Promise((r) => {
      new LibCurl().custom(uri, null, res => {
        const loc = res?.result?.geometry?.location
        if (loc?.lat && loc?.lng)
          r(loc.lat + ',' + loc.lng)
        else
          LibToastProperty.show(esp.lang("market/gps", "alert_try_again"))
      })
    })
  }

  geocoding(locationName: string): Promise<any> {
    return new Promise((r) => {
      new LibCurl().custom('https://maps.googleapis.com/maps/api/geocode/json?address=' + locationName + '&key=' + esp.config('firebase', 'apiKey'), null, (res) => {
        let latlng = res && res.results && res.results.length > 0 && res.results[0].geometry.location || null
        r(latlng && latlng.lat + ',' + latlng.lng)
      })
    })
  }

  reverseGeocoding(latlong: string): Promise<any> {
    return new Promise((r) => {
      new LibCurl().custom('https://maps.googleapis.com/maps/api/geocode/json?address=' + latlong + '&key=' + esp.config('firebase', 'apiKey'), null, (res) => {
        //         // new LibCurl().custom('https://locationiq.com/v1/reverse_sandbox.php?format=json&lat=' + latlong.split(',')[0] + '&lon=' + latlong.split(',')[1] + '&accept-language=en', null, (res) => {
        r(res?.results[0]?.formatted_address || esp.lang("market/gps", "select_point"))
      }, 1)
    })
  }
}