import { environment } from './../environments/environment';
import { Injectable } from '@angular/core';
import { ContactService } from './contact.service';
import * as io from 'socket.io-client';
import { Params,Router, ActivatedRoute, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import{Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';


@Injectable()
export class DatainjectionService {
  devicess:any;
  final:any=null;
  userID:any;
  fs:any;
  ls:any;
  or:any;
  emailid:any;
  useridd:any;
  custtype:any;
  cust:boolean = false;

   dev_url = environment.hostUrl;
  
  //dev_url = 'http://localhost:3000';
  // socketUrl= environment.socket5000;

  mb: any;
  socket_gps: any; socket_5000: any; socket_notifIO: any;
  getSocket_gps(){
    return this.socket_gps;
  }
  getSocket_5000(){
    return this.socket_5000
  }
  getSocket_notifIO(){
    return this.socket_notifIO;
  }
  constructor(private contactService: ContactService, private activatedRoute: ActivatedRoute, private http: Http,private router:Router) {
    // alert('ata injection constructor')
    this.socket_gps = io.connect('https://soc.oneqlik.in' +'/gps', {
      secure: true, rejectUnauthorized: false, transports: ["websocket", "polling"], upgrade: false
    });
    
    this.socket_5000 = io.connect('https://soc.oneqlik.in', {
      secure: true, rejectUnauthorized: false, transports: ["websocket", "polling"], upgrade: false
    });

    this.socket_notifIO = io.connect('https://soc.oneqlik.in'+'/notifIO', {
      secure: true, rejectUnauthorized: false, transports: ["websocket", "polling"], upgrade: false
    });

    // this.socket_gps = io.connect('https://socket.oneqlik.in' +'/gps', {
    //   secure: true, rejectUnauthorized: false, transports: ["websocket", "polling"], upgrade: false
    // });
    
    // this.socket_5000 = io.connect('https://socket.oneqlik.in', {
    //   secure: true, rejectUnauthorized: false, transports: ["websocket", "polling"], upgrade: false
    // });

    // this.socket_notifIO = io.connect('https://socket.oneqlik.in'+'/notifIO', {
    //   secure: true, rejectUnauthorized: false, transports: ["websocket", "polling"], upgrade: false
    // });
    
    // https://server2.oneqlik.in/
    if(window.localStorage.custumer_token){
      this.fs = JSON.parse(window.atob(window.localStorage.custumer_token.split(".")[1])).fn;
      this.ls = JSON.parse(window.atob(window.localStorage.custumer_token.split(".")[1])).ln;
      this.emailid = JSON.parse(window.atob(window.localStorage.custumer_token.split(".")[1])).email;
      this.or = JSON.parse(window.atob(window.localStorage.custumer_token.split(".")[1]))._orgName;
      this.useridd = JSON.parse(window.atob(window.localStorage.custumer_token.split('.')[1]))._id;
      this.mb =  JSON.parse(window.atob(window.localStorage.custumer_token.split('.')[1])).phn;
      this.custtype =  JSON.parse(window.atob(window.localStorage.custumer_token.split('.')[1])).isDealer;
      if(this.custtype == true){
        this.cust = true;
      }  
      if(this.mb.charAt(0)=="n"){
        this.mb = ' '
      }
    }
    else{
      console.log(router.url);
      var url=(router.url).substring(0,12);
      console.log(url);
      
      if(url=="/ViewVehicle"){

      }else{
        this.fs = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).fn;
        this.ls = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).ln;
        this.emailid = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).email;
        this.or = JSON.parse(window.atob(window.localStorage.token.split(".")[1]))._orgName;
        this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
        this.mb =  JSON.parse(window.atob(window.localStorage.token.split('.')[1])).phn;
        this.custtype =  JSON.parse(window.atob(window.localStorage.token.split('.')[1])).isDealer;
        if(this.custtype == true){
          this.cust = true;
        }  
        if(this.mb.charAt(0)=="n"){
          this.mb = ' '
        }

      }
      
    }

  }
 
  getDevice(emailid,id) {
    return this.http.get(this.dev_url + "/devices/getDeviceByUser?email="+emailid+'&id='+id)
        .map(data => {
            data.json();
           

             var devices = data.json().devices;
         

            /* console.log("Devices: ",temp);  */
            return data.json();
    });
  }
  getData(e,u,g,s,l,input,supadm,dealer){ 
   
    if(input && input!='undefined'){
      s=0;
    }
    var url = this.dev_url + "/devices/getDeviceByUser?email="+e+'&id='+u+'&skip='+s+"&limit="+l;
    if(input && input!="undefined"){
      url+= "&search="+input;
    }
    console.log(url)
    if(supadm)
  url+= '&supAdmin=' + supadm;
  if(dealer)
  url+= '&dealer=' + dealer;
    if(g)
    url += "&group=" + g;
    return this.http.get(url)
    .map(data => {
    data.json();
    
    var devices = data.json().devices;
    /* console.log("Devices: ",temp); */
    return data.json();
    });
    
    }
  getGroupData(e,u){
      return this.http.get(this.dev_url + "/devices/getVehiclesUnderGroup?userid="+u)
        .map(data => {

          data.json();
            return data.json();
    });
  
  }

}
