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-addpoi-bylocation',
  templateUrl: './addpoi-bylocation.component.html',
  styleUrls: ['./addpoi-bylocation.component.scss']
})
export class AddpoiBylocationComponent implements OnInit {
  poiName:any;
  address:any;
  useridd: any;
  Saddress: any;
  lattitude:any;
  longitude:any;
  poitype:any;
  poiType=[
    {view : "Provider",value:""},
     {view : "Customer",value:"customer"},
      {view : "Garage",value:"garage"}, 
      {view : "Emergency",value:"emergency"},
       {view : "Others",value:"others"}, 
       {view : "Gas Station",value:"gas_station"},
       {view :"Residence",value : "residence"},
       {view : "City",value:"city"},
       {view : "Work",value:"work"},
        {view : "Resturant",value:"resturant"}, 
        {view : "Shopping",value:"shopping"},
         {view : "Bank",value:"bank"}, 
         {view : "School",value:"school"},
         {view :"Super Market",value : "supermarket"},
         {view :"Gym",value : "gym"},
         {view : "Home", value : "home"},
         {view :"Grocery Store",value : "grocery_store"}
  ];

  constructor(public dialogRef: MdDialogRef<AddpoiBylocationComponent>,@Inject(MD_DIALOG_DATA) public data: any,public contactService: ContactService) {
    // console.log("deviceInfo=>",data);
    this.lattitude = data.deviceData.lat;
    this.longitude = data.deviceData.long;
    console.log('this.lattitude',this.lattitude);
    console.log('this.longitude',this.longitude);

    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
   }

  ngOnInit() {
    // this.Saddress = new google.maps.places.Autocomplete(document.getElementById('txtSource'));
    this.getAddress();

  }

 

  saveDetails(){
    // console.log(this.address_show);
    var locationObj = {
      'type' : 'Point',
      "coordinates": [this.longitude,this.lattitude]
    }
    // this.Saddress = document.getElementById('txtSource');
    // this.address =this.Saddress.value;
  var poiObj = {
    poi:  [
      {  
        "location": locationObj,
        "poiname" : this.poiName,       
        "status": "Active",
        "address" : this.address_show,
        "user" : this.useridd,
        "radius" : 30
      }
    ]
  }

  if(this.poitype != undefined){
    poiObj.poi[0]['poi_type'] = this.poitype ; 
  }

    this.contactService.addOnePoi(poiObj)
      .subscribe(res=>{
        // console.log(res);
      },err=>{
        // console.log(err);
      })
    // console.log(poiObj);
    this.dialogRef.close("success");
  }

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

  address_show:any;
  getAddress() {

    var outerThis = this;

    let d_lat = (this.lattitude != undefined)?this.lattitude : null;
  let d_lng = (this.longitude != undefined)?this.longitude : null;


  var latlng_1 =
  {
    "lat": d_lat,
    "long":  d_lng
  }
  this.contactService.getAddressByApi(latlng_1).subscribe(res=>{   
  
    if(res.message == "Address not found in databse"){
      let geocoder = new google.maps.Geocoder();
      let t_latlng = new google.maps.LatLng(d_lat, d_lng);

      let request = {
        latLng: t_latlng
      };
      geocoder.geocode(request, function (data, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (data[0] != null) {
            outerThis.address_show = data[0].formatted_address;
            // console.log(outerThis.address_show);
         
          } else {
            // console.log("No address available")
            outerThis.address_show = "No address available";
          }
        }
        else {

          outerThis.address_show = 'NA';


        }

      })
    }else{
     
      outerThis.address_show = res.address;
    }
  })





    // if (this.lattitude== undefined) {
    //   this.lattitude = null;
    //   this.longitude = null;
    // } 
    // var geocoder = new google.maps.Geocoder();
    // var latlng = new google.maps.LatLng(this.lattitude, this.longitude);

    // var request = {
    //   latLng: latlng
    // };



    // geocoder.geocode(request, function (data, status) {
    //   if (status == google.maps.GeocoderStatus.OK) {
    //     if (data[0] != null) {
    //       outerThis.address_show = data[0].formatted_address;
       

    //     } else {

    //       outerThis.address_show = "No address available";
    //     }
    //   }
    //   else {

    //     outerThis.address_show = outerThis.lattitude + ", " + outerThis.longitude;
     

    //   }

    // })


  }

  
}



  