"use strict";

app.controller('TracksIndexPageController', [
  '$scope',
  '$filter',
  '$http',
  'Track',
  'currentUser',
  'Categories',
  'AccumulatedStatistics',
  'TrackFilteringOptions',
  'CategoryListWidget',
  'CategoriesFilteringOptions',

  function ($scope, $filter, $http, Track, currentUser, Categories, AccumulatedStatistics, TrackFilteringOptions, CategoryListWidget, CategoriesFilteringOptions) {
    $scope.apikey = currentUser.apikey;

    // TODO: Move to constants?
    $scope.WEEKLY_PERIOD  = "week";
    $scope.MONTHLY_PERIOD   = "month";
    $scope.YEARLY_PERIOD  = "year";


    $scope.currentRangeStartDate= new Date();
    $scope.period         =  $scope.MONTHLY_PERIOD;

     //...
     //some code not shown
     //...
    $scope.updateRange = function(direction) {
      var now = moment($scope.currentRangeStartDate);
      if ($scope.period == $scope.WEEKLY_PERIOD) {
        var newWeek = now.isoWeek() + direction;
        $scope.currentRangeStartDate = moment().isoWeek(newWeek).isoWeekday(1).toDate();
        $scope.displayFromDate = moment().isoWeek(newWeek).isoWeekday(1).toDate();
        $scope.displayToDate = moment().isoWeek(newWeek).isoWeekday(7).toDate();
      }

      if ($scope.period == $scope.MONTHLY_PERIOD) {
        var newMonth = now.month() + direction;
        $scope.currentRangeStartDate = moment().month(newMonth).date(1).toDate();
        $scope.displayFromDate = $scope.currentRangeStartDate;
        $scope.displayToDate = moment().month(newMonth).add('months', 1).date(0).toDate();
      }

      if ($scope.period == $scope.YEARLY_PERIOD) {
        var newYear = now.year() + direction;
        $scope.currentRangeStartDate = moment().year(newYear).dayOfYear(1).toDate();
        $scope.displayFromDate = $scope.currentRangeStartDate;
        $scope.displayToDate = moment().year(newYear).add('year', 1).dayOfYear(0).toDate();
      }

      $scope.filterData();
    };

     //...
     //some code not shown
     //...
     
    $scope.setRangeSizeTo = function(period) {
      $scope.period = period;
      $scope.updateRange( 0);
      return false;
    };

    $scope.today = function() {
      $scope.currentRangeStartDate = new Date();
      return false;
    };

    $scope.nextRange = function() {
      $scope.updateRange( 1);
      return false;
    };

    $scope.previousRange = function() {
      $scope.updateRange(-1);
      return false;
    };

     //...
     //some code not shown
     //...

    $scope.updateRange(0);
  }
]);