
import { Component, OnInit, Input } from '@angular/core';
import { DatainjectionService } from '../datainjection.service';
import { Router } from '@angular/router';
import * as io from 'socket.io-client';
import * as jquery from 'jquery';
import { Observable } from 'rxjs/Rx';
import { ContactService } from '../contact.service';



@Component({
  selector: 'app-sidemenu-fuel',
  templateUrl: './sidemenu-fuel.component.html',
  styleUrls: ['./sidemenu-fuel.component.scss']
})


export class SidemenuFuelComponent implements OnInit {
  @Input('data') data: string;
  vehicle_IMEI: any;
  vehicleDetails: any;
  total_final: any;
  mb: any 
  checker: any;
  socket_Ing: any;
  speed_data: any;
  vehicle_id: any;
  mapName: any;
  timer: any = Observable.timer(100, 100);
  add_dev: boolean;
  subscription: any;
  try: any
  devicess: any;
  final: any;
  userID: any;
  fs: any;
  ls: any;
  or: any;
  emailid: any;
  useridd: any;
  custtype: any;
  cust: boolean = false;
  deviceIgnReport:any;
  Load: boolean = false;
  ACImg: string='../../assets/image/acGrey.png';
  aStatus: string;
  pwrURL:string = "../../assets/image/noPower.png";
  pStatus :string;

  gpsImg:string = "../../assets/image/no_gps.png";
  gStatus :string;

  constructor(private injectData: DatainjectionService, private router: Router,public contaservice : ContactService) {
    // console.log(this.data)

  
    this.vehicleDetails = JSON.parse(localStorage.getItem('devDetail'));
    // console.log(this.vehicleDetails);
    this.vehicle_IMEI = this.vehicleDetails.vehicleId;
    // console.log(this.vehicle_IMEI);
    window.localStorage['GetDevData'] = 'True'
    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 = ' '
    }


    this.socket_Ing = this.injectData.getSocket_gps();
    //io.connect('https://www.oneqlik.in/gps', { secure: true, rejectUnauthorized: false, transports: ["polling"], upgrade: false });

    this.socket_Ing.on('connect', function (this) {
      // console.log('Ignition IO Connected');

    });
    this.Ing_Status();
    // this.Load = true;
    // var secretKey = 'adnateIOTtrackeR';
    this.checker = window.localStorage.checker
    // console.log(this.checker)
    // this.sidebarData();
  }

  ngOnInit() {
   
  }

  //--
  whoAmI() {
    return 'I am a child!!';
  }

  //--

getNotification(evt) {
  // console.log(evt);
        // Do something with the notification (evt) sent by the child!
    }

    refresh(){
      this.subscription = this.timer.subscribe(t => {
      this.Ing_Status();
    });
  }



  imgurl= '../../assets/image/PowerIdle.png'
  // test(a, b) {


  //   if (!a) {
  //     this.imgurl = "../../assets/image/ignition-idle.png";
  //     return true;
  //   }
  //   else if (a == "1") {
  //     this.imgurl = "../../assets/image/engine-on.png";

  //     return true;
  //   }
  //   else if (a == "0") {

  //     this.imgurl = "../../assets/image/engine-off.png"


  //     return true;
  //   }
  //   else {


  //     return false;
  //   }
  // }

  ignition_status:any;
  ing_Arr = [];
  ign_1=false;
  ign_0=false;
  ignitionStatus:any;
 
  accListener(ignStatus) {
    console.log(ignStatus);
    var outerThis= this;   

    return function (acc) {
     this.ignition_status = acc; 
     console.log(ignStatus);
     outerThis.getDeviceStatus(ignStatus);
     console.log("socketdata=>",acc);
    //  console.log("ignitionStatus=>",acc)
     if(!this.ignition_status){
      outerThis.imgurl='../../assets/image/enginena.png';
     }else if(this.ignition_status==0){
      outerThis.imgurl='../../assets/image/enginered.png';
     }else if(this.ignition_status==1){
      outerThis.imgurl='../../assets/image/enginegreen.png';
     }else{
      outerThis.imgurl='../../assets/image/enginena.png'
     }
  }
}



getDeviceStatus(did){
  console.log(did);
  this.contaservice.getdevById(did).subscribe(res=>{ 
    var deviceResponse  = JSON.parse(res['_body']);
    console.log(deviceResponse);
    var ACtrack  = deviceResponse.ac ; 
    if (!ACtrack) {
      // grey
      this.ACImg = "../../assets/image/acGrey.png";
      this.aStatus = "AC NOT SUPPORTED"
    
    } else if (ACtrack == "1") {
      // green
      this.ACImg = "../../assets/image/acGreen.png";
      this.aStatus = "AC ON"
    
    } else if (ACtrack == "0") {
      // red
      this.ACImg = "../../assets/image/acRed.png";
      this.aStatus = "AC OFF"
    
    }

    var pwr = deviceResponse.power;
    if (!pwr) {
      this.pwrURL = "../../assets/image/noPower.png";
      this.pStatus = "No Power";
     
    } else if (pwr == "1") {
      this.pwrURL = "../../assets/image/power_on.png";
      this.pStatus = "Power ON";
     
    } else if (pwr == "0") {
      this.pwrURL = "../../assets/image/power_off.png";
      this.pStatus = "Power OFF";
   
    } 

    var gTrack = deviceResponse.gpsTracking;
    if (!gTrack) {
      this.gpsImg = "../../assets/image/no_gps.png";
      this.gStatus = "GPS NOT SUPPORTED"
    
    } else if (gTrack == "1") {
      this.gpsImg = "../../assets/image/gpsOn.png";
      this.gStatus = "GPS ON"
  
    } else if (gTrack == "0") {
      this.gpsImg = "../../assets/image/gps_off.png";
      this.gStatus = "GPS OFF"
     
    }


  })


}


  Ing_Status() { 
    
    // console.log("Inside Socket function!!!");
    // console.log(this.vehicle_IMEI);
    this.socket_Ing.emit('acc', this.vehicle_IMEI);
    this.socket_Ing.on(this.vehicle_IMEI+'acc', this.accListener(this.vehicle_IMEI));
  }

  

  icon_change(ign){
    // console.log(ign);
  }

}

