/// <reference path="../../node_modules/moment/moment.d.ts"/>
/// <reference path="../../node_modules/@types/jquery/index.d.ts"/>
// import moment = require("moment");
var DoctorSchedule;
(function (DoctorSchedule_1) {
    var recalculateDate = function (dateSpec, action) {
        dateSpec = parseDate(dateSpec);
        dateSpec.push((new Date).getFullYear());
        dateSpec = dateSpec.reverse();
        dateSpec[1]--;
        var momentDate = moment(dateSpec);
        momentDate[action](7, 'days');
        return momentDate.format('DD.MM');
    };
    var parseDate = function (string) {
        return string.split('.').map(function (data) {
            return parseInt(data);
        });
    };
    var getDateColumn = function (row) {
        return row.find('td:first-child');
    };
    var nextDate = function (prev, next) {
        getRows(next).each(function (index) {
            var date = getDateColumn($(this)).html();
            getDateColumn($(this)).html(recalculateDate(date, 'add'));
        });
        setButtonsText(prev, next, 'add');
    };
    var prevDate = function (prev, next) {
        getRows(next).each(function (index) {
            var date = getDateColumn($(this)).html();
            getDateColumn($(this)).html(recalculateDate(date, 'subtract'));
        });
        setButtonsText(prev, next, 'subtract');
    };
    var setButtonsText = function (prev, next, action) {
        setButtonDate(prev, action);
        setButtonDate(next, action);
    };
    var setButtonDate = function (button, action) {
        var dates = button.html().trim().split(' – ');
        console.log(dates);
        button.html(recalculateDate(dates[0], action) + ' &ndash; ' + recalculateDate(dates[1], action));
    };
    var getRows = function (element) {
        return element.parent().find('tr');
    };
    var DoctorSchedule = (function () {
        function DoctorSchedule(prevBtn, nextBtn) {
            var next = nextDate.bind(this);
            var prev = prevDate.bind(this);
            prevBtn.on('click', function (e) {
                e.preventDefault();
                prev(prevBtn, nextBtn);
            });
            nextBtn.on('click', function (e) {
                e.preventDefault();
                next(prevBtn, nextBtn);
            });
        }

        return DoctorSchedule;
    }());

    function init($prevBtn, $nextBtn) {
        return new DoctorSchedule($prevBtn, $nextBtn);
    }

    DoctorSchedule_1.init = init;
})(DoctorSchedule || (DoctorSchedule = {}));
