define(
	["dojo/_base/declare",
	    "dijit/_WidgetBase",
	    "dijit/_TemplatedMixin",
	    "dijit/_WidgetsInTemplateMixin",
	    "scheduler/dhtmlxscheduler",
	    "dojo/_base/array",
	    "dojo/when",
	    "dojo/store/Memory",
	    "dojo/store/Observable",
	    "dojo/request/xhr",
	    "dojo/date/stamp",
	    "dijit/Dialog",
	    "dojo/text!./templates/Scheduler.html",
	],
	function(declare, _WidgetBase, _TemplatedMixin,_WidgetsInTemplateMixin, dhtmlxscheduler, array,when,Memory,Observable,xhr,stamp,Dialog,template){
		return declare("chy.widget.Scheduler",[_WidgetBase, _TemplatedMixin,_WidgetsInTemplateMixin], {	
			templateString: template,
			firstHour: 8,
			lastHour: 18,
					
			constructor: function(args){
				this.inherited(arguments);
	            declare.safeMixin(this, args);
			},
			
			startup: function(){
				this.inherited(arguments);
				
				console.log("Scheduler Startup", this);
				scheduler = Scheduler.getSchedulerInstance();
				scheduler.config.url = this.url;
				scheduler.config.sectionurl = this.sectionurl;
				scheduler.config.statusfilter = this.statusfilter;
				
				var _this = this;
				require([ "scheduler/ext/dhtmlxscheduler_timeline","scheduler/ext/dhtmlxscheduler_tooltip"], function(a,b){
					scheduler.locale.labels.timelineday_tab ="Day";
					scheduler.locale.labels.timelineweek_tab ="Week";
					scheduler.locale.labels.timelinemonth_tab ="Month";
					scheduler.config.xml_date="%Y-%m-%d %H:%i";
					scheduler.config.fix_tab_position = false;
					scheduler.config.drag_resize = false;
					scheduler.config.drag_move = false;
					scheduler.config.drag_create = false;
										
					//load tickets on y-axis
					var sections=[];
					var datefrom = new Date();
					var dateto = new Date();
					dateto.setDate(dateto.getDate()+1);
					xhr(scheduler.config.sectionurl+'&P_DATE_FROM='+datefrom.getDate()+'-'+(datefrom.getMonth()+1)+'-'+datefrom.getFullYear()+'&P_DATE_TO='+dateto.getDate()+'-'+(dateto.getMonth()+1)+'-'+dateto.getFullYear()+'&P_FILTER='+scheduler.config.statusfilter, {
						handleAs: "json",
						sync: true
					  }).then(function(data){
						 if(typeof data.data != 'undefined'){
							sections=data.data;
						 }
					  });
					  scheduler.serverList("sections",sections);
					
					//dayview
					scheduler.createTimelineView({
						 name:"timelineday",
						 x_unit:"minute",//measuring unit of the X-Axis.
						 x_date:"%H:%i", //date format of the X-Axis
						 x_step:60,      //X-Axis step in 'x_unit's
						 x_size:(_this.lastHour - _this.firstHour), //X-Axis length specified as the total number of 'x_step's
						 x_start:_this.firstHour,     //X-Axis offset in 'x_unit's
						 x_length:24,    //number of 'x_step's that will be scrolled at a time
						 y_unit:scheduler.serverList("sections"), //sections of the view (titles of Y-Axis)
						 y_property:"section_id", //mapped data property
						 render:"bar"             //view mode
					});
					
					//weekview
					scheduler.createTimelineView({
						 name:"timelineweek",
						 x_unit:"day",//measuring unit of the X-Axis.
						 x_date:"%d-%m-%Y", //date format of the X-Axis
						 x_step:1,      //X-Axis step in 'x_unit's
						 x_size:7,      //X-Axis length specified as the total number of 'x_step's
						 x_start:0,     //X-Axis offset in 'x_unit's
						 x_length:1,    //number of 'x_step's that will be scrolled at a time
						 y_unit:scheduler.serverList("sections"), //sections of the view (titles of Y-Axis)
						 y_property:"section_id", //mapped data property
						 render:"bar",             //view mode
						 first_hour:_this.firstHour,
						 last_hour:_this.lastHour
					});
					//gray weekend day cells
					scheduler.templates.timelineweek_cell_class = function(evs, date, section) {
					   if(date.getDay() == 0 || date.getDay() == 6){
						   return "schedulerWeekendDayCell";
					   }
					   return "schedulerWeekDayCell";
					}
					//oprional ignore weekend days
					/*scheduler.ignore_timelineweek = function(date){
						if(date.getDay() == 0 || date.getDay() == 6){
							return true;
						else	
							return false;
					};*/
										
					//month view
					scheduler.createTimelineView({
						 name:"timelinemonth",
						 x_unit:"day",//measuring unit of the X-Axis.
						 x_date:"%d", //date format of the X-Axis
						 x_step:1,      //X-Axis step in 'x_unit's
						 x_size:31,      //X-Axis length specified as the total number of 'x_step's
						 x_start:0,     //X-Axis offset in 'x_unit's
						 x_length:14,    //number of 'x_step's that will be scrolled at a time
						 y_unit:scheduler.serverList("sections"), //sections of the view (titles of Y-Axis)
						 y_property:"section_id", //mapped data property
						 render:"bar",             //view mode
						 first_hour:_this.firstHour,
						 last_hour:_this.lastHour
					});
					//gray weekend day cells
					scheduler.templates.timelinemonth_cell_class = function(evs, date, section) {
					   if(date.getDay() == 0 || date.getDay() == 6){
						   return "schedulerWeekendDayCell";
					   }
					   return "schedulerWeekDayCell";
					}
										
					scheduler.init('scheduler_here',new Date(),"timelineday");
					//load initial events
					scheduler.load(scheduler.config.url+'&P_DATE_FROM='+datefrom.getDate()+'-'+(datefrom.getMonth()+1)+'-'+datefrom.getFullYear()+'&P_DATE_TO='+dateto.getDate()+'-'+(dateto.getMonth()+1)+'-'+dateto.getFullYear()+'&P_FILTER='+scheduler.config.statusfilter, "json");
					
					//load action on click
					scheduler.attachEvent("onClick", function(id, e) {
						console.log("event click ", id, e);
						KPWindowLoadParseStr('https://eden.benp.nl/tamcom_haho/cheyenne?cddid=xam.instance_addedit&P_INSTANCE_ID='+id+'&P_CONTEXT_ID=1027&P_NAV_ID=1021','kpwindowb2','kpwindowb2'); 							
						return false;
					});
					
					//disable drag and drop
					scheduler.attachEvent("onBeforeDrag", function(){
						return false;
					})
					
					//get event ccustom event color
					scheduler.templates.event_class = function(start, end, ev){
						 return ev.status;
					}
					
					//update data onchage
					scheduler.firstUpdate = true;
					scheduler.attachEvent("onViewChange", function (new_mode , new_date){
						console.log("view change", new_mode, new_date, this);
						var datefrom = new_date;
						var dateto = new Date(new_date.getTime());;
						if(new_mode.indexOf('day') > -1){
								dateto.setDate(dateto.getDate()+1);
						}
						if(new_mode.indexOf('week') > -1){
								dateto.setDate(dateto.getDate()+7);
								console.log('set week date',dateto);
						}
						if(new_mode.indexOf('month') > -1){
								dateto.setDate(dateto.getDate()+31);
						}
						//first update will update the y-axis (sections), this will trigger a second onViewChange event.
						if(this.firstUpdate){
							this.firstUpdate = false;
							scheduler.clearAll();
							xhr(scheduler.config.sectionurl+'&P_DATE_FROM='+datefrom.getDate()+'-'+(datefrom.getMonth()+1)+'-'+datefrom.getFullYear()+'&P_DATE_TO='+dateto.getDate()+'-'+(dateto.getMonth()+1)+'-'+dateto.getFullYear()+'&P_FILTER='+scheduler.config.statusfilter, {
								handleAs: "json",
								sync: true
							  }).then(function(data){
								if(typeof data.data != 'undefined'){
									scheduler.updateCollection("sections",data.data);
								 } else {
									scheduler.updateCollection("sections",[]);
								 }
								 scheduler.updateView();
							  });
						} else {
							//on second event load event data.
							this.firstUpdate = true;
							scheduler.load(this.config.url+'&P_DATE_FROM='+datefrom.getDate()+'-'+(datefrom.getMonth()+1)+'-'+datefrom.getFullYear()+'&P_DATE_TO='+dateto.getDate()+'-'+(dateto.getMonth()+1)+'-'+dateto.getFullYear()+'&P_FILTER='+scheduler.config.statusfilter, "json");							
						}
					});
				});
			},
			
			destroy: function(){
				
			}
		});
});