import {MdDialog, MdDialogRef, MD_DIALOG_DATA} from '@angular/material';
import {Device} from '../../device';
import {ContactService} from '../../contact.service';
import { Component, OnInit,Inject } from '@angular/core';
import { stringify } from '@angular/core/src/util';

@Component({
  selector: 'app-share-loc',
  templateUrl: './share-loc.component.html',
  styleUrls: ['./share-loc.component.scss'],
  providers: [ContactService]

})
export class ShareLocComponent implements OnInit {
  Loading:boolean = false;
  deviceObject: any;
  phone_number:number;
  errFlag:boolean= false;
  errmsg:string='';
  useridd: any;
  email_add:any;  
  link: string;

  constructor(public dialogRef: MdDialogRef<ShareLocComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService) { 
    this.deviceObject =  this.data.deviceObject ;
    console.log(this.deviceObject);
  
    }

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

    time_share = 1; 
    changeTime(id){
      console.log("Id",id);
      this.time_share = parseInt(id);


    }
    btnFlag:boolean=true;
    locationShare(){
      var that = this;
      if((this.phone_number == undefined)&&(this.email_add == undefined)){   
        that.errFlag = true ; 
        that.errmsg = 'Please Provide Phone number or email address ';
        let Error = setTimeout(function(){
          that.errFlag = false ; 
          that.errmsg = '';
          clearInterval(Error);
        },2000)
        return ; 
      }else{
        console.log('this.deviceObject',this.deviceObject);
        var ttl=0
        if(this.time_share == 15){
          this.time_share = 15; 
           ttl = this.time_share*60
        }else{
          ttl = this.time_share*3600
        }
        var data = {
          id: this.deviceObject._id,
          imei: this.deviceObject.Device_ID,
          sh: this.deviceObject.user,
          ttl: ttl
          };

          this.contactService.shareLiveLocation(data).subscribe(res=>{
            console.log(res); 
            this.btnFlag=false;
            console.log('shareToken',res);
            this.link = 'https://www.oneqlik.in' + "/share/liveShare?t=" + res.t;
            console.log(this.link);
            // this.shareLocationToDevice(link);
          },err=>{
            console.log(err);
          })
      }
    }

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

    shareLocationToDevice(){
      // console.log('link',link);
      var userInfo = JSON.parse(window.atob(window.localStorage.token.split('.')[1]));
      console.log(userInfo);
      var userName = userInfo.fn?userInfo.fn:'' + ' ' + userInfo.ln?userInfo.ln:'';
      var data={
        link : this.link,
        device : this.deviceObject.Device_ID , 
        user : this.deviceObject.user,
        userName :userName
      };
      if((this.phone_number != undefined)||(this.phone_number != null)){
        data['phone_num']= this.phone_number ; 
      }
      if((this.email_add != undefined)||(this.email_add !='')){
        data['email_add'] = this.email_add ; 

      }
      this.contactService.sendLiveLocationLink(data).subscribe(res=>{
        console.log(res);
        this.btnFlag=true;
        this.dialogRef.close('succ');
      },err=>{
        console.log(err);

      })
    }
  
   
copytoClipboard(){
  var text = this.link;
 const selBox = document.createElement('textarea');
 selBox.style.position = 'fixed';
 selBox.style.left = '0';
 selBox.style.top = '0';
 selBox.style.opacity = '0';
 selBox.value = text;
 document.body.appendChild(selBox);
 selBox.focus();
 selBox.select();
 document.execCommand('copy');
 document.body.removeChild(selBox);
 var that = this
 this.errFlag = true ; 
 this.errmsg = 'Link copied to clipboard';
 let Error = setTimeout(function(){
   that.errFlag = false ; 
   that.errmsg = '';
   clearInterval(Error);
 },5000)
 
 }
}
