Ext.define("Ext.ux.grid.DropZone", {
	extend: "Ext.container.Container",
	alias: [
		"widget.ux.grid.dropzone"
	],
	layout: "fit",
	config: {
		grid: null,
		entity: {},
		overZone: false,
		dragging: 0,
		zone: null,
		zones: [],
		wrapperComponent: null
	},
	dropFn: Ext.emptyFn,
	style: {
		"position": "absolute",
		"padding": "5px",
		"top": "0px",
		"left": "0px",
		"right": "0px",
		"bottom": "0px",
		"font-size": "5px"
	},
	listeners: {
		afterrender: {
			fn: function() {
				var i_self = this;
				i_self.setDragging(0);

				var i_dropzone = i_self.getZone();
				var i_container = i_self.getWrapperComponent();

				i_self.mon(Ext.getBody(), "dragenter", function(i_event) {
					var n_dragging = i_self.getDragging();
					if (n_dragging === 0) {
						var i_el = i_dropzone.getEl();
						if (i_el.dom) {
							i_el.setStyle("display", "inline");
							i_el.setStyle("z-index", "1");
						}

						if (i_container) {

							var o_size = i_container.getSize();

							i_self.setSize(o_size.width, o_size.height);
							i_dropzone.setSize(o_size.width, o_size.height);
							o_size = i_self.getSize();
							i_el.setSize(o_size);
						}
					}

					n_dragging++;
					i_self.setDragging(n_dragging);
				});

				i_self.mon(Ext.getBody(), "dragleave", function(i_event) {

					var n_dragging = i_self.getDragging();
					n_dragging--;

					var i_el = i_dropzone.getEl();
					if (i_el.dom && n_dragging === 0) {
						i_el.setStyle("display", "none");
						i_el.setStyle("z-index", "0");
					}

					i_self.setDragging(n_dragging);
				});

				document.body.addEventListener("drop", function(o_event) {
					var i_el = i_dropzone.getEl();
					if (i_el.dom) {
						i_el.setStyle("display", "none");
					}
					i_self.setDragging(0);
				});
			}
		},
		dragenter: {
			element: "el",
			fn: function (i_event) {

				var i_self = this.component;

				var n_dragging = i_self.getDragging();
				n_dragging++;
				i_self.setDragging(n_dragging);
			}
		},
		dragleave: {
			element: "el",
			fn: function (i_event) {

				var i_self = this.component;

				var n_dragging = i_self.getDragging();
				n_dragging--;
				i_self.setDragging(n_dragging);
			}
		},
		drop: {
			fn: function(i_container, a_result, o_config) {

				var i_self = this;
				var i_dropzone = i_self.getZone();
				i_self.setDragging(0);
				var i_el = i_dropzone.getEl();
				if (i_el.dom) {
					i_el.setStyle("display", "none");
					i_el.setStyle("z-index", "0");
				}

				var s_value = "";
				var a_join = [];

				Ext.Array.each(a_result, function(o_value) {

					var s_path = o_value.FILPATH || o_value.PATH;
					var s_name = o_value.FILNAME || o_value.NAME;

					a_join.push(s_path);
					a_join.push(s_name);
				});

				s_value = ip2.cleanPath(a_join.join("/"));

				i_self.dropFn(s_value, o_config);
			}
		}
	},
	initComponent: function() {

		var i_self = this;
		i_self.callParent(arguments);

		var a_zones = i_self.getZones();
		var i_grid = i_self.getGrid();

		if (i_grid) {

			var s_dmd = i_grid.dmdName;
			var o_entity = i_self.getEntity();
			var s_view = i_grid.uiName;
			var o_drop = o_entity.ui.grids[s_view].drop || {
				entities: []
			};

			Ext.Array.each(o_drop.entities, function(o_config) {

				var o_dmd = ip2.resource.get(o_config.dmd);

				var a_entities = o_dmd.model.entities;

				var o_entity = Ext.Array.findBy(a_entities, function(o_value) {
					return o_value.name == o_config.name;
				});

				Ext.apply(o_config, {
					title: o_entity.title,
					dmdName: s_dmd
				});
			});

			i_self.add(i_self.createPortlet(o_drop.entities));
		}
		else {
			i_self.add(i_self.createPortlet(a_zones));
		}

		return i_self;
	},

	createPortlet: function(a_entires) {

		var i_self = this;

		var i_portlet = Ext.widget("container", {
			border: 0,
			layout: {
				type: "vbox",
				padding: 0,
				align: 'stretch'

			},
			style: {
				"background-color": a_entires.length ? "white" : "transparent",
				"position": "absolute",
				"padding": "5px",
				"top": "0px",
				"left": "0px",
				"right": "0px",
				"bottom": "0px",
				"font-size": "5px",
				"display": "none",
				"opacity": "0.9"
			},
			items: i_self.createColumns(a_entires)
		});

		i_self.setZone(i_portlet);

		return i_portlet;
	},

	createColumns: function(a_entires) {

		var i_self = this;

		var a_columns = [];

		a_entires = Ext.Array.filter(a_entires, function(o_value) {
			return o_value.path;
		});

		var n_count = a_entires.length;

		if (n_count % 2 == 0) {
			n_count = Math.ceil(n_count / 6);
		}
		else {
			n_count = Math.ceil(n_count / 5);
		}

		var a_items = [];

		Ext.Array.each(a_entires, function(o_config) {

			var i_result = i_self.createPortletItem(o_config);
			if (i_result) {
				a_items.push(i_result);
			}
		});

		if (a_items.length > 0) {
			var i_row = Ext.widget("container", {
				flex: 1,
				border: 0,
				layout: {
					type: "hbox",
					padding: 5,
					align: "stretch"
				},
				items: a_items
			});

			a_columns.push(i_row);
		}

		return a_columns;
	},

	createPortletItem: function(o_config) {

		var i_self = this;
		var i_portletItem = Ext.widget("container", {
			border: 0,
			draggable: false,
			closable: false,
			collapsible: false,
			layout: "fit",
			flex: 1,
			items: [
				{
					xtype: "gallerydropbox",
					flex: 1,
					path: o_config.path,
					dropConfig: o_config,
					dropText: ip2.i18n(o_config.title) + "</br><small><small>" +
					ip2.i18n(
						{
							da: "Drop for at oprette record ",
							en: "Drop to create record"
						}
					) + "</small></small>",
					extensions: ["*"],
					maxItems: o_config.maxItems || 1,
					style: {
						"position": "absolute",
						"padding": "5px",
						"top": "0px",
						"left": "0px",
						"right": "0px",
						"bottom": "0px",
						"font-size": "25px"
					},
					listeners: {
						drop: {
							fn: function(i_dropbox, a_files, i_event) {

								var i_plugin = i_dropbox.getDDPlugin();

								if (i_plugin.enableDrop) {

									var s_message = ip2.i18n({
										da: "Kontrolerer filer....",
										en: "Checking files..."
									});

									i_self.setLoading(s_message, true);

									var s_uploadpath = i_dropbox.path;

									Ext.ux.file.util.Handler.checkFiles(s_uploadpath, a_files, function(o_result) {
										i_self.setLoading(false);
										var a_update = o_result.update;
										var a_create = o_result.create;
										var b_update = false;
										var b_cancel = false;
										var b_create = false;
										var a_result = [];
										if (a_update.length > 0) {
											b_update = true;
											Ext.ux.file.util.Handler.getUIConflickWindow(i_dropbox.getEl(),
												i_self,
												i_dropbox.path,
												a_update,
												function(b_cancel, a_override, a_rename) {

													b_cancel = b_cancel;

													if (!b_cancel) {
														b_cancel = (a_override.length === 0 && a_rename.length === 0);
													}

													if (b_cancel) {
														if (!b_create) {
															i_self.setLoading(false);
															i_self.fireEvent("drop", i_self, a_result, i_dropbox.dropConfig);
														}
														return;
													}
													var a_complete = a_override.concat(a_rename);

													var a_temp = [];

													Ext.Array.each(a_complete, function(o_record) {
														a_temp.push(o_record.file);
													});

													s_message = ip2.i18n({
														da: "Overskriver fil....",
														en: "Overwriting file..."
													});

													i_self.setLoading(s_message);

													if (Ext.ux.file.util.Handler.hasFileManager()) {
														Ext.ux.file.util.Handler.updateRecordsFileInfo(s_uploadpath, a_temp, function(a_records) {
															Ext.Array.each(a_records, function(i_record) {
																a_result.push(i_record.data);
															});
															if (!b_create) {
																i_self.setLoading(false);
																i_self.fireEvent("drop", i_self, a_result, i_dropbox.dropConfig);
															}
															else {
																s_message = ip2.i18n({
																	da: "Opretter filer....",
																	en: "Creating files..."
																});
																i_self.setLoading(s_message);
															}
														});
													}
													else {
														Ext.ux.file.util.Handler.uploadFiles(s_uploadpath, a_temp, false, function(a_values) {
															Ext.Array.each(a_values, function(o_value) {
																a_result.push({
																	PATH: s_uploadpath,
																	NAME: o_value.file.name
																});
															});
															if (!b_create) {
																i_self.setLoading(false);
																i_self.fireEvent("drop", i_self, a_result, i_dropbox.dropConfig);
															}
															else {
																s_message = ip2.i18n({
																	da: "Opretter filer....",
																	en: "Creating files..."
																});
																i_self.setLoading(s_message);
															}
														});
													}
												},
												false
											);
										}
										if (a_create.length > 0) {
											b_create = true;
											if (!b_update) {
												i_self.setLoading(false);
												s_message = ip2.i18n({
													da: "Opretter filer....",
													en: "Creating files..."
												});
												i_self.setLoading(s_message);
											}

											if (Ext.ux.file.util.Handler.hasFileManager()) {
												Ext.ux.file.util.Handler.createRecords(s_uploadpath, a_create, false, function(a_records) {
													b_create = false;
													Ext.Array.each(a_records, function(i_record) {
														a_result.push(i_record.data);
													});
													if (!b_update) {
														i_self.setLoading(false);
														i_self.fireEvent("drop", i_self, a_result, i_dropbox.dropConfig);
													}
													if (b_cancel) {
														i_self.setLoading(false);
														i_self.fireEvent("drop", i_self, a_result, i_dropbox.dropConfig);
													}
												});
											}
											else {
												Ext.ux.file.util.Handler.uploadFiles(s_uploadpath, a_create, false, function(a_values) {
													b_create = false;
													Ext.Array.each(a_values, function(o_value) {
														a_result.push({
															PATH: s_uploadpath,
															NAME: o_value.file.name
														});
													});
													if (!b_update) {
														i_self.setLoading(false);
														i_self.fireEvent("drop", i_self, a_result, i_dropbox.dropConfig);
													}
													if (b_cancel) {
														i_self.setLoading(false);
														i_self.fireEvent("drop", i_self, a_result, i_dropbox.dropConfig);
													}
												});
											}
										}
									});
								}
								i_self.setDragging(0);
								var i_dropzone = i_self.getZone();
								var i_el = i_dropzone.getEl();
								if (i_el.dom) {
									i_el.setStyle("display", "none");
									i_el.setStyle("z-index", "0");
								}
							}
						},
						dragenter: {
							fn: function(i_dropbox, o_event) {

								var i_plugin = i_dropbox.getDDPlugin();
								var a_items = o_event.dataTransfer.items || o_event.dataTransfer.files;
								var s_dmd = o_config.dmdName || "";
								i_self.setOverZone(true);

								if (s_dmd) {
									if (!(ip2.isAccessible("app." + s_dmd + ".data.create") ||
										ip2.isAccessible("app." + s_dmd + ".data." + i_self.entity.name + ".create")) ||
										i_dropbox.maxItems < a_items.length) {
										i_dropbox.addCls(i_plugin.failureCls);
										i_plugin.enableDrop = false;
									}
								}
								else {
									if (!i_dropbox.maxItems < a_items.length) {
										i_dropbox.addCls(i_plugin.failureCls);
										i_plugin.enableDrop = false;
									}
								}
							}
						},
						dragleave: {
							fn: function(i_dropbox, o_event) {
								var i_plugin = i_dropbox.getDDPlugin();
								i_plugin.enableDrop = true;
								i_self.setOverZone(false);
							}
						}
					}
				}
			]
		});

		return i_portletItem;
	}
});
