'use strict';

// Charts controller
angular.module('charts').controller('ChartsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Charts', 'Feeds',
  function ($scope, $stateParams, $location, Authentication, Charts, Feeds) {
    $scope.authentication = Authentication;

    // Init to show all feeds
    $scope.init = function(){
      $scope.feeds = Feeds.query();
      $scope.types = ['line', 'bar'/* 'doughnut',*/ ,'radar'/*, 'pie', 'polar-area'*/];
      $scope.ranges = ['realtime','hourly','daily'];
    };

    // Create new Chart
    $scope.create = function (isValid) {
      $scope.error = null;

      if (!isValid) {
        $scope.$broadcast('show-errors-check-validity', 'chartForm');
        return false;
      }

      // Create new Chart object
      var chart = new Charts({
        title: this.title,
        content: this.content,
        type: this.typeSelected,    // Insert chart type
        range: this.rangeSelected,  // Insert chart range
        feed: this.feedSelected     // Insert feed associated
      });

      // Redirect after save
      chart.$save(function (response) {
        $location.path('charts/' + response._id);

        // Clear form fields
        $scope.title = '';
        $scope.content = '';
      }, function (errorResponse) {
        $scope.error = errorResponse.data.message;
      });
    };

    // Remove existing Chart
    $scope.remove = function (chart) {
      if (chart) {
        chart.$remove();

        for (var i in $scope.charts) {
          if ($scope.charts[i] === chart) {
            $scope.charts.splice(i, 1);
          }
        }
      } else {
        $scope.chart.$remove(function () {
          $location.path('charts');
        });
      }
    };

    // Update existing Chart
    $scope.update = function (isValid) {
      $scope.error = null;

      if (!isValid) {
        $scope.$broadcast('show-errors-check-validity', 'chartForm');
        return false;
      }

      var chart = $scope.chart;
      chart.type = this.typeSelected;     // Update chart type
      chart.range = this.rangeSelected;   // Update chart range
      chart.feed = this.feedSelected;     // Update feed associated


      chart.$update(function () {
        $location.path('charts/' + chart._id);
      }, function (errorResponse) {
        $scope.error = errorResponse.data.message;
      });
    };

    // Find a list of Charts
    $scope.find = function () {
      $scope.charts = Charts.query();
    };

    // Find existing Chart
    $scope.findOne = function () {

      $scope.feeds = Feeds.query();
      $scope.types = ['line', 'bar'/* 'doughnut',*/ ,'radar'/*, 'pie', 'polar-area'*/];
      $scope.ranges = ['realtime', 'hourly', 'daily'];

      $scope.chart = Charts.get({chartId: $stateParams.chartId}, function(chartResponse){

        //Functions to process chart data needed as the feed.
        var feed = chartResponse.feed;
        var valueFeed = [], dateFeed = [];
        console.log($scope.chart.range);
        for(var i=0; i<feed.content.length; i++){
          valueFeed.unshift(feed.content[i].value);
          if($scope.chart.range === 'daily')
            dateFeed.unshift(feed.content[i].date.substr(8,2) + '/' + feed.content[i].date.substr(5,2));
          else 
            dateFeed.unshift(feed.content[i].date.substr(11,5));
        }

        $scope.labels = dateFeed;
        $scope.data = [valueFeed];
        $scope.series = [feed.title];

        $scope.labelsOther = ['January', 'February', 'March'];//#Change to correct funcionality 
        $scope.dataOther = [10, 20, 70];
      });
    };
  }

]);
