angular.module("mystrom.scheduler.controllers",["ionic"]).controller("SchedulerController",["$scope","tile","schedule","$ionicHistory","schedulerService","$timeout","loadingService","$translate","settingsService","$ionicPopup",function(e,a,s,t,r,n,c,u,l,d){function o(a){var s=a.split(";");e.scheduler.week={mon:[],tue:[],wed:[],thu:[],fri:[],sat:[],sun:[]};var t=e.scheduler.week;if(""!==s[0]){for(var r=0,n=s.length;n>r;r++){var c=s[r].split(" "),u=c[0],l=c[1],d=c[2];""!==u&&t[u].push(l+" "+d)}_.isEqual(t.mon,t.tue)&&_.isEqual(t.tue,t.wed)&&_.isEqual(t.wed,t.thu)&&_.isEqual(t.thu,t.fri)&&_.isEqual(t.fri,t.sat)&&_.isEqual(t.sat,t.sun)?(e.weekType=!0,e.dayType=!1,e.schedulerData=i(t.mon),e.schedulerData!=={}&&(e.weekData=angular.copy(e.schedulerData))):(e.weekType=!1,e.dayType=!0,e.day="mon",e.schedulerData=i(t.mon))}}function i(e){for(var a=[],s=[],t=0,r=0;r<e.length;r++){e[r]=e[r].trim();var n=e[r].split(" ")[0],c=e[r].split(" ")[1],u=n.split(":")[0],l=n.split(":")[1];t=2*u+("30"===l?1:0),""!==e[r]&&s.push({offset:t,state:c})}for(var d=0,r=0;r<s.length;r++){for(var o=d;o<s[r].offset;o++)a.push("on"===s[r].state?0:1);d=s[r].offset}var i="off";s.length>0&&(i=s[s.length-1].state,a.push("on"==i?1:0));for(var r=s.length;48>r;r++)a.push("on"==i?1:0);return a}function h(e,a){for(var s="",t=0,r=0,n=a.length;n>r;r++)if(a[r]!=t){if(24!=parseInt(r/2))if(1==a[r]){var c=e+" "+parseInt(r/2)+":"+(r%2==1?30:0)+" on;";s+=c.trim()}else{var c=e+" "+parseInt(r/2)+":"+(r%2==1?30:0)+" off;";s+=c.trim()}t=a[r]}return s}e.$on("$ionicView.afterEnter",function(){ionic.Platform.isIPad()?n(function(){c.hideLoading()},500):c.hideLoading()}),e.tile=a,e.isIPad=ionic.Platform.isIPad(),e.isIOS=ionic.Platform.isIOS(),e.isAndroid=ionic.Platform.isAndroid(),e.weekType=!0,e.scheduler={},e.scheduler.reset=!1,e.scheduler.changed=!1,e.schedulerData=_.map(new Array(48),function(){return 0}),o(s.ranges),e.weekData=angular.copy(e.schedulerData),e.scheduler.show=s.enabled,e.showDescription=!e.scheduler.show,e.state={},e.state.show=e.scheduler.show,e.$watchCollection("schedulerData",function(a,s){var t=_.isEqual(a,s);if(t||e.scheduler.reset||(e.scheduler.changed=!0),e.scheduler.reset=!1,e.weekType&&!t)e.weekData=angular.copy(e.schedulerData);else if(e.dayType&&!t){var r=h("",e.schedulerData).split(";");e.scheduler.week[e.day]=angular.copy(r)}}),e.changeSchedulerState=function(){e.scheduler.changed=!0,console.log(e.state.show),window.cordova&&l.setAnalytics("Scheduler"),e.weekType||e.dayType||(e.weekType=!0,e.dayType=!1),e.scheduler.show=!e.scheduler.show;var a=e.showDescription===!1&&e.isAndroid?300:0;n(function(){e.showDescription=!e.scheduler.show},a)},e.goBack=function(){ionic.Platform.isIPad()?r.getSchedule({id:a.id}).then(function(a){"ok"==a.data.status&&(r.setData(a.data.schedule),o(a.data.schedule.ranges),e.scheduler.show=a.data.schedule.enabled,e.state.show=e.scheduler.show,e.schedulerData=i(e.scheduler.week[e.day||"mon"]),e.scheduler.changed=!1,e.scheduler.reset=!0)}):t.goBack()},e.setWeekType=function(){var a=e.scheduler.week;e.dayType&&(_.isEqual(a.mon,a.tue)&&_.isEqual(a.tue,a.wed)&&_.isEqual(a.wed,a.thu)&&_.isEqual(a.thu,a.fri)&&_.isEqual(a.fri,a.sat)&&_.isEqual(a.sat,a.sun)?(e.weekData=angular.copy(e.schedulerData),e.weekType=!0,e.dayType=!1):u(["parental_schedule_identical_warn","Abort","OK"]).then(function(a){d.confirm({title:a.parental_schedule_identical_warn,cssClass:"custom-popup",cancelText:a.Abort,okText:a.OK}).then(function(a){a&&(e.weekData=angular.copy(e.schedulerData),e.weekType=!0,e.dayType=!1)})}))},e.setDay=function(a){n(function(){e.day=a,e.schedulerData=i(e.scheduler.week[a])},100)},e.setDayType=function(){if(window.cordova&&l.setAnalytics("SchedulerDays"),e.weekType){var a=h("",e.weekData).split(";");e.scheduler.week.mon=angular.copy(a),e.scheduler.week.tue=angular.copy(a),e.scheduler.week.wed=angular.copy(a),e.scheduler.week.thu=angular.copy(a),e.scheduler.week.fri=angular.copy(a),e.scheduler.week.sat=angular.copy(a),e.scheduler.week.sun=angular.copy(a)}e.setDay("mon"),e.dayType=!0,e.weekType=!1},e.clearScheduler=function(){e.schedulerData=_.map(e.schedulerData,function(){return 0})},e.saveScheduler=function(){c.showLoading();var s="";for(var u in e.scheduler.week)e.dayType?u!==e.day&&(s+=h(u,i(e.scheduler.week[u]))):e.weekType&&(s+=h(u,e.schedulerData));e.dayType&&(s+=h(e.day,e.schedulerData)),r.setSchedule({deviceId:a.id,schedule:{enabled:e.scheduler.show,ranges:s}}).then(function(){ionic.Platform.isIPad()?n(function(){r.getSchedule({id:a.id}).then(function(a){"ok"==a.data.status&&(r.setData(a.data.schedule),o(a.data.schedule.ranges),e.scheduler.show=a.data.schedule.enabled,e.state.show=a.data.schedule.enabled,e.schedulerData=i(e.scheduler.week[e.day||"mon"]),e.scheduler.changed=!1,e.scheduler.reset=!0),c.hideLoading()})},1e3):(c.hideLoading(),t.goBack())},function(){console.log("Error while saving scheduler."),t.goBack(),c.showError()})}}]);