/**
 * 
 */
define(
	["dojo/_base/declare",
	    "dijit/_WidgetBase",
	    "dijit/_TemplatedMixin",
	    "dijit/_WidgetsInTemplateMixin",
	    "gantt/dhtmlxgantt",
	    "dojo/_base/array",
	    "dojo/when",
	    "dojo/store/Memory",
	    "dojo/store/Observable",
	    "dojo/_base/xhr",
	    "dojo/date/stamp",
	    "dijit/Dialog",
	    "dojo/text!./templates/Gantt.html",
	],
	function(declare, _WidgetBase, _TemplatedMixin,_WidgetsInTemplateMixin, Calendar,array,when,Memory,Observable,xhr,stamp,Dialog,template){
		return declare([_WidgetBase, _TemplatedMixin,_WidgetsInTemplateMixin], {	
			templateString: template,
			id:'gantt',
			cal_data:[],
			cal_store:null,
			url: null,
			project_id: null,
			context_id: null,
			
			
			constructor: function(args){
	            declare.safeMixin(this,args);
				
	            console.log("GANTT Constructor");
            
	            //setup gantt
				gantt.config.scale_unit = "week";
				gantt.config.date_scale = "%M %d";
				gantt.config.min_column_width = 40;
				gantt.config.date_grid = "%d-%m-%Y";
				gantt.config.scale_height = 54;
				gantt.config.drag_move = false;
				gantt.config.open_tree_initially = true;
				gantt.config.autosize = true;
				gantt.config.autofit = true;
				
				//custom param passing
				gantt.config.project_id = this.project_id;
				gantt.config.parent_context_id = this.parent_context_id;
				gantt.config.context_id = this.context_id;
	            				
				gantt.config.details_on_create = true;
				if(!gantt.checkEvent("onBeforeLightbox")){
					gantt.attachEvent("onBeforeLightbox", function(id) {
						var task = gantt.getTask(id);
						if(task.$new){
							gantt.deleteTask(id);
							KPWindowShowOverlay('cheyenne?cddid=xam.instance_addedit&P_CONTEXT_ID='+gantt.config.context_id+'&P_ACTION=add&w_proj_t='+gantt.config.project_id+'&cmode=kpwindow_dialogborder_overlay');
							return false;
						}
						return true;
					});
				}
				
				if(!gantt.checkEvent("onBeforeTaskSelected")){
					gantt.attachEvent("onBeforeTaskSelected", function(id) {
						var task = gantt.getTask(id);
						if(gantt.getParent(id) == 0){
							//project
							KPWindowShowOverlay('cheyenne?cddid=xam.instance_addedit&P_CONTEXT_ID='+gantt.config.parent_context_id+'&P_INSTANCE_ID='+gantt.config.project_id+'&P_NAV_ID=&P_TASK_ID=&cmode=kpwindow_dialogborder_overlay');
						} else {
							//taks
							console.log(task);
							KPWindowShowOverlay('cheyenne?cddid=xam.instance_addedit&P_CONTEXT_ID='+gantt.config.context_id+'&P_INSTANCE_ID='+task.id+'&P_NAV_ID=&P_TASK_ID=&cmode=kpwindow_dialogborder_overlay');
						}
					});
				}
			},
			
			startup: function(){
				console.log("GANTT Startup");
				console.log("URL",this.url);
				gantt.init(this.ganttNode);
				gantt.attachEvent("onTaskDblClick", function(id,e){
					this.showEditOverlay(id);
				});
				
				var results = xhr("GET", {
					url: this.url,
					handleAs: "json"
				}).then(function(data){
					console.log("GANTT data", data)
					array.forEach(data.item,function(item){
						var cal_item={};
						cal_item.id=item.id;
						cal_item.text=item.name;
						cal_item.desc=item.desc;
						cal_item.start_date=new Date(item.date_from);
						cal_item.duration=gantt.calculateDuration(cal_item.start_date,new Date(item.date_to));
						
						console.log("Item",cal_item,item.parent);
						
						gantt.addTask(cal_item,item.parent);
						
					});
					gantt.render();	
				});
				
			},
			
			showEditOverlay: function(id){
				
			},
			
			destroy: function(){
				gantt.clearAll();
			}
		});
});