Tizen 2.0 Release
[samples/web/ExercisePlanner.git] / js / app.alarmsGenerating.js
1 /*jslint devel: true*/
2 /*global ExercisePlanner: true, ONE_DAY:false, TIME_OF_SLEEP:false */
3 /**
4  *
5  */
6 (function () {
7         "use strict";
8         /**
9          *
10          *
11          * @param availableTime
12          * @param typeOfPeriod
13          * @returns {object}
14          */
15         ExercisePlanner.prototype.calculateFactor = function calculateFactors(availableTime, typeOfPeriod) {
16                 var factor,
17                         result = {
18                                 count: 0,
19                                 period: 0
20                         };
21
22                 if (availableTime === 0) {
23                         return result;
24                 }
25
26                 factor = availableTime / (this.ONE_DAY - this.TIME_OF_SLEEP);
27                 result.proportionalFrequency = this.getNumberOfWorkoutsByFrequency(this.config.frequency[typeOfPeriod]) * factor;
28
29                 if ((Math.round(result.proportionalFrequency) - 1) > 0) {
30                         result.count = (Math.round(result.proportionalFrequency));
31                         result.period = availableTime / (result.count - 1);
32                 } else {
33                         result.count = 1;
34                         result.period = 0;
35                 }
36
37                 return result;
38         };
39
40         /**
41          *
42          *
43          * @param availableTime
44          * @returns {object}
45          */
46         ExercisePlanner.prototype.calculateFactors = function calculateFactors(availableTime) {
47                 return {
48                         weekend: this.calculateFactor(availableTime.weekend, 'weekend'),
49                         workday: this.calculateFactor(availableTime.workday, 'workday'),
50                         everyday: this.calculateFactor(availableTime.everyday, 'workday')
51                 };
52         };
53
54         /**
55          *
56          *
57          * @param factor
58          * @param tmpPeriods
59          * @returns {Array}
60          */
61         ExercisePlanner.prototype.generateAlarmsByFactor = function (factor, tmpPeriods) {
62                 var i, numberOfPeriods = tmpPeriods.length,
63                         period,
64                         begin,
65                         end = 0,
66                         tableOfAlarms = [],
67                         optimalTime,
68                         deltaTime = 0,
69                         dayTime = this.beginDate || new Date();
70
71                 if (numberOfPeriods === 0) {
72                         return [];
73                 }
74
75                 begin = tmpPeriods[0].start;
76
77                 for (i = 0; i < numberOfPeriods; i += 1) {
78                         if (tmpPeriods[i].stop > end) {
79                                 end = tmpPeriods[i].stop;
80                         }
81                 }
82
83                 dayTime.setSeconds(0);
84
85                 if (factor.count === 1) {
86                         // One alarm per day, default placed in middle of available time;
87                         optimalTime = this.findNearestTimeRange((end + begin) / 2, tmpPeriods);
88
89                         dayTime.setHours(parseInt(optimalTime.optimalHour, 10));
90                         dayTime.setMinutes(60 * (optimalTime.optimalHour - parseInt(optimalTime.optimalHour, 10)));
91                         tableOfAlarms.push(new Date(dayTime.getTime()));
92                 } else {
93                         // set time for begin;
94                         dayTime.setHours(tmpPeriods[0].start);
95                         dayTime.setMinutes(0);
96                         tableOfAlarms.push(new Date(dayTime.getTime()));
97
98                         // set time for next hop;
99                         for (i = 0; i < numberOfPeriods; i += 1) {
100                                 period = tmpPeriods[i];
101                                 deltaTime += period.duration;
102                                 // if available period is too small, then accumulate time
103                                 // and continue to next period;
104                                 while (deltaTime >= factor.period * 0.999) {
105                                         deltaTime -= factor.period;
106
107                                         dayTime.setHours(parseInt(period.stop - deltaTime, 10));
108                                         dayTime.setMinutes(60 * (period.stop - deltaTime - parseInt(period.stop - deltaTime, 10)));
109
110                                         tableOfAlarms.push(new Date(dayTime.getTime()));
111                                 }
112                         }
113                 }
114
115                 return tableOfAlarms;
116         };
117
118         /**
119          * Generate table of alarms => this.alarms
120          * @param {Date} customDate;
121          */
122         ExercisePlanner.prototype.generateAlarms = function () {
123                 var factors,
124                         alarms = this.alarms,
125                         periodsWeek = {
126                                 everyday: [],
127                                 workday: [],
128                                 weekend: []
129                         };
130
131                 // some periods may overlap, must be merged
132                 periodsWeek = this.mergePeriods();
133                 // store in cache for later reuse
134                 this.cache.periodsWeek = periodsWeek;
135
136                 // factors to correct how often may workouts per day
137                 factors = this.calculateFactors(this.getSummaryAvailableTime());
138
139                 // Set new alarms;
140                 if (periodsWeek.everyday.length > 0) {
141                         alarms.everyday = this.generateAlarmsByFactor(factors.everyday, periodsWeek.everyday);
142                 } else {
143                         alarms.workday = this.generateAlarmsByFactor(factors.workday, periodsWeek.workday);
144                         alarms.weekend = this.generateAlarmsByFactor(factors.weekend, periodsWeek.weekend);
145                 }
146
147                 // if trainig is in run then resinstall alarm imediately
148                 if (this.config.trainingEnabled) {
149                         this.startAlarms();
150                 }
151         };
152 }());
153