import { Injectable, NgZone } from '@angular/core';
import { BackgroundGeolocation } from '@ionic-native/background-geolocation';
import { Geolocation, Geoposition } from '@ionic-native/geolocation';
import 'rxjs/add/operator/filter';
import { Platform } from 'ionic-angular';

@Injectable()
export class TrackingProvider {
  public watch: any;   
  public lat: number = 0;
  public lng: number = 0;
  public lugar:any;
  public id: any; 
  public trackingInProgress: boolean = false;
  constructor(public zone: NgZone,
    public platform: Platform,
    public backgroundGeolocation:BackgroundGeolocation,
    public geolocation: Geolocation
  ) {
  }


  queryInProgress() {
    return this.trackingInProgress;
  }
  startTracking() {
    if (this.trackingInProgress == false) {
      console.log('starting tracking, this is first time');
      let config = {
        desiredAccuracy: 0,
        stationaryRadius: 20,
        distanceFilter: 10,
        debug: false,
        interval: 10000,
        pauseLocationUpdates:false
      };
      //Corriendo en segundo plano
      this.backgroundGeolocation.configure(config).subscribe((location) => {
  
        console.log('Ubicacion:  ' + location.latitude + ',' + location.longitude);
        
        this.zone.run(() => {
          this.lat = location.latitude;
          this.lng = location.longitude;
        });
      }, (err) => {
  
        let options = {
          frequency: 10000,
          enableHighAccuracy: true
        };
        this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => {
    
          /*console.log(position);*/
          this.zone.run(() => {
            this.lat = position.coords.latitude;
            this.lng = position.coords.longitude;
          });
        });
  
      });
      this.backgroundGeolocation.start();
  
  
      //Corriendo en primer plano (aplicacion abierta)
      let options = {
        frequency: 10000,
        enableHighAccuracy: true
      };
      this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => {
  
        /*console.log(position);*/
        this.zone.run(() => {
          this.lat = position.coords.latitude;
          this.lng = position.coords.longitude;
        });
      });
  
    }
    this.trackingInProgress = true;
  }

  stopTracking() {
    console.log('stopTracking');
    this.backgroundGeolocation.finish();
    this.watch.unsubscribe();
   
  }
  UbicacionLat(){
    if (!this.platform.is('cordova')) {
      this.geolocation.getCurrentPosition().then((ok) => {
        this.lat = ok.coords.latitude;
        this.lng = ok.coords.longitude;
        //console.log('Ubicacion por Ubicación Lat', ok.coords);
      }, (err) => {
        console.log('no cordova geo err');
      });
    }
    return this.lat;
  }
  UbicacionLng(){
    if (!this.platform.is('cordova')) {
      this.geolocation.getCurrentPosition().then((ok) => {
        this.lat = ok.coords.latitude;
        this.lng = ok.coords.longitude;
        /*console.log('Ubicacion por Ubicación Lng', ok.coords);*/
      }, (err) => {
        /*console.log('no cordova geo err');*/
      });
    }
    return this.lng;
  }

}
