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

@Component({
  selector: 'app-censor-display',
  templateUrl: './censor-display.component.html',
  styleUrls: ['./censor-display.component.scss']
})
export class CensorDisplayComponent implements OnInit {
  identifier : String = "censor_detail";
  Load : boolean = false;
  superAdmin: any;
  emailid: any;
  useridd: any;
  custtype: any;
  data_descip: string;
  censorType:any=[];
  tempreture1: any=0;
  tempreture2: any=0;
  gaugeLabel= false;
  constructor(public dialog: MdDialog,public snackBar: MdSnackBar,private contactService: ContactService) { }

  ngOnInit() {
    this.superAdmin = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).isSuperAdmin;
    this.emailid = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).email; 
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;   
    this.custtype =  JSON.parse(window.atob(window.localStorage.token.split('.')[1])).isDealer;

 
  }


  deviceArr = []; 
  Fromdate = new Date() ; 
  todate = new Date();


  getReport(event){
    var deviceId: any;
    var tpdata = event ; 
    var that = this;
   
      console.log(event);
    if(event.device.length==0){
      deviceId=[];
    }else{
      deviceId= event.device;
    }
    this.deviceArr = deviceId;        
   
    this.getExpenses()
}

getExpenses(){
   
  if(this.deviceArr.length === 0){
    this.data_descip = "Please Select Device";
   return launch_toast()   ;
  }

  var getExpensesPayload = {
    user : this.useridd,
    vehicle : this.deviceArr[0]
  }

  this.tempreture1=0
  this.tempreture2=0
  console.log('getExpensesPayload',getExpensesPayload);
  this.censorType = [];
  this.contactService.getdevById(this.deviceArr[0]).subscribe(res=>{
    console.log('resresresresres=>',JSON.parse(res['_body']));  
    var devObj = JSON.parse(res['_body']);
    // (parseFloat(temp)/10).toFixed(2)
    this.tempreture1 = Number(devObj.temp?(parseFloat(devObj.temp)/10).toFixed(2) : 0 ); 
    this.tempreture2 = Number(devObj.temp1?(parseFloat(devObj.temp1)/10).toFixed(2) : 0) ; 
    console.log(this.tempreture2,(this.tempreture1));
    var that = this  ;
    // setTimeout(function(){
      this.gaugeLabel=true;
      google.charts.load('current', {'packages':['gauge']});
      google.charts.setOnLoadCallback(drawChart);
      //  }, 5000);
 
  },err=>{
    this.data_descip = "Internal Server Error";
    return launch_toast()   ;
  })
  // mainUrl + "expense/expensebycateogry?user=" + this.islogin._id + "&fdate=" + new Date(this.datetimeStart).toISOString() + "&tdate=" + new Date(this.datetimeEnd).toISOString() + "&vehicle=" + this._vehId._id;

  function launch_toast() {
    // console.log(divid);
     var x = document.getElementById("toast")
     //console.log(x);
     x.className = "show";
     setTimeout(function(){ x.className = x.className.replace("show", ""); }, 1500);
 }
 var that=this;
 function drawChart() {
  
  
 
 var data = google.visualization.arrayToDataTable([
   ['Label', 'Value'],
   ['Temp 1', that.tempreture1],
   
 ]);

 var options = {
   width: 400, height: 120,
   redFrom: 90, redTo: 100,
   yellowFrom:75, yellowTo: 90,
   minorTicks: 5
 };

 var data_1 = google.visualization.arrayToDataTable([
   ['Label', 'Value'],
   ['Temp 2', that.tempreture2],
 ]);

 var options_1 = {
   width: 400, height: 120,
   redFrom: 90, redTo: 100,
   yellowFrom:75, yellowTo: 90,
   minorTicks: 5
 };

 var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
 var chart_1 = new google.visualization.Gauge(document.getElementById('chart_div_1'));

 chart.draw(data, options);
 chart_1.draw(data_1, options_1);

 // setInterval(function() {
 //   data.setValue(0, 1, 40 + Math.round(60 * Math.random()));
 //   chart.draw(data, options);
 // }, 13000);
 // setInterval(function() {
 //   data.setValue(1, 1, 40 + Math.round(60 * Math.random()));
 //   chart.draw(data, options);
 // }, 5000);
 // setInterval(function() {
   //   data.setValue(2, 1, 60 + Math.round(20 * Math.random()));
 //   chart.draw(data, options);
 // }, 26000);
}
}





}
