import { Component, OnInit, Inject } from '@angular/core';
import { MdDialogRef, MD_DIALOG_DATA } from '@angular/material';
import { ContactService } from '../../contact.service';
declare var google:any;

@Component({
  selector: 'app-manual-address',
  templateUrl: './manual-address.component.html',
  styleUrls: ['./manual-address.component.scss']
})
export class ManualAddressComponent implements OnInit {
  errFlag : boolean =false;
  manualAddress:any='';
  useridd: any;
  addressSuggested: any;
  addLat: any;
  addlng: any;
  long: any;
  lat: any;
  mapLink: any;
  constructor(public dialogRef: MdDialogRef<ManualAddressComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService) {
      console.log(this.data);
     }

  ngOnInit() {
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
   this.manualAddress =  this.data.locationAddress ;
    console.log('this.manualAddress',this.manualAddress);
    this.lat = this.data.last_location.lat;
    this.long = this.data.last_location.long;
    this.mapLink = "https://maps.google.com/?q=" + this.lat + ","+ this.long;

    var that = this;
    var input = document.getElementById('txtSource');
    var searchBox = new google.maps.places.SearchBox(input);

    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(this.lat, this.long);

    var request = {
      latLng: latlng
    };
    // console.log('latlongVal', request)
    geocoder.geocode(request, function (data, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (data[0] != null) {
          that.addressSuggested = data[0].formatted_address;
          // console.log(that.liveAdd);
        } else {
          // console.log("No address available")
          that.addressSuggested = "";
        }
      }
      else {
        that.addressSuggested = '';
      }

    })
  
    searchBox.addListener('places_changed', function() {
      var places = searchBox.getPlaces();
      if (places.length == 0) {
        return;
      }
      console.log(places);
      places.forEach(function(place:any) {
     //   console.log(place.geometry.location);
        that.addressSuggested = place.formatted_address;
        that.addLat =  place.geometry.location.lat();
        that.addlng = place.geometry.location.lng();
        console.log(that.addLat,that.addlng);
        if (!place.geometry) {
          return;
        }
      });
    });
  }

  addManualAddress(){
    console.log(this.data);
    console.log('update Address clicked');
    var payload ={
        lat : this.data.last_location.lat,
        long: this.data.last_location.long,
        address: this.addressSuggested,
        current_address : this.manualAddress,
        status : 'Pending',
        updatedBy : this.useridd
    }
    console.log('payload',payload);

    this.contactService.updateAddress(payload).subscribe(res=>{
      console.log(res);
      this.dialogRef.close('success');
    },err=>{
      console.log(err);
    })
  }

  close(){
    this.dialogRef.close('cancel');
  }
}
