angular.module('gaugesScreen', [])
.controller('GaugesScreenController', function($scope, $window) {
  var unit = null;
  $scope.data = {}
  // overwriting plain javascript function so we can access from within the controller
  $window.setUnits = (data) => {
    unit = data.unitType;
  }
  
  $window.updateData = (data) => {
    $scope.$evalAsync(function() {

      if (data.parkingbrake != 0) {
        if (data.enginerunning != 0 ) {

		$scope.data.odometer = " "
		$scope.data.parkprake = "PARK BRAKE" //dont ask me why \n or <br> dont do shite
		$scope.data.parkprakebot = "ENGAGED" 
		$scope.data.tillempty = " "
		
        }
		else {
		$scope.data.odometer = " "
		$scope.data.parkprake = " "
		$scope.data.parkprakebot = " " 
		$scope.data.tillempty = " "
		} 
      }
      else if (data.parkingbrake === 0){
        if (data.enginerunning != 0 ) {
			if (data.bottomscreenunit != 1 ) {

		$scope.data.odometer = (data.odometer * 0.621371).toFixed(1) + "mi"
		$scope.data.parkprake = " "
		$scope.data.parkprakebot = " " 
		$scope.data.tillempty = (data.tillempty * 0.621371).toFixed(0) + " MILES TO E"
		}
		else {
		$scope.data.odometer = (data.odometer).toFixed(1) + "km"
		$scope.data.parkprake = " "
		$scope.data.parkprakebot = " " 
		$scope.data.tillempty = (data.tillempty).toFixed(0) + " KM TO E"
		}
        }
		else {
		$scope.data.odometer = " "
		$scope.data.parkprake = " "
		$scope.data.parkprakebot = " " 
		$scope.data.tillempty = " "
		}
		
      }
    }
	)
 }
});    