(function (KDApi, $, _) {
	function MyComponent(model) {
		this._setModel(model);
	}

	var deleteGanttIDArray=new Array();

	MyComponent.prototype = {
		_setModel: function (model) {
			this.model = model;
		},
		init: function (props) {
			console.log("----初始甘特图----", this.model, props);

			var paramsMap = new Object();

			paramsMap["action"]="GanttFunctionInitial";

			props.data=paramsMap;

			invokeGantt(this.model, props);
		},
		update: function (props) {
			console.log("----更新甘特图----", this.model, props);

			invokeGantt(this.model, props);

			if (props && props.data){
				this.model.invoke("GanttFunctionClearControlData", this.model.schemaId);
			}
		},
		destoryed: function () {
			console.log("----关闭甘特图----", this.model);
		}
	}

	var invokeGantt = function (model, props) {
		if (!props || !model) return;

		var paramsMap = props.data;

		var schemeID = model.schemaId;

		if (!paramsMap || typeof(paramsMap)!="object") return;

		if (!schemeID || typeof(schemeID)!="string") return;

		var action = paramsMap["action"];

		if (!action || typeof(action)!="string") return;

		if (action=="GanttFunctionInitial"){
			KDApi.loadFile("./bundle.js", model, null);

			KDApi.loadFile("./Header.umd.js", model, null);

			KDApi.templateFilePath("./ganttContent.html", model,null).then(
				result => {
					model.dom.innerHTML = result;
					KDApi.loadFile("./gantt.js", model, () => {
						$(model.dom).css("overflow", "visible");

						cc_gantt_model=model;
					});
				}
			);
		}else if (action=="GanttFunctionResetData"){
			app.tasks=[];
		}else if (action=="GanttFunctionAddData"){
			var datas = paramsMap["datas"];

			if (!datas || typeof(datas)!="object") return;

			var datasSize=datas.size;

			var dataObjs = new Array();

			for (var dataIndex=0;dataIndex<datasSize;dataIndex++){
				var data=datas.get(dataIndex);

				if (!data || typeof(data)!="object") continue;

				var propsSize=data.size;

				if (!propsSize || typeof(propsSize)!="number") continue;

				if (propsSize<=0) continue;

				var dataObj = new Object();

				for (var propIndex=0;propIndex<propsSize;propIndex++){
					var prop=data.get(propIndex);

					if (!prop || typeof(prop)!="object") continue;

					var propSize=prop.size;

					if (!propSize || typeof(propSize)!="number") continue;

					if (propSize!=2) continue;

					var propName=prop.get(0);

					var propValue=prop.get(1);

					if (!propName || typeof(propName)!="string") continue;

					var newPropValue;

					if (propName=="start"){
						newPropValue=!propValue || typeof(propValue)!="string"?null:propValue.length!=19?null:new Date(propValue);
					}else if (propName=="dependentOn"){
						if (!propValue || typeof(propValue)!="object") continue;

						var valueSize=propValue.size;

						if (!valueSize || typeof(valueSize)!="number") continue;

						if (valueSize<=0) continue;

						var propValues=new Array();

						for (var valueIndex=0;valueIndex<valueSize;valueIndex++){
							propValues[valueIndex]=propValue.get(valueIndex);
						}

						newPropValue=!propValues || propValues.size<=0?null:propValues;
					}else if (propName=="style"){
						if (!propValue || typeof(propValue)!="object") continue;

						var stylesSize=propValue.size;

						var styles=new Object();

						for (var styleIndex=0;styleIndex<stylesSize;styleIndex++){
							var style=propValue.get(0);

							if (!style || typeof(style)!="object") continue;

							var styleSelectorSize=style.size;

							if (!styleSelectorSize || typeof(styleSelectorSize)!="number") continue;

							if (styleSelectorSize!=2) continue;

							var styleSelectorName=style.get(0);

							var styleSelectorValue=style.get(1);

							if (!styleSelectorName || typeof(styleSelectorName)!="string") continue;

							if (!styleSelectorValue || typeof(styleSelectorValue)!="object") continue;

							var stylePropsSize=styleSelectorValue.size;

							if (!stylePropsSize || typeof(stylePropsSize)!="number") continue;

							if (stylePropsSize<=0) continue;

							var styleProps=new Object();

							for (var stylePropIndex=0;stylePropIndex<stylePropsSize;stylePropIndex++){
								var styleProp=styleSelectorValue.get(stylePropIndex);

								if (!styleProp || typeof(styleProp)!="object") continue;

								var stylePropSize=styleProp.size;

								if (!stylePropSize || typeof(stylePropSize)!="number") continue;

								if (stylePropSize!=2) continue;

								var stylePropName=styleProp.get(0);

								var stylePropValue=styleProp.get(1);

								if (!stylePropName || typeof(stylePropName)!="string" || !stylePropValue || typeof(stylePropValue)!="string") continue;

								styleProps[stylePropName]=stylePropValue;
							}

							if (styleProps){
								styles[styleSelectorName]=styleProps;
							}
						}

						newPropValue=!styles || typeof(styles)!="object"?null:styles;
					}else{
						newPropValue=propValue;
					}

					dataObj[propName]=newPropValue;
				}

				if (dataObj){
					dataObjs.push(dataObj);
				}
			}

			if (dataObjs){
				app.tasks=[];
				app.tasks.push.apply(app.tasks,dataObjs);
			}
		}else if (action=="GanttFunctionDeleteData"){
			var ids = paramsMap["ids"];

			if (!ids || typeof(ids)!="object") return;

			var idsSize=ids.size;

			if (!idsSize || typeof(idsSize)!="number") return;

			if (idsSize<=0) return;

			var idsArray=new Array();

			deleteGanttIDArray=[];

			for (var idIndex=0;idIndex<idsSize;idIndex++){
				deleteGanttIDArray[idIndex]=ids.get(idIndex);
			}

			if (!app.tasks || typeof(app.tasks)!="object") return;

			var tasksCount=app.tasks.length;

			if (!tasksCount || typeof(tasksCount)!="number") return;

			if (tasksCount<=0) return;

			app.tasks=app.tasks.filter((task) =>{
				if (!task || typeof(task)!="object") return false;

				var id = task.id;

				if (!id || typeof(id)!="number") return false;

				if (deleteGanttIDArray.indexOf(id)>=0){
					return false;
				}else{
					return true;
				}
			});
		}else if (action=="GanttFunctionUpdateData"){
			if (!app.tasks || typeof(app.tasks)!="object") return;

			var tasksCount=app.tasks.length;

			if (!tasksCount || typeof(tasksCount)!="number") return;

			if (tasksCount<=0) return;

			var datas = paramsMap["datas"];

			if (!datas || typeof(datas)!="object") return;

			var datasSize=datas.size;

			if (datasSize<=0) return;

			var datasMap = new Map();

			for (var dataIndex=0;dataIndex<datasSize;dataIndex++){
				var data=datas.get(dataIndex);

				if (!data || typeof(data)!="object") continue;

				var dataSize=data.size;

				if (!dataSize || typeof(dataSize)!="number") continue;

				if (dataSize!=2) continue;

				var dataID=data.get(0);

				var dataProps=data.get(1);

				if (!dataID || typeof(dataID)!="number" || !dataProps || typeof(dataProps)!="object") continue;

				var dataPropsSize=dataProps.size;

				if (dataPropsSize<=0) continue;

				datasMap.set(dataID,dataProps);
			}

			for (var taskIndex=0;taskIndex<tasksCount;taskIndex++){
				var task=app.tasks[taskIndex];

				if (!task || typeof(task)!="object") continue;

				var taskID=task.id;

				if (!taskID || typeof(taskID)!="number") continue;

				if (!datasMap.has(taskID)) continue;

				var dataProps=datasMap.get(taskID);

				if (!dataProps || typeof(dataProps)!="object") continue;

				var dataPropsSize=dataProps.size;

				if (dataPropsSize<=0) continue;

				for (var dataPropIndex=0;dataPropIndex<dataPropsSize;dataPropIndex++){
					var dataProp=dataProps.get(dataPropIndex);

					if (!dataProp || typeof(dataProp)!="object") continue;

					var dataPropSize=dataProp.size;

					if (!dataPropSize || typeof(dataPropSize)!="number") continue;

					if (dataPropSize!=2) continue;

					var dataPropName=dataProp.get(0);

					var dataPropValue=dataProp.get(1);

					if (!dataPropName || typeof(dataPropName)!="string" || !dataPropValue) continue;

					if (dataPropName=="id" || dataPropName=="duration" || dataPropName=="progress" || dataPropName=="parentId"){
						if (typeof(dataPropValue)!="number") continue;

						if (dataPropName=="id" || dataPropName=="parentId"){
							continue;
						}else if (dataPropName=="duration"){
							app.tasks[taskIndex]["durationMs"]=dataPropValue * 1000;
						}
					} else if (dataPropName=="label" || dataPropName=="type" || dataPropName=="start"){
						if (typeof(dataPropValue)!="string") continue;

						if (dataPropName=="start"){
							continue;
						}
					}else if (dataPropName=="style" || dataPropName=="dependentOn"){
						if (typeof(dataPropValue)!="object") continue;

						continue;
					}else{
						if (typeof(dataPropValue)!="string" && typeof(dataPropValue)!="number") continue;
					}

					app.tasks[taskIndex][dataPropName]=dataPropValue;
				}
			}
		}
	}

	console.log("----注册甘特图----");

	KDApi.register("gantt", MyComponent);
})(window.KDApi, jQuery, _);
