angular.module('app').controller('DateRangeCtrl', function ($scope, $rootScope, $timeout, ConditionsGridService, SQConstants, UniqueIdGenerator, AppService) {
    console.log("DateRangeCtrl initialized");

    $scope.sinceLastDateEnabled = false;

    $scope.config = {
        dateFrom : null,
        dateTo : null,
        dateMin : null,
        dateMax : null,
        dateLast : null,
        lastSelection: null
    };

    $scope.onDateFromChange = function (date) {
        $scope.config.dateFrom = date;
        onDateChange();

        $scope.config.lastSelection = null;
    }

    $scope.onDateToChange = function (date) {
        $scope.config.dateTo = date;
        onDateChange();

        $scope.config.lastSelection = null;
    }

    function onDateChange(actionType) {
        var type = actionType ? actionType : "custom";

        $scope.onDateChange($scope.config.dateFrom, $scope.config.dateTo, type);
    }

    $scope.onSinceLastDate = function() {
        $scope.config.dateFrom = $scope.config.dateLast;

        onDateChange("sinceLast");

        $scope.config.lastSelection = 'LastDate';
    }

    $scope.onLastYear = function(){
        var dateFrom = new Date();
        dateFrom.setFullYear(dateFrom.getFullYear() - 1);

        $scope.config.dateFrom = getDateString(dateFrom);

        onDateChange();

        $scope.config.lastSelection = 'LastYear';
    }

    $scope.onLast5Years = function(){
        var dateFrom = new Date();
        dateFrom.setFullYear(dateFrom.getFullYear() - 5);

        $scope.config.dateFrom = getDateString(dateFrom);
        
        onDateChange();

        $scope.config.lastSelection = 'Last5Years';
    }

    $scope.onLast10Years = function(){
        var dateFrom = new Date();
        dateFrom.setFullYear(dateFrom.getFullYear() - 10);

        $scope.config.dateFrom = getDateString(dateFrom);

        onDateChange();

        $scope.config.lastSelection = 'Last10Years';
    }

    $scope.onAllTime = function() {        
        $scope.config.dateFrom = $scope.config.dateMin;
        $scope.config.dateTo = $scope.config.dateMax;

        onDateChange("allTime");

        $scope.config.lastSelection = 'AllTime';
    }

    $scope.onLast6Months = function(){
        var dateFrom = new Date();
        dateFrom.setMonth(dateFrom.getMonth() - 6);

        $scope.config.dateFrom = getDateString(dateFrom);

        onDateChange();

        $scope.config.lastSelection = 'Last6Months';
    }

    $scope.instance.setRange = function(dateFrom, dateTo, dateLast, selectSinceLastDate) {
        $scope.config.dateFrom = dateFrom;
        $scope.config.dateTo = dateTo;

        $scope.config.dateLast = dateTo;
        
        if(dateLast) {
            $scope.config.dateLast = dateLast;
        }

        $scope.config.lastSelection = null;

        if(selectSinceLastDate) {
            $scope.onSinceLastDate();

            $scope.sinceLastDateEnabled = true;
        } else {
            $scope.sinceLastDateEnabled = false;
        }
    }

    $scope.instance.setRangeLimit = function(dateMin, dateMax) {
        $scope.config.dateMin = dateMin;
        $scope.config.dateMax = dateMax;

        try { $scope.$digest(); } catch (er) {}
    }
});