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

	extend: "Ext.panel.Panel",

	requires: [
		"Ext.ux.form.field.AceTextArea",
		"Ext.ux.file.view.Explorer"
	],

	alias: [
		"widget.ux.file.view.editor",
		"widget.file.editor"
	],

	config: {
		mode: "text",
		editor: null,
		toolbar: null,
		file: "",
		useOwner: false,
		allowNew: true,
		allowOpen: true,
		fileMap: {},
		fileExtension: [
			{
				"ext": "*",
				"desc": "All Files (*.*)"
			}
		]
	},

	layout: "fit",

	initComponent: function() {

		var i_self = this;

		i_self.callParent(arguments);

		i_self.setFileMap({
			dmd: "json",
			xpd: "json",
			lib: "json",
			aspx: "javascript",
			widget: "javascript",
			txt: "text",
			sqltpl: "sql"
		});

		i_self.add(i_self.createEditor());
		i_self.addDocked(i_self.createToolbar());
	},

	createEditor: function() {

		var i_self = this;

		var i_editor = Ext.widget("acetextarea", {
			mode: i_self.getMode(),
			showPrintMargin: false,
			userWorker: true,
			listeners: {
				afterrender: {
					fn: function(i_field) {

						var s_file = i_self.getFile();

						if (s_file) {

							i_self.getFileFromPath(s_file, function(s_data) {

								var s_extension = s_file.split(".").pop().toLowerCase();

								var o_fileMape = i_self.getFileMap();

								var i_acefield = i_editor.getEditor();

								if (o_fileMape.hasOwnProperty(s_extension)) {
									s_extension = o_fileMape[s_extension];
								}

								i_acefield.setOptions({
									mode: "ace/mode/" + s_extension
								});

								i_editor.setValue(s_data);

								i_self.setFile(s_file);

								i_self.doTitleUpdate(s_file);
							});
						}
					},
					buffer: 10
				}
			}
		});

		i_self.setEditor(i_editor);

		return i_editor;
	},

	createToolbar: function() {

		var i_self = this;

		var i_toolbar = Ext.widget("toolbar", {
			dock: "top",
			items: [
				{
					xtype: "button",
					text: ip2.i18n({
						da: "Menu",
						en: "Menu"
					}),
					menu: {
						items: i_self.createMenuItems()
					}
				}
			]
		});

		i_self.setToolbar(i_toolbar);

		return i_toolbar;
	},

	createMenuItems: function() {

		var i_self = this;

		var a_items = [];

		if (i_self.getAllowNew()) {

			var i_newItem = Ext.widget("menuitem", {
				text: ip2.i18n({
					da: "Ny",
					en: "New"
				}),
				iconCls: "pf-add",
				menu: {
					items: [
						{
							xtype: "menuitem",
							text: "Json",
							handler: function(i_button) {

								var i_editor = i_self.getEditor();

								i_editor.setValue("");

								var i_acefield = i_editor.getEditor();

								i_acefield.setOptions({
									mode: "ace/mode/json"
								});

								i_self.setFile("");

								i_self.doTitleUpdate("Json");
							}
						},
						{
							xtype: "menuitem",
							text: "Javascript",
							handler: function(i_button) {

								var i_editor = i_self.getEditor();

								i_editor.setValue("");

								var i_acefield = i_editor.getEditor();

								i_acefield.setOptions({
									mode: "ace/mode/javascript"
								});

								i_self.setFile("");

								i_self.doTitleUpdate("Javascript");
							}
						},
						{
							xtype: "menuitem",
							text: "Html",
							handler: function(i_button) {

								var i_editor = i_self.getEditor();

								i_editor.setValue("");

								var i_acefield = i_editor.getEditor();

								i_acefield.setOptions({
									mode: "ace/mode/html"
								});

								i_self.setFile("");

								i_self.doTitleUpdate("Html");
							}
						},
						{
							xtype: "menuitem",
							text: "Xml",
							handler: function(i_button) {

								var i_editor = i_self.getEditor();

								i_editor.setValue("");

								var i_acefield = i_editor.getEditor();

								i_acefield.setOptions({
									mode: "ace/mode/xml"
								});

								i_self.setFile("");

								i_self.doTitleUpdate("Xml");
							}
						}
					]
				}
			});

			a_items.push(i_newItem);
		}

		if (i_self.getAllowOpen()) {

			var i_openItem = Ext.widget("menuitem", {
				text: ip2.i18n({
					da: "Åben",
					en: "Open"
				}),
				iconCls: "pf-gearwheel",
				handler: function(i_button) {

					var i_editor = i_self.getEditor();

					i_self.openFile(function(s_data, s_type) {

						var o_fileMape = i_self.getFileMap();

						var i_acefield = i_editor.getEditor();

						if (o_fileMape.hasOwnProperty(s_type)) {
							s_type = o_fileMape[s_type];
						}

						i_acefield.setOptions({
							mode: "ace/mode/" + s_type
						});

						i_editor.setValue(s_data);
					});
				}
			});

			a_items.push(i_openItem);
		}

		var i_saveFile = Ext.widget("menuitem", {
			text: ip2.i18n({
				da: "Gem",
				en: "Save"
			}),
			iconCls: "pf-floppy_disk",
			handler: function(i_button) {

				var i_editor = i_self.getEditor();

				var s_value = i_editor.getValue();

				var s_file = i_self.getFile();

				if (s_file) {
					i_self.saveFileToPath(s_file, s_value);
					i_self.fireEvent("filesave", i_self, s_file);
				}
				else {
					i_self.doSaveAs(s_value);
				}
			}
		});

		var i_saveAsFile = Ext.widget("menuitem", {
			text: ip2.i18n({
				da: "Gem som",
				en: "Save as"
			}),
			iconCls: "pf-floppy_disk",
			handler: function(i_button) {

				var i_editor = i_self.getEditor();

				i_self.doSaveAs(i_editor.getValue());
			}
		});

		a_items.push(i_saveFile);
		a_items.push(i_saveAsFile);

		return a_items;
	},

	getFileFromPath: function(s_path, f_callback) {

		Ext.Ajax.request({
			url: "/ip2-services/webBios.aspx",
			params: {
				func: "readstmf",
				file: s_path
			},
			success: function (o_response) {

				var s_result = o_response.responseText;

				f_callback(s_result);
			}
		});
	},

	saveFileToPath: function(s_file, s_data) {

		Ext.Ajax.request({
			url: "/ip2-services/webBios.aspx",
			params: {
				func: "writestmf",
				data: s_data,
				file: s_file,
				ignoreStaticDir: true
			}
		});
	},

	openFile: function(f_callback) {

		var i_self = this;

		f_callback = f_callback || Ext.emptyFn;

		var i_explorer = Ext.widget("file.explorer", {
			path: ip2.baseconfig.session.preset.root || "/",
			saveAction: false,
			listeners: {
				fileopen: {
					fn: function(i_explorer, s_file) {

						var s_extension = s_file.split(".").pop().toLowerCase();

						i_self.getFileFromPath(s_file, function(s_data) {

							f_callback(s_data, s_extension);
						});

						i_self.setFile(s_file);

						i_self.doTitleUpdate(s_file);
					}
				}
			}
		});

		i_explorer.show();
	},

	doSaveAs: function(s_value) {

		var i_self = this;

		var i_editor = i_self.getEditor();

		var s_data = s_value;

		var s_file = i_self.getFile();

		var s_part = ip2.baseconfig.session.preset.root || "/";

		if (s_file) {

			if (s_file.split(".").length > 1) {

				var a_split = s_file.split(".");
				a_split.pop();

				var s_temp = a_split.join("");

				a_split = s_temp.split("/");
				a_split.pop();

				s_path = a_split.join("/");
			}
		}

		var i_explorer = Ext.widget("file.explorer", {
			path: s_path,
			saveAction: true,
			fileExtension: i_self.getFileExtension(),
			listeners: {
				filesave: {
					fn: function(i_explorer, s_file) {

						i_self.saveFileToPath(s_file, s_data);

						var s_extension = s_file.split(".").pop().toLowerCase();

						var o_fileMape = i_self.getFileMap();

						var i_acefield = i_editor.getEditor();

						if (o_fileMape.hasOwnProperty(s_extension)) {
							s_extension = o_fileMape[s_extension];
						}

						i_acefield.setOptions({
							mode: "ace/mode/" + s_extension
						});

						i_self.setFile(s_file);

						i_self.doTitleUpdate(s_file);

						i_self.fireEvent("filesave", i_self, s_file);
					}
				}
			}
		});

		i_explorer.show();
	},

	doTitleUpdate: function(s_file) {

		var i_self = this;

		var a_title = [];

		var s_part = ip2.i18n({
			da: "Editing",
			da: "Edíting"
		});

		a_title.push(s_part);
		a_title.push(s_file);

		var b_setOwner = i_self.getUseOwner();

		var i_owner = b_setOwner ? i_self.ownerCt || i_self : i_self;

		i_owner.setTitle(a_title.join(" - "));
	}
});
