import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { Loader } from "@googlemaps/js-api-loader"



@Injectable({
  providedIn: 'root'
})
export class BuscarUnidadeService {
  private apiKey = 'https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJrTLr-GyuEmsRBfy61i59si0&fields=address_components&key=AIzaSyBTkf2qgAplsW7BDcsWxr7n01cfGrdhP0A';

  private loader = new Loader({
    apiKey: "AIzaSyBTkf2qgAplsW7BDcsWxr7n01cfGrdhP0A",
    version: "weekly"
  });
  private map!:google.maps.Map;
  private teste!: google.maps.PlacesLibrary;
  
  // initMap() {
  //   var sydney = new google.maps.LatLng(-33.867, 151.195);
  
  //   const infowindow = new google.maps.InfoWindow();
  
    // const map = new google.maps.Map();
  
    // var request = {
    //   query: 'hospital em itapeva',
    //   fields: ['name', 'geometry'],
    // };
  
  //   var service = new google.maps.places.PlacesService(this.map);
  
  //   service.findPlaceFromQuery(request, function(results, status) {
  //     if (status === google.maps.places.PlacesServiceStatus.OK) {
  //       console.log(results)
  //     }
  //   });
  // }


  

  constructor(private http: HttpClient) {
/*this.loader.load().then(async () => {
    const { Map } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary;
    this.map = new Map(document.getElementById("map") as HTMLElement, {
      center: { lat: -34.397, lng: 150.644 },
      zoom: 8,
    });
  });*/
  // this.initMap()

  }

  getUnidadesDeSaudePlaces(cidade: string): Observable<any> {
    const query = `hospital${cidade}`;
    const url = `${this.apiKey}&query=${encodeURIComponent(query)}`;


    console.log(url)
    return this.http.get<any>(url);
  }
}

  // código google maps -----------------------------------------------------------------------------------------------------------------
  // private map: google.maps.Map | undefined;
  // private service: google.maps.places.PlacesService | undefined;
  // private infowindow: google.maps.InfoWindow | undefined;

  // apiLoaded: Observable<boolean>;
  // key = 'AIzaSyBTkf2qgAplsW7BDcsWxr7n01cfGrdhP0A';
  
  // constructor(
  //   http: HttpClient
  // ) {
  //   this.apiLoaded = http.jsonp(`https://places.googleapis.com/v1/places/GyuEmsRBfy61i59si0?fields=addressComponents&key=${this.key}`, 'callback')
  //     .pipe(
  //       map(() => true),
  //       catchError(() => of(false)),
  //     );
  // }

  // initializeMap(): void {
  //   const mapElement = document.getElementById('map');

  //   if (mapElement) {
  //     const pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);

  //     this.map = new google.maps.Map(mapElement, {
  //       center: pyrmont,
  //       zoom: 15
  //     });

  //     this.infowindow = new google.maps.InfoWindow();

  //     const request: google.maps.places.PlaceSearchRequest = {
  //       location: pyrmont,
  //       radius: 500,
  //       type: 'hospital'
  //     };

  //     this.service = new google.maps.places.PlacesService(this.map!);
  //     this.service.nearbySearch(request, this.callback.bind(this));
  //   } else {
  //     console.error('Element with ID "map" not found.');
  //   }
  // }

  // buscarUnidades(): void {
  //   const pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);

  //   const request: google.maps.places.PlaceSearchRequest = {
  //     location: pyrmont,
  //     radius: 500,
  //     type: 'hospital'
  //   };

  //   if (this.service) {
  //     this.service.nearbySearch(request, this.callback.bind(this));
  //   }
  // }

  // private createMarker(place: google.maps.places.PlaceResult): void {
  //   if (this.map) {
  //     const marker = new google.maps.Marker({
  //       map: this.map,
  //       position: place.geometry?.location!,
  //       title: place.name
  //     });

  //     marker.addListener('click', () => {
  //       if (this.infowindow) {
  //         this.infowindow.setContent(place.name || '');
  //         this.infowindow.open(this.map!, marker);
  //       }
  //     });
  //   }
  // }

  // private callback(
  //   results: google.maps.places.PlaceResult[] | null,
  //   status: google.maps.places.PlacesServiceStatus,
  //   pagination: google.maps.places.PlaceSearchPagination | null
  // ): void {
  //   if (status === google.maps.places.PlacesServiceStatus.OK && results) {
  //     for (let i = 0; i < results.length; i++) {
  //       const place = results[i];
  //       this.createMarker(results[i]);
  //     }
  //   }
  // }

