Ext.define("Ext.ux.file.view.DropZone", {

	extend: "Ext.window.Window",
	alias: "widget.filedropzone",

	layout: "fit",
	treeHive: undefined,
	portletCount: 0,
	a_portlets: [],
	modal: true,
	constrain: true,
	resizable: false,
	preventHeader: false,
	closable: false,
	dropFn: undefined,
	overZone: false,
	closeAction: "hide",
	extensions: undefined,
	title: ip2.i18n({
		da: "Dropzone oversigt",
		en: "Dropzone overview"
	}),

	initComponent: function(config) {
		var i_self = this;
		i_self.addListners();
		i_self.callParent(config);
		i_self.portlet = i_self.createPortlet();
		i_self.add(i_self.portlet);
		i_self.createTreeHives(config);
	},

	createTreeHives: function () {
		var i_self = this;
		var stack = [];
		i_self.treeHive = Ext.ux.file.util.Handler.getUITree({
			useSystem: false
		});

		var store = i_self.treeHive.getStore();

		store.on({
			load: {
				fn: function(store, a_records, b_success) {
					ip2.each(a_records, function(o_record) {
						o_record.set("allowDrag", false);

						if (!o_record.get("hasChild")) {
							o_record.set("loaded", true);
						}
						else {
							stack.push(0);
							o_record.expand();
						}
					});
					stack.pop();
					if (stack.length == 0) {
						var root = i_self.treeHive.getRootNode();
						i_self.a_portlets = [];
						i_self.portletCount = 0;
						if (i_self.portlet.items) {
							i_self.portlet.removeAll(true);
						}
						root.cascadeBy(function(node) {
							if (!node.isRoot() && node.get("allowDrop")) {
								i_self.portletCount++;
								var s_path = ip2.cleanPath(node.get("path")) + "/";
								var text = "";
								if (!node.parentNode.isRoot()) {
									var parent = node.parentNode;
									while (!parent.isRoot() && parent.get("allowDrop")) {
										text = parent.get("text") + "/" + text;
										parent = parent.parentNode;
									}
									text = text + node.get("text");
								}
								else {
									text = node.get("text");
								}
								var item = i_self.createPortletItem(s_path, text);
								i_self.a_portlets.push(item);
							}
						});
						i_self.portlet.add(i_self.createColumns());
						i_self.addItems();
					}
				}
			}
		});
	},

	addItems: function() {
		var i_self = this;
		i_self.index = 0;
		var columns = i_self.portlet.items.items;
		ip2.each(i_self.a_portlets, function(item) {
			columns[i_self.index].add(item);
			i_self.index++;
			if (i_self.index == columns.length) {
				i_self.index = 0;
			}
		});
	},

	createColumns: function() {
		var i_self = this;
		var a_columns = [];

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

		for (var i = 0; i < i_self.portletCount; i++) {
			var column = {
				xtype: "container",
				flex: 1,
				border: 0,
				layout: {
					type: "hbox",
					padding: 5,
					align: "stretch"
				},
				items: []
			};
			a_columns.push(column);
		}

		return a_columns;
	},

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

			},
			style: {
				"position": "absolute",
				"padding": "5px",
				"top": "0px",
				"left": "0px",
				"right": "0px",
				"bottom": "0px",
				"font-size": "5px"
			},
			items: []
		});
		return i_portlet;
	},

	createPortletItem: function(s_path, text) {
		var i_self = this;
		var item = {
			xtype: "container",
			border: 0,
			draggable: false,
			closable: false,
			collapsible: false,
			layout: "fit",
			flex: 1,
			items: [
				{
					xtype: "gallerydropbox",
					flex: 1,
					dropText: text + "</br><small><small>" + ip2.i18n({
						da: "Drop for at tilføje filer",
						en: "Drop to add files"
					}) + "</small></small>",
					path: s_path,
					extensions: i_self.extensions != undefined ? i_self.extensions : Ext.ux.file.util.Handler.extensions,
					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, o_event) {
								var plugin = i_dropbox.getDDPlugin();
								if (plugin.enableDrop) {
									var msg = ip2.i18n({
										da: "Kontrolerer filer....",
										en: "Checking files..."
									});
									i_self.setLoading(msg, true);
									Ext.ux.file.util.Handler.checkFiles(i_dropbox.path, 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(cancel, a_records) {
												b_cancel = cancel;
												ip2.each(a_records, function(o_record) {
													a_result.push(o_record);
												});
												if (!b_create) {
													i_self.setLoading(false);
													i_self.onDrop(a_result);
												}
												else {
													var msg = ip2.i18n({
														da: "Opretter filer....",
														en: "Creating files..."
													});
													i_self.setLoading(msg, true);
												}
											});
										}
										if (a_create.length > 0) {
											b_create = true;
											if (!b_update) {
												i_self.setLoading(false);
												var msg = ip2.i18n({
													da: "Opretter filer....",
													en: "Creating files..."
												});
												i_self.setLoading(msg, true);
											}
											Ext.ux.file.util.Handler.createRecords(i_dropbox.path, a_create, false, function(a_records) {
												b_create = false;
												ip2.each(a_records, function(o_record) {
													a_result.push(o_record);
												});
												if (!b_update) {
													i_self.setLoading(false);
													i_self.onDrop(a_result);
												}
												if (b_cancel) {
													i_self.setLoading(false);
													i_self.onDrop(a_result);
												}
											});
										}
									});
								}
							}
						},
						dragenter: {
							fn: function(i_dropbox, o_event) {
								i_self.overZone = true;
								var plugin = i_dropbox.getDDPlugin();
								var items = o_event.dataTransfer.items || o_event.dataTransfer.files;
								if (i_dropbox.maxItems) {
									if (items.length > i_dropbox.maxItems && i_dropbox.maxItems != -1) {
										i_dropbox.addCls(plugin.failureCls);
										plugin.enableDrop = false;
									}
								}
							}
						},
						dragleave: {
							fn: function(i_dropbox, o_event) {
								var plugin = i_dropbox.getDDPlugin();
								plugin.enableDrop = true;
								i_self.overZone = false;
							}
						}
					}
				}
			]
		};
		return item;
	},

	onDrop: function(a_records) {
		var i_self = this;
		i_self.hide();
		i_self.dropFn(a_records);
	},

	addListners: function() {
		var i_self = this;
		Ext.Object.merge(i_self, {
			listeners: {
				afterrender: function(item) {
					i_self.dragging = 0;
					document.body.addEventListener('drop', function(o_event) {
						if (!i_self.overZone) {
							i_self.hide();
						}
					});
					i_self.mon(i_self.el, {
						dragenter: function (m_event) {
							i_self.dragging++;
						}
					});
					i_self.mon(i_self.el, {
						dragleave: function (m_event) {
							i_self.dragging--;
							if (i_self.dragging === 0) {
								if (i_self.dragging === 0) {
									i_self.hide();
								}
							}
						}
					});
				},
				beforeshow: function(item) {
					var o_size = Ext.ux.file.util.Handler.getWindowSize();
					o_size.width = o_size.width * 0.9;
					o_size.height = o_size.height * 0.9;
					i_self.setSize(o_size);

					var i_portlet = i_self.portlet;

					var f_layout = i_portlet.doLayout || i_portlet.updateLayout;

					f_layout.call(i_portlet);
				},
				show: function() {
					i_self.overZone = false;
					i_self.dragging = 0;
				}
			}
		});
	}
});
