<commit_before>(function() {
  'use strict';

  angular.module('app.components')
    .controller('KitController', KitController);

    KitController.$inject = ['$state','$scope', '$stateParams', 'kitData',
      'ownerKits', 'utils', 'sensor', 'FullKit', '$mdDialog', 'belongsToUser',
      'timeUtils', 'animation', '$location', 'auth', 'kitUtils', 'userUtils',
      '$timeout', 'mainSensors', 'compareSensors', 'alert', '$q', 'device',
      'HasSensorKit', 'geolocation'];
    function KitController($state, $scope, $stateParams, kitData,
      ownerKits, utils, sensor, FullKit, $mdDialog, belongsToUser,
      timeUtils, animation, $location, auth, kitUtils, userUtils,
      $timeout, mainSensors, compareSensors, alert, $q, device,
      HasSensorKit, geolocation) {

      var vm = this;
      var sensorsData = [];

      var mainSensorID, compareSensorID;
      var picker = initializePicker();

      if(kitData){
        animation.kitLoaded({lat: kitData.latitude ,lng: kitData.longitude, id: parseInt($stateParams.id) });
      }
      console.log(mainSensors);

      vm.kit = kitData;
      vm.ownerKits = ownerKits;
      vm.kitBelongsToUser = belongsToUser;
      vm.removeUser = removeUser;

      vm.battery = undefined;
      vm.sensors = mainSensors ? mainSensors : undefined;
      vm.sensorsToCompare = compareSensors;

      vm.slide = slide;

      console.log(vm.sensors[0]);

      vm.selectedSensor = (vm.sensors && vm.sensors.length > 0) ? vm.sensors[0].uuid : undefined;
      vm.selectedSensorData = {};

      vm.selectedSensorToCompare = undefined;
      vm.selectedSensorToCompareData = {};

      vm.showSensorOnChart = showSensorOnChart;
      vm.moveChart = moveChart;
      vm.loadingChart = true;

      vm.geolocate = geolocate;

      // event listener on change of value of main sensor selector
      $scope.$watch('vm.selectedSensor', function(newVal, oldVal) {
        vm.selectedSensorToCompare = undefined;
        vm.selectedSensorToCompareData = {};
        vm.chartDataCompare = [];
        compareSensorID = undefined;

        if(vm.sensors){
          vm.sensors.forEach(function(sensor) {
            if(sensor.uuid === newVal) {
              _.extend(vm.selectedSensorData, sensor);
            }
          });
        }
        vm.sensorsToCompare = getSensorsToCompare();

        $timeout(function() {
          colorSensorMainIcon();
          colorSensorCompareName();

          setSensor({type: 'main', value: newVal});
          changeChart([mainSensorID]);
        }, 100);

      });

      // event listener on change of value of compare sensor selector
      $scope.$watch('vm.selectedSensorToCompare', function(newVal, oldVal) {
        vm.sensorsToCompare.forEach(function(sensor) {
          if(sensor.uuid === newVal) {
            _.extend(vm.selectedSensorToCompareData, sensor);
          }
        });

        $timeout(function() {
          colorSensorCompareName();
          setSensor({type: 'compare', value: newVal});

          if(oldVal === undefined && newVal === undefined) {
            return;
          }
          changeChart([compareSensorID]);
        }, 100);

      });

      $scope.$on('hideChartSpinner', function() {
        vm.loadingChart = false;
      });

      initialize();

      ///////////////

      function initialize() {
        $timeout(function() {
          colorSensorMainIcon();
          colorArrows();
          colorClock();
          // events below can probably be refactored to use $viewContentLoaded https://github.com/angular-ui/ui-router/wiki#user-content-view-load-events
          animation.viewLoaded();
          animation.mapStateLoaded();
        }, 1000);

        if(vm.kit){
          if(vm.kit.state.name === 'never published' || vm.kit.state.name === 'not configured') {
            if(vm.kitBelongsToUser) {
              alert.info.noData.owner($stateParams.id);
            } else {
              alert.info.noData.visitor();
            }
            $timeout(function() {
              animation.kitWithoutData({belongsToUser: vm.kitBelongsToUser});
            }, 1000);
          } else if(!timeUtils.isWithin(1, 'months', vm.kit.time)) {
            alert.info.longTime();
          }else{
            if(geolocation.isHTML5GeolocationGranted()){
              geolocate();
            }
          }
        }
      }

      function removeUser() {
        var alert = $mdDialog.alert()
          .title('Delete your account?')
          .content('If you wish to remove you account completely, please contact our support team at <EMAIL>')
          .ariaLabel('')
          .ok('OK!')
          .theme('primary')
          .clickOutsideToClose(true);

        $mdDialog.show(alert);
      }

      function showSensorOnChart(sensorID) {
        vm.selectedSensor = sensorID;
      }

      function slide(direction) {
        var slideContainer = angular.element('.sensors_container');
        var scrollPosition = slideContainer.scrollLeft();
        var slideStep = 20;

        if(direction === 'left') {
          slideContainer.scrollLeft(scrollPosition + slideStep);
        } else if(direction === 'right') {
          slideContainer.scrollLeft(scrollPosition - slideStep);
        }
      }

      function getSensorsToCompare() {
        return vm.sensors ? vm.sensors.filter(function(sensor) {
          return sensor.uuid !== vm.selectedSensor;
        }) : [];
      }

      function changeChart(sensorsID, options) {
        if(!sensorsID[0]) {
          return;
        }

        if(!options) {
          options = {};
        }
        options.from = options && options.from || picker.getValuePickerFrom();
        options.to = options && options.to || picker.getValuePickerTo();

        //show spinner
        vm.loadingChart = true;
        //grab chart data and save it

        // it can be either 2 sensors or 1 sensor, so we use $q.all to wait for all
        $q.all(
          _.map(sensorsID, function(sensorID) {
            console.log(sensorsID);
            var id = vm.kit.uuid;//$stateParams.id
            return getChartData(id, sensorID, options.from, options.to)
              .then(function(data) {
                return data;
              });
          })
        ).then(function() {
          // after all sensors resolve, prepare data and attach it to scope
          // the variable on the scope will pass the data to the chart directive
          vm.chartDataMain = prepareChartData([mainSensorID, compareSensorID]);
        });
      }
      // calls api to get sensor data and saves it to sensorsData array
      function getChartData(deviceID, sensorID, dateFrom, dateTo, options) {
        return sensor.getSensorsDataNew(deviceID, sensorID, dateFrom, dateTo)
          .then(function(data) {
            //save sensor data of this kit so that it can be reused
            sensorsData[sensorID] = data.readings;
            return data;
          });
      }

      function prepareChartData(sensorsID) {
        var compareSensor;
        var parsedDataMain = parseSensorData(sensorsData, sensorsID[0]);
        var mainSensor = {
          data: parsedDataMain,
          color: vm.selectedSensorData.color,
          unit: vm.selectedSensorData.unit
        };
        if(sensorsID[1] && sensorsID[1] !== -1) {
          var parsedDataCompare = parseSensorData(sensorsData, sensorsID[1]);
          compareSensor = {
            data: parsedDataCompare,
            color: vm.selectedSensorToCompareData.color,
            unit: vm.selectedSensorToCompareData.unit
          };
        }
        var newChartData = [mainSensor, compareSensor];
        return newChartData;
      }

      function parseSensorData(data, sensorID) {
        if(data[sensorID].length === 0) {
          return [];
        }
        return data[sensorID].map(function(dataPoint) {
          var time = moment(dataPoint.datetime).toISOString(); //tmp. ensure validation
          var value = Number(dataPoint.value);
          var count = value === null ? 0 : value;

          return {
            time: time,
            count: count,
            value: value
          };
        });
      }

      function setSensor(options) {
        var sensorID = options.value;
        if(sensorID === undefined) {
          return;
        }
        if(options.type === 'main') {
          mainSensorID = sensorID;
        } else if(options.type === 'compare') {
          compareSensorID = sensorID;
          console.log(sensorID);
        }
      }

      function colorSensorMainIcon() {
        var svgContainer = angular.element('.sensor_icon_selected');
        var parent = svgContainer.find('.container_parent');
        parent.css('fill', vm.selectedSensorData.color);
      }

      function colorSensorCompareName() {
        var name = angular.element('.sensor_compare').find('md-select-label').find('span');
        name.css('color', vm.selectedSensorToCompareData.color || 'white');
        var icon = angular.element('.sensor_compare').find('md-select-label').find('.md-select-icon');
        icon.css('color', 'white');
      }

      function colorArrows() {
        var svgContainer;

        svgContainer = angular.element('.chart_move_left').find('svg');
        svgContainer.find('.fill_container').css('fill', '#03252D');

        svgContainer = angular.element('.chart_move_right').find('svg');
        svgContainer.find('.fill_container').css('fill', '#4E656B');
      }

      function colorClock() {
        var svgContainer = angular.element('.kit_time_icon');
        svgContainer.find('.stroke_container').css({'stroke-width': '0.5px', 'stroke':'#A4B0B3'});<commit_msg>Cleaned<commit_after>(function() {
  'use strict';

  angular.module('app.components')
    .controller('KitController', KitController);

    KitController.$inject = ['$state','$scope', '$stateParams', 'kitData',
      'ownerKits', 'utils', 'sensor', 'FullKit', '$mdDialog', 'belongsToUser',
      'timeUtils', 'animation', '$location', 'auth', 'kitUtils', 'userUtils',
      '$timeout', 'mainSensors', 'compareSensors', 'alert', '$q', 'device',
      'HasSensorKit', 'geolocation'];
    function KitController($state, $scope, $stateParams, kitData,
      ownerKits, utils, sensor, FullKit, $mdDialog, belongsToUser,
      timeUtils, animation, $location, auth, kitUtils, userUtils,
      $timeout, mainSensors, compareSensors, alert, $q, device,
      HasSensorKit, geolocation) {

      var vm = this;
      var sensorsData = [];

      var mainSensorID, compareSensorID;
      var picker = initializePicker();

      if(kitData){
        animation.kitLoaded({lat: kitData.latitude ,lng: kitData.longitude, id: parseInt($stateParams.id) });
      }

      vm.kit = kitData;
      vm.ownerKits = ownerKits;
      vm.kitBelongsToUser = belongsToUser;
      vm.removeUser = removeUser;

      vm.battery = undefined;
      vm.sensors = mainSensors ? mainSensors : undefined;
      vm.sensorsToCompare = compareSensors;

      vm.slide = slide;

      vm.selectedSensor = (vm.sensors && vm.sensors.length > 0) ? vm.sensors[0].uuid : undefined;
      vm.selectedSensorData = {};

      vm.selectedSensorToCompare = undefined;
      vm.selectedSensorToCompareData = {};

      vm.showSensorOnChart = showSensorOnChart;
      vm.moveChart = moveChart;
      vm.loadingChart = true;

      vm.geolocate = geolocate;

      // event listener on change of value of main sensor selector
      $scope.$watch('vm.selectedSensor', function(newVal, oldVal) {
        vm.selectedSensorToCompare = undefined;
        vm.selectedSensorToCompareData = {};
        vm.chartDataCompare = [];
        compareSensorID = undefined;

        if(vm.sensors){
          vm.sensors.forEach(function(sensor) {
            if(sensor.uuid === newVal) {
              _.extend(vm.selectedSensorData, sensor);
            }
          });
        }
        
        vm.sensorsToCompare = getSensorsToCompare();

        $timeout(function() {
          colorSensorMainIcon();
          colorSensorCompareName();

          setSensor({type: 'main', value: newVal});
          changeChart([mainSensorID]);
        }, 100);

      });

      // event listener on change of value of compare sensor selector
      $scope.$watch('vm.selectedSensorToCompare', function(newVal, oldVal) {
        vm.sensorsToCompare.forEach(function(sensor) {
          if(sensor.uuid === newVal) {
            _.extend(vm.selectedSensorToCompareData, sensor);
          }
        });

        $timeout(function() {
          colorSensorCompareName();
          setSensor({type: 'compare', value: newVal});

          if(oldVal === undefined && newVal === undefined) {
            return;
          }
          changeChart([compareSensorID]);
        }, 100);

      });
      
      $scope.$on('hideChartSpinner', function() {
        vm.loadingChart = false;
      });

      initialize();

      ///////////////

      function initialize() {
        $timeout(function() {
          colorSensorMainIcon();
          colorArrows();
          colorClock();
          // events below can probably be refactored to use $viewContentLoaded https://github.com/angular-ui/ui-router/wiki#user-content-view-load-events
          animation.viewLoaded();
          animation.mapStateLoaded();
        }, 1000);

        if(vm.kit){
          if(vm.kit.state.name === 'never published' || vm.kit.state.name === 'not configured') {
            if(vm.kitBelongsToUser) {
              alert.info.noData.owner($stateParams.id);
            } else {
              alert.info.noData.visitor();
            }
            $timeout(function() {
              animation.kitWithoutData({belongsToUser: vm.kitBelongsToUser});
            }, 1000);
          } else if(!timeUtils.isWithin(1, 'months', vm.kit.time)) {
            alert.info.longTime();
          }else{
            if(geolocation.isHTML5GeolocationGranted()){
              geolocate();
            }
          }
        }
      }

      function removeUser() {
        var alert = $mdDialog.alert()
          .title('Delete your account?')
          .content('If you wish to remove you account completely, please contact our support team at <EMAIL>')
          .ariaLabel('')
          .ok('OK!')
          .theme('primary')
          .clickOutsideToClose(true);

        $mdDialog.show(alert);
      }

      function showSensorOnChart(sensorID) {
        vm.selectedSensor = sensorID;
      }

      function slide(direction) {
        var slideContainer = angular.element('.sensors_container');
        var scrollPosition = slideContainer.scrollLeft();
        var slideStep = 20;

        if(direction === 'left') {
          slideContainer.scrollLeft(scrollPosition + slideStep);
        } else if(direction === 'right') {
          slideContainer.scrollLeft(scrollPosition - slideStep);
        }
      }

      function getSensorsToCompare() {
        return vm.sensors ? vm.sensors.filter(function(sensor) {
          return sensor.uuid !== vm.selectedSensor;
        }) : [];
      }

      function changeChart(sensorsID, options) {
        if(!sensorsID[0]) {
          return;
        }

        if(!options) {
          options = {};
        }
        options.from = options && options.from || picker.getValuePickerFrom();
        options.to = options && options.to || picker.getValuePickerTo();

        //show spinner
        vm.loadingChart = true;
        //grab chart data and save it

        // it can be either 2 sensors or 1 sensor, so we use $q.all to wait for all
        $q.all(
          _.map(sensorsID, function(sensorID) {
            var id = vm.kit.uuid;//$stateParams.id
            return getChartData(id, sensorID, options.from, options.to)
              .then(function(data) {
                return data;
              });
          })
        ).then(function() {
          // after all sensors resolve, prepare data and attach it to scope
          // the variable on the scope will pass the data to the chart directive
          vm.chartDataMain = prepareChartData([mainSensorID, compareSensorID]);
        });
      }
      // calls api to get sensor data and saves it to sensorsData array
      function getChartData(deviceID, sensorID, dateFrom, dateTo, options) {
        return sensor.getSensorsDataNew(deviceID, sensorID, dateFrom, dateTo)
          .then(function(data) {
            //save sensor data of this kit so that it can be reused
            sensorsData[sensorID] = data.readings;
            return data;
          });
      }

      function prepareChartData(sensorsID) {
        var compareSensor;
        var parsedDataMain = parseSensorData(sensorsData, sensorsID[0]);

        var mainSensor = {
          data: parsedDataMain,
          color: vm.selectedSensorData.color,
          unit: vm.selectedSensorData.unit
        };
        if(sensorsID[1] && sensorsID[1] !== -1) {
          var parsedDataCompare = parseSensorData(sensorsData, sensorsID[1]);
          compareSensor = {
            data: parsedDataCompare,
            color: vm.selectedSensorToCompareData.color,
            unit: vm.selectedSensorToCompareData.unit
          };
        }
        var newChartData = [mainSensor, compareSensor];
        return newChartData;
      }

      function parseSensorData(data, sensorID) {
        if(data[sensorID].length === 0) {
          return [];
        }
        return data[sensorID].map(function(dataPoint) {
          var time = moment(dataPoint.datetime).toISOString(); //tmp. ensure validation
          var value = Number(dataPoint.value);
          var count = value === null ? 0 : value;

          return {
            time: time,
            count: count,
            value: value
          };
        });
      }

      function setSensor(options) {
        var sensorID = options.value;
        if(sensorID === undefined) {
          return;
        }
        if(options.type === 'main') {
          mainSensorID = sensorID;
        } else if(options.type === 'compare') {
          compareSensorID = sensorID;
        }
      }

      function colorSensorMainIcon() {
        var svgContainer = angular.element('.sensor_icon_selected');
        var parent = svgContainer.find('.container_parent');
        parent.css('fill', vm.selectedSensorData.color);
      }

      function colorSensorCompareName() {
        var name = angular.element('.sensor_compare').find('md-select-label').find('span');
        name.css('color', vm.selectedSensorToCompareData.color || 'white');
        var icon = angular.element('.sensor_compare').find('md-select-label').find('.md-select-icon');
        icon.css('color', 'white');
      }

      function colorArrows() {
        var svgContainer;

        svgContainer = angular.element('.chart_move_left').find('svg');
        svgContainer.find('.fill_container').css('fill', '#03252D');

        svgContainer = angular.element('.chart_move_right').find('svg');
        svgContainer.find('.fill_container').css('fill', '#4E656B');
      }

      function colorClock() {
        var svgContainer = angular.element('.kit_time_icon');
        svgContainer.find('.stroke_container').css({'stroke-width': '0.5px', 'stroke':'#A4B0B3'});
{'fraction_non_alphanumeric': 0.12855357700718525, 'fraction_numerical': 0.004633968551494325, 'mean_word_length': 2.1699950486879023, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5102609', 'n_tokens_mistral': 5357, 'n_tokens_neox': 5249, 'n_words': 1069}