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

	extend: "Ext.window.Window",
	alias: ["widget.fileconflict"],

	//properties
	modal: true,
	width: 800,
	height: 600,
	layout: "fit",
	files: [],
	path: "",
	isOk: false,
	isCancel: false,
	closable: false,
	a_rename: [],
	a_override: [],
	okFn: Ext.emptyFn,
	cancelFn: Ext.emptyFn,

	initComponent: function(config) {
		var i_cmp = this;
		i_cmp.addListeners();
		i_cmp.callParent(config);
		i_cmp.add(i_cmp.createMain());
		var title = ip2.i18n({
			da: "Fil konflikt",
			en: "File conflict"
		});
		i_cmp.setTitle(title);
		i_cmp.getSuggestions();
	},

	addListeners: function() {
		var i_cmp = this;
		Ext.Object.merge(i_cmp, {
			listeners: {
				close: function() {
					if (i_cmp.isOk) {
						var a_override = i_cmp.a_override;
						var a_rename = i_cmp.a_rename;
						i_cmp.okFn(a_override, a_rename);
					}
					else if (i_cmp.isCancel) {
						i_cmp.cancelFn();
					}
					else {
						i_cmp.cancelFn();
					}
				}
			}
		});
	},

	createMain: function() {
		var i_cmp = this;
		var container = {
			xtype: "form",
			layout: "border",
			items: [
				i_cmp.createNorth(),
				i_cmp.createCenter()
			],
			buttons: i_cmp.createButtons()
		};
		return container;
	},

	createButtons: function() {
		var i_cmp = this;
		var a_buttons = [
			{
				text: ip2.i18n({
					da: "Ok",
					en: "Ok"
				}),
				handler: function () {
					i_cmp.isOk = true;
					i_cmp.isCancel = false;
					var i_form = i_cmp.down("#formRecords");

					var a_files = i_cmp.files;
					i_cmp.a_override = [];
					i_cmp.a_rename = [];

					ip2.each(a_files, function (o_file, n_index) {

						var extension = o_file.name.split(".").pop();
						var s_fileName = o_file.name.replace("." + extension, "");
						var i_record = i_form.down("#record_" + n_index);
						var b_overwrite = i_record.down("#i_" + n_index + "_replace").getValue();
						var b_rename = i_record.down("#i_" + n_index + "_rename").getValue();
						if (b_overwrite) {
							var o_record = {
								file: o_file,
								index: n_index
							};
							i_cmp.a_override.push(o_record);
						}
						if (b_rename) {
							var s_suggested = i_record.down("#i_" + n_index + "_suggested").getValue();
							var o_record = {
								file: o_file,
								index: n_index,
								suggestedName: s_suggested
							};
							i_cmp.a_rename.push(o_record);
						}
					});

					if (i_cmp.a_rename.length > 0) {
						var msg = ip2.i18n({
							da: "Validerer navne",
							en: "Validating names"
						});
						i_cmp.setLoading(msg, true);
						Ext.ux.file.util.Handler.validateSuggestions(i_cmp.a_rename, i_cmp.path, function(a_invalid) {
							i_cmp.setLoading(false);
							if (a_invalid.length > 0) {
								ip2.each(a_invalid, function(o_record) {
									var i_field = i_form.down("#i_" + o_record.index + "_suggested");
									var msg = ip2.i18n({
										da: "Navnet findes allerede",
										en: "Name allready exists"
									});
									i_field.markInvalid(msg);
								});
							}
							else {
								i_cmp.close();
							}
						});
					}
					else {
						i_cmp.close();
					}
				}
			},
			{
				text: ip2.i18n({
					da: "Annuller",
					en: "Cancel"
				}),
				handler: function () {
					i_cmp.isCancel = true;
					i_cmp.isOk = false;
					i_cmp.close();
				}
			}

		];
		return a_buttons;
	},

	createNorth: function() {
		var i_cmp = this;
		var a_files = i_cmp.files;
		var layout = {
			xtype: "container",
			layout: "hbox",
			region: "north",
			height: 91,
			style: {
				padding: a_files.length >= 4 ? "15px 30px 20px 15px" : "15px 15px 20px 15px",
				borderBottom: "1px solid #8C8C8C"
			},
			items: [
				{
					xtype: "container",
					flex: 5,
					items: [
						{
							xtype: "container",
							autoEl: "h1",
							style: {
								fontSize: "14px",
								marginBottom: "10px"
							},
							html: ip2.i18n({
								da: "Vælg en handling for filerne før du trykker ok",
								en: "Select a action for the files before you press ok"
							})
						},
						{
							xtype: "container",
							autoEl: "p",
							html: ip2.i18n({
								da: "En eller flere filer findes i destinations folderen, vælg en handling",
								en: "One or more files exist in the destination folder, select a action"
							})
						}
					]
				},
				{
					xtype: "form",
					flex: 3,
					layout: "hbox",
					bodyStyle: "background-color:transparent;",
					style: {
						"text-align": "center"
					},
					border: 0,
					items: [
						{
							xtype: "radiofield",
							checked: true,
							labelAlign: "top",
							fieldLabel: ip2.i18n({
								da: "Skip",
								en: "Skip"
							}),
							width: 40,
							itemId: "skip_all",
							b_allowed: true,
							listeners: {
								change: {
									fn: function(i_self, newVal, oldVal) {

										if (i_self.b_allowed) {

											var i_replace_all = i_cmp.down("#replace_all");
											i_replace_all.b_allowed = false;
											i_replace_all.setValue(false);
											i_replace_all.b_allowed = true;

											var i_rename_all = i_cmp.down("#rename_all");
											i_rename_all.b_allowed = false;
											i_rename_all.setValue(false);
											i_rename_all.b_allowed = true;

											var i_form_records = i_cmp.down("#formRecords").getForm();

											i_form_records.getFields().each(function(i_record_field) {
												if (i_record_field.isSkip) {
													i_record_field.setValue(newVal);
												}
											});
										}
									}
								}
							}
						},
						{
							xtype: "radiofield",
							checked: false,
							labelAlign: "top",
							fieldLabel: ip2.i18n({
								da: "Overskriv",
								en: "Overwrite"
							}),
							width: 70,
							itemId: "replace_all",
							b_allowed: true,
							listeners: {
								change: {
									fn: function(i_self, newVal, oldVal) {

										if (i_self.b_allowed) {
											var i_skip_all = i_cmp.down("#skip_all");
											i_skip_all.b_allowed = false;
											i_skip_all.setValue(false);
											i_skip_all.b_allowed = true;

											var i_rename_all = i_cmp.down("#rename_all");
											i_rename_all.b_allowed = false;
											i_rename_all.setValue(false);
											i_rename_all.b_allowed = true;

											var i_form_records = i_cmp.down("#formRecords").getForm();

											i_form_records.getFields().each(function(i_record_field) {
												if (i_record_field.isReplace) {
													i_record_field.setValue(newVal);
												}
											});
										}
									}
								}
							}
						},
						{
							xtype: "fieldcontainer",
							layout: "hbox",
							flex: 2,
							style: {
								marginRight: "3px"
							},
							items: [
								{
									xtype: "radiofield",
									checked: false,
									readOnly: false,
									labelAlign: "top",
									fieldLabel: ip2.i18n({
										da: "Omdøb",
										en: "Rename"
									}),
									width: 50,
									itemId: "rename_all",
									b_allowed: true,
									listeners: {
										change: {
											fn: function(i_self, newVal, oldVal) {
												if (i_self.b_allowed) {

													var i_skip_all = i_cmp.down("#skip_all");
													i_skip_all.b_allowed = false;
													i_skip_all.setValue(false);
													i_skip_all.b_allowed = true;

													var i_replace_all = i_cmp.down("#replace_all");
													i_replace_all.b_allowed = false;
													i_replace_all.setValue(false);
													i_replace_all.b_allowed = true;

													var i_form_records = i_cmp.down("#formRecords").getForm();

													i_form_records.getFields().each(function(i_record_field) {
														if (i_record_field.isRename) {
															i_record_field.setValue(newVal);
														}
													});
												}
											}
										}
									}
								},
								{
									xtype: "container",
									flex: 1
								}
							]
						}

					]
				}
			]
		};
		return layout;
	},

	createCenter: function() {
		var i_cmp = this;
		var a_files = i_cmp.files;

		var a_extension_image = ["jpg","jpeg","png","gif", "bmp"];
		var a_docs = ["doc", "docx"];
		var a_xls = ["xls", "xlsx", "xlm","xlt", "xlw"];
		var s_pdf = "pdf";

		var s_path = i_cmp.path;
		var index = -1;

		var form = {
			xtype: "form",
			layout: "anchor",
			region: "center",
			itemId: "formRecords",
			border: false,
			autoScroll: true,
			items: (function () {
				var a_items = [];
				var s_dc = ip2.getDcTimestamp();

				ip2.each(a_files, function (o_file, n_index) {
					index++;
					var extension = o_file.name.split(".").pop();
					var s_fileName = o_file.name.replace("." + extension, "");
					var url = "";
					var b_extension = false;

					if (s_pdf == extension) {
						url = "/portfolio/styles/icon/flatui/media/pdf.png?_dc=" + s_dc;
						b_extension = true;
					}
					else if (a_docs.indexOf(extension) != -1) {
						url = "/portfolio/styles/icon/flatui/media/doc.png?_dc=" + s_dc;
						b_extension = true;
					}
					else if (a_xls.indexOf(extension) != -1) {
						url = "/portfolio/styles/icon/flatui/media/xls.png?_dc=" + s_dc;
						b_extension = true;
					}
					else {
						if (o_file.http) {
							url = o_file.http + "?_dc=" + s_dc;
						}
						else {
							url = [s_path, o_file.name].join("/") + "?_dc=" + s_dc;
						}
					}

					if (b_extension) {
						o_file.src = url;
					}

					a_items.push({
						xtype: "container",
						style: {
							padding: "15px",
							borderBottom: "1px solid #CCC"
						},
						layout: "hbox",
						flex: 1,
						items: [
							{
								xtype: "container",
								flex: 5,
								layout: "hbox",
								style: {
									fontSize: "11px"
								},
								items: [
									{
										xtype: "container",
										autoEl: "div",
										width: 100,
										height: 76,
										file: o_file,
										style: {
											marginRight: "10px",
											"background-image": "url(\"" + url + "\")",
											"background-size": "contain",
											"background-repeat": "no-repeat",
											"background-position": "50% 50%",
											"border": "1px solid black"
										},
										listeners: {
											afterrender: {
												fn: function(item) {
													var i_image = item;
													if (item.file.src) {
														i_image.getEl().dom.style.backgroundImage = "url(\"" + item.file.src + "?dc=" + ip2.getDcTimestamp() + "\")";
													}
													else {
														var reader = new FileReader();
														reader.onload = (function(i_file) {
															return function(e) {
																i_image.getEl().dom.style.backgroundImage = "url(\"" + e.target.result + "\")";
															};
														})(item.file);

														reader.readAsDataURL(item.file);
													}
												}
											}
										}
									},
									{
										xtype: "container",
										autoEl: "span",
										html: "<big><big><big><big><big><big>&#10137;</big></big></big></big></big></big>",
										style: {
											marginRight: "10px",
											marginLeft: "0px",
											paddingTop: "15px"
										}
									},
									{
										xtype: "container",
										autoEl: "div",
										width: 100,
										height: 76,
										file: o_file,
										style: {
											marginRight: "10px",
											"background-image": "url(\"" + url + "\")",
											"background-size": "contain",
											"background-repeat": "no-repeat",
											"background-position": "50% 50%",
											"border": "1px solid black"
										}
									},
									{
										xtype: "container",
										flex: 1,
										style: {
											paddingTop: "5px"
										},
										items: [
											{
												xtype: "container",
												html: o_file.name,
												style: {
													fontWeight: 700,
													fontSize: "12px"
												}
											},
											{
												xtype: "container",
												style: {
													color: "#8C8C8C",
													lineHeight: "19px"
												},
												html: s_path
											}
										]
									}
								]
							},
							{
								xtype: "container",
								flex: 3,
								itemId: "record_" + index,
								layout: "hbox",
								style: {
									paddingTop: "12px",
									"text-align": "center"
								},
								items: [
									{
										xtype: "radiofield",
										width: 40,
										checked: true,
										itemId: "i_" + index + "_skip",
										index: index,
										b_allowed: true,
										isSkip: true,
										listeners: {
											change: {
												fn: function(i_self, newVal, oldVal) {
													if (i_self.b_allowed) {
														var i_rename = i_cmp.down("#i_" + i_self.index + "_rename");
														var i_replace = i_cmp.down("#i_" + i_self.index + "_replace");
														var i_suggested = i_cmp.down("#i_" + i_self.index + "_suggested");

														i_rename.b_allowed = false;
														i_rename.setValue(false);
														i_rename.b_allowed = true;

														i_replace.b_allowed = false;
														i_replace.setValue(false);
														i_replace.b_allowed = true;

														var i_replace_all = i_cmp.down("#replace_all");
														var i_rename_all = i_cmp.down("#rename_all");
														var i_skip_all = i_cmp.down("#skip_all");

														var isAllRenameChecked = i_cmp.isAllRenameChecked();
														var isAllReplaceChecked = i_cmp.isAllReplaceChecked();
														var isAllSkipChecked = i_cmp.isAllSkipChecked();

														i_replace_all.b_allowed = false;
														i_replace_all.setValue(isAllReplaceChecked);
														i_replace_all.b_allowed = true;

														i_rename_all.b_allowed = false;
														i_rename_all.setValue(isAllRenameChecked);
														i_rename_all.b_allowed = true;

														i_skip_all.b_allowed = false;
														i_skip_all.setValue(isAllSkipChecked);
														i_skip_all.b_allowed = true;

														i_suggested.setReadOnly(true);
													}
												}
											}
										}
									},
									{
										xtype: "radiofield",
										width: 70,
										checked: false,
										itemId: "i_" + index + "_replace",
										index: index,
										b_allowed: true,
										isReplace: true,
										listeners: {
											change: {
												fn: function(i_self, newVal, oldVal) {
													if (i_self.b_allowed) {
														var i_rename = i_cmp.down("#i_" + i_self.index + "_rename");
														var i_skip = i_cmp.down("#i_" + i_self.index + "_skip");
														var i_suggested = i_cmp.down("#i_" + i_self.index + "_suggested");

														i_rename.b_allowed = false;
														i_rename.setValue(false);
														i_rename.b_allowed = true;

														i_skip.b_allowed = false;
														i_skip.setValue(false);
														i_skip.b_allowed = true;

														var i_replace_all = i_cmp.down("#replace_all");
														var i_rename_all = i_cmp.down("#rename_all");
														var i_skip_all = i_cmp.down("#skip_all");

														var isAllRenameChecked = i_cmp.isAllRenameChecked();
														var isAllReplaceChecked = i_cmp.isAllReplaceChecked();
														var isAllSkipChecked = i_cmp.isAllSkipChecked();

														i_replace_all.b_allowed = false;
														i_replace_all.setValue(isAllReplaceChecked);
														i_replace_all.b_allowed = true;

														i_rename_all.b_allowed = false;
														i_rename_all.setValue(isAllRenameChecked);
														i_rename_all.b_allowed = true;

														i_skip_all.b_allowed = false;
														i_skip_all.setValue(isAllSkipChecked);
														i_skip_all.b_allowed = true;

														i_suggested.setReadOnly(true);
													}
												}
											}
										}
									},
									{
										xtype: "fieldcontainer",
										layout: "hbox",
										flex: 2,
										items: [
											{
												xtype: "radiofield",
												width: 50,
												checked: false,
												itemId: "i_" + index + "_rename",
												index: index,
												isRename: true,
												b_allowed: true,
												listeners: {
													change: {
														fn: function(i_self, newVal, oldVal) {
															if (i_self.b_allowed) {
																var i_replace = i_cmp.down("#i_" + i_self.index + "_replace");
																var i_skip = i_cmp.down("#i_" + i_self.index + "_skip");
																var i_suggested = i_cmp.down("#i_" + i_self.index + "_suggested");

																i_replace.b_allowed = false;
																i_replace.setValue(false);
																i_replace.b_allowed = true;

																i_skip.b_allowed = false;
																i_skip.setValue(false);
																i_skip.b_allowed = true;

																var i_replace_all = i_cmp.down("#replace_all");
																var i_rename_all = i_cmp.down("#rename_all");
																var i_skip_all = i_cmp.down("#skip_all");

																var isAllRenameChecked = i_cmp.isAllRenameChecked();
																var isAllReplaceChecked = i_cmp.isAllReplaceChecked();
																var isAllSkipChecked = i_cmp.isAllSkipChecked();

																i_replace_all.b_allowed = false;
																i_replace_all.setValue(isAllReplaceChecked);
																i_replace_all.b_allowed = true;

																i_rename_all.b_allowed = false;
																i_rename_all.setValue(isAllRenameChecked);
																i_rename_all.b_allowed = true;

																i_skip_all.b_allowed = false;
																i_skip_all.setValue(isAllSkipChecked);
																i_skip_all.b_allowed = true;

																i_suggested.setReadOnly(false);
															}
														}
													}
												}
											},
											{
												xtype: "textfield",
												flex: 1,
												itemId: "i_" + index + "_suggested",
												readOnly: true,
												index: index,
												getValue: function() {
													var value = this.getRawValue();
													return value + "." + this.extension;
												},
												setValue: function(_value) {
													var value = _value || "";
													var extension = value.split(".").pop();
													this.extension = extension;
													i_cmp.extension = extension;
													var name = value.replace("." + extension, "");
													this.mixins.field.setValue.call(this, name);
													this.setRawValue(name);
													return this;
												}
											}
										]
									}
								]
							}
						]
					});
				});

				return a_items;
			})()
		};
		return form;
	},

	isAllReplaceChecked: function() {
		var i_cmp = this;
		var i_form_records = i_cmp.down("#formRecords").getForm();
		var checked = true;
		var fields = i_form_records.getFields();

		for (var i = 0; i < fields.items.length; i++) {
			var i_field = fields.items[i];
			if (!i_field.checked && i_field.isReplace) {
				checked = false;
				break;
			}
		}
		return checked;
	},

	isAllRenameChecked: function() {
		var i_cmp = this;
		var i_form_records = i_cmp.down("#formRecords").getForm();
		var checked = true;
		var fields = i_form_records.getFields();

		for (var i = 0; i < fields.items.length; i++) {
			var i_field = fields.items[i];
			if (!i_field.checked && i_field.isRename) {
				checked = false;
				break;
			}
		}
		return checked;
	},

	isAllSkipChecked: function() {
		var i_cmp = this;
		var i_form_records = i_cmp.down("#formRecords").getForm();
		var checked = true;
		var fields = i_form_records.getFields();

		for (var i = 0; i < fields.items.length; i++) {
			var i_field = fields.items[i];
			if (!i_field.checked && i_field.isSkip) {
				checked = false;
				break;
			}
		}
		return checked;
	},

	getSuggestions: function() {
		var i_cmp = this;
		var a_files = i_cmp.files;
		var s_path = i_cmp.path;
		Ext.ux.file.util.Handler.getSuggestions(a_files, s_path, function(a_records) {
			var i_form_records = i_cmp.down("#formRecords");
			ip2.each(a_records, function(o_record, n_index) {
				var field = i_form_records.down("#i_" + n_index + "_suggested");
				var field = field.setValue(o_record.suggestion);
			});
		});
	}
});
