import { Component, NgZone } from '@angular/core';
import { IonicPage, NavController, NavParams, Platform } from 'ionic-angular';
import { dataService } from "../../providers/common.service";
import { Myservice } from "../../providers/myservice";
import { Storage } from "@ionic/storage";
import { user } from "../../models/mymodel";

declare var google;
@IonicPage()
@Component({
  selector: 'page-ridedetail',
  templateUrl: 'ridedetail.html',
})
export class RidedetailPage {
  book: any;
  geocoder: any;
  loc = { from: '', to: '', fromAd: '', toAd: '' }
  local: user
  public unregisterBackButtonAction: any;

  constructor(public navCtrl: NavController, public navParams: NavParams, private data: dataService, private zone: NgZone, private myservice: Myservice, private storage: Storage, public platform: Platform) {
  }

  ionViewDidEnter() {
    // this.myservice.show_loader()
    this.initializeBackButtonCustomHandler();
    this.storage.get('user_data').then(data => {
      if (data) {
        this.local = data
        this.book = this.data.getbookingData()
        console.log(this.book)
        var bdata = { 'book_date': this.book.date, 'drop_area': this.book.dropArea, 'pickup_area': this.book.pickArea, 'taxi_type': this.book.carDetails.car_type, 'amount': '', 'km': '', 'promocode': this.book.promocode, 'token': this.local.token, 'userid': this.local.id, 'type':'now' }
        this.myservice.load_post(bdata, 'book_cab').subscribe(response => {
          this.myservice.hide_loader()
          if (response.status == 'success') {
            this.myservice.load_post({},'checkBookingStatus')
          }
          else {
            this.myservice.show_alert('Failed', 'Please try again')
            this.navCtrl.pop()
          }
        })

        this.geocoder = new google.maps.Geocoder();
        for (let i = 0; i < 2; i++) {
          if (i == 0) {
            var lat = this.book.pickLat
            var lng = this.book.pickLng
          }
          else {
            var lat = this.book.dropLat
            var lng = this.book.dropLng
          }
          var latlng = new google.maps.LatLng(lat, lng);
          var This = this;
          This.geocoder.geocode({ 'latLng': latlng }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              if (results[0]) {
                if (i == 0) {
                  This.loc.from = results[0].address_components[0].short_name
                  This.loc.fromAd = results[0].formatted_address
                }
                else {
                  This.loc.to = results[0].address_components[0].short_name
                  This.loc.toAd = results[0].formatted_address
                  This.zone.run(() => { });
                }
              }
            }
            else {
              console.log("Geocoder failed due to: " + status);
            }
          });
        }
      }
    })
  }

  public initializeBackButtonCustomHandler(): void {
    this.unregisterBackButtonAction = this.platform.registerBackButtonAction(() => {
      // this.customHandleBackButton();
    }, 10);
  }

  ionViewWillLeave() {
    this.unregisterBackButtonAction && this.unregisterBackButtonAction();
  }
}
