/**
 * Created by wok on 08.02.16.
 */

class TaskRepeater {
    constructor(templateTask) {
        this._cloneTask = new Task();
        this._cloneTask.cloneFromTask (templateTask);
        this._calcRepeatedDates();
        this._cloneTask.save();
    }

    get clone() {
        return this._cloneTask;
    }

    // ###### PRIVATE ##############

    _calcRepeatedDates() {
        console.log("NEW CALC-REPEAT-DATES");
        if (this._cloneTask.repeat && this._cloneTask.repeat.repeat) {
            if (this._cloneTask.repeat.everyInterval == "day") {
                this._cloneTask = this._calcRepeatedDays(this._cloneTask.repeat.everyNumber);
            }
            if (this._cloneTask.repeat.everyInterval == "week") {
                this._cloneTask = this._calcRepeatedDays(7 * this._cloneTask.repeat.everyNumber);
            }
            if (this._cloneTask.repeat.everyInterval == "month") {
                this._cloneTask = this._calcRepeatedMonth(this._cloneTask.repeat.everyNumber);
            }
            if (this._cloneTask.repeat.everyInterval == "year") {
                this._cloneTask = this._calcRepeatedYear(this._cloneTask.repeat.everyNumber);
            }
        }
    };

    _calcRepeatedDays (dayDelta) {
        var groundDate = new Date();    // if (this._cloneTask.repeat.repeatOnCompleteDate) ...

        if (this._cloneTask.startDate && !this._cloneTask.dueDate) {
            if (this._cloneTask.repeat.repeatOnTaskDate) {
                groundDate = this._cloneTask.startDate;
            }
            this._cloneTask.startDate = groundDate;   // setDate can't work accross month. We must pull it up to grounddate, first.
            this._cloneTask.startDate.setDate(groundDate.getDate() + dayDelta);
        }
        if (!this._cloneTask.startDate && this._cloneTask.dueDate) {
            if (this._cloneTask.repeat.repeatOnTaskDate) {
                groundDate = this._cloneTask.dueDate;
            }
            this._cloneTask.dueDate = groundDate;   // setDate can't work accross month. We must pull it up to grounddate, first.
            this._cloneTask.dueDate.setDate(groundDate.getDate() + dayDelta);
        }
        if (this._cloneTask.startDate && this._cloneTask.dueDate) {
            if (this._cloneTask.repeat.repeatOnTaskDate) {
                groundDate = this._cloneTask.dueDate;
            }
            var startToDueTimeDiff = this._cloneTask.dueDate.getTime() - this._cloneTask.startDate.getTime()
            this._cloneTask.dueDate = groundDate;   // setDate can't work accross month. We must pull it up to grounddate, first.
            this._cloneTask.dueDate.setDate(groundDate.getDate() + dayDelta);
            this._cloneTask.startDate.setTime(this._cloneTask.dueDate.getTime() - startToDueTimeDiff);
        }

        return this._cloneTask;
    };


    _calcRepeatedMonth(monthDelta) {
        var groundDate = new Date();    // if (this._cloneTask.repeat.repeatOnCompleteDate) ...

        if (this._cloneTask.startDate && !this._cloneTask.dueDate) {
            if (this._cloneTask.repeat.repeatOnTaskDate) {
                groundDate = this._cloneTask.startDate;
            }

            this._cloneTask.startDate.setMonth(groundDate.getMonth() + monthDelta);
        }
        if (!this._cloneTask.startDate && this._cloneTask.dueDate) {
            if (this._cloneTask.repeat.repeatOnTaskDate) {
                groundDate = this._cloneTask.dueDate;
            }
            this._cloneTask.dueDate.setMonth(groundDate.getMonth() + monthDelta);
        }
        if (this._cloneTask.startDate && this._cloneTask.dueDate) {
            if (this._cloneTask.repeat.repeatOnTaskDate) {
                groundDate = this._cloneTask.dueDate;
            }
            var startToDueTimeDiff = this._cloneTask.dueDate.getTime() - this._cloneTask.startDate.getTime()
            this._cloneTask.dueDate.setMonth(groundDate.getMonth() + monthDelta);
            this._cloneTask.startDate.setTime(this._cloneTask.dueDate.getTime() - startToDueTimeDiff);
        }

        return this._cloneTask;
    };


    _calcRepeatedYear(yearDelta) {
        var groundDate = new Date();    // if (this._cloneTask.repeat.repeatOnCompleteDate) ...

        if (this._cloneTask.startDate && !this._cloneTask.dueDate) {
            if (this._cloneTask.repeat.repeatOnTaskDate) {
                groundDate = this._cloneTask.startDate;
            }

            this._cloneTask.startDate.setFullYear(groundDate.getFullYear() + yearDelta);
        }
        if (!this._cloneTask.startDate && this._cloneTask.dueDate) {
            if (this._cloneTask.repeat.repeatOnTaskDate) {
                groundDate = this._cloneTask.dueDate;
            }
            this._cloneTask.dueDate.setFullYear(groundDate.getFullYear() + yearDelta);
        }
        if (this._cloneTask.startDate && this._cloneTask.dueDate) {
            if (this._cloneTask.repeat.repeatOnTaskDate) {
                groundDate = this._cloneTask.dueDate;
            }
            var startToDueTimeDiff = this._cloneTask.dueDate.getTime() - this._cloneTask.startDate.getTime()
            this._cloneTask.dueDate.setFullYear(groundDate.getFullYear() + yearDelta);
            this._cloneTask.startDate.setTime(this._cloneTask.dueDate.getTime() - startToDueTimeDiff);
        }

        return this._cloneTask;
    };
}

this.TaskRepeater = TaskRepeater;

@@ -1610,33 +1610,32 @@
 te can't work ac
-c
 ross month. We m
@@ -2025,33 +2025,32 @@
 te can't work ac
-c
 ross month. We m
@@ -2554,17 +2554,16 @@
  work ac
-c
 ross mon

Comment typos