Ext.define("Portfolio.dynapp.view.Main", {

	extend: "Ext.panel.Panel",

	alias: "widget.dynapp.view.main",

	collapsible: false,
	closable: false,

	layout: "fit",
	fields: [],

	schema: "",
	table: "",

	meta: null,

	alternateClassName: [
		"Ext.dynapp.view.Main"
	],

	requires: [
		"Portfolio.dynapp.view.Grid"
	],

	uses: [
		"Portfolio.dynapp.ui.schema.Open",
		"Portfolio.dynapp.ui.schema.Save",
		"Portfolio.dynapp.ui.schema.Add",
		"Portfolio.dynapp.ui.table.Add",
		"Portfolio.dynapp.ui.table.Manager",
		"Portfolio.dynapp.ui.column.Editor"
	],

	config: {
		grid: null
	},

	initComponent: function() {

		var i_self = this;
		i_self.callParent(arguments);
		var i_grid = i_self.createLayout();
		i_self.add(i_grid);
		i_self.addDocked(i_self.createToolbar(i_grid));
		return i_self;
	},

	createLayout: function() {

		var i_self = this;
		var o_meta = i_self.meta;
		var o_options = i_self.options;

		var i_grid = Ext.widget("dynapp.gridpanel", {
			fields: o_meta.fields,
			idProperty: o_meta.id,
			options: i_self.options,
			viewConfig: {
				trackOver: true,
				loadMask: true,
				variableRowHeight: true
			},
			numFromEdge: 4
		});

		i_self.setGrid(i_grid);

		return i_grid;
	},

	createToolbar: function(i_grid) {

		var i_self = this;
		var o_options = i_self.options;
		var a_items = [];
		var o_menu = i_self.createMenu(i_grid);

		if (o_menu.items.length > 0) {
			a_items.push(
				{
					xtype: "button",
					text: ip2.i18n({
						en: "File",
						da: "Fil"
					}),
					itemId: "menu-file",
					iconCls: "pf-edit",
					menu: o_menu
				},
				"-"
			);
		}

		a_items = a_items.concat(i_self.createLabels(o_options));

		if (o_options.library && o_options.file) {
			a_items.push("->");
			a_items.push(i_self.createLinkButton());
		}

		var i_toolbar = Ext.widget("toolbar", {
			dock: "top",
			items: a_items,
			grid: i_grid
		});

		i_self._actionBar = i_toolbar;

		return i_toolbar;
	},

	createMenu: function(i_grid) {

		var i_self = this;
		var o_options = i_grid.options;
		var a_items = [];

		if (ip2.isAccessible("app.dynapp.functions.table.open")) {
			var i_open = Ext.widget("menuitem", {
				text: ip2.i18n({
					en: "Open",
					da: "Åben"
				}),
				handler: function() {

					var i_editor = Ext.widget("dynapp.ui.schema.open", {
						title: ip2.i18n({
							en: "Open schema:",
							da: "Åben skema:"
						}),
						width: 600,
						grid: i_grid
					});

					i_editor.show();
				}
			});

			a_items.push(i_open);
		}

		var a_addItems = i_self.createAdd(i_grid);
		if (a_addItems.length > 0) {
			var i_add = Ext.widget("menuitem", {
				text: ip2.i18n({
					en: "Add",
					da: "Tilføj"
				}),
				menu: {
					items: a_addItems
				}

			});
			a_items.push(i_add);
		}

		if (ip2.isAccessible("app.dynapp.functions.application.editor")) {
			var i_tableEditor = Ext.widget("menuitem", {
				text: ip2.i18n({
					en: "Editor",
					da: "Edtior"
				}),
				itemId: "item-plugin",
				disabled: o_options.library === "" || o_options.file === "",
				handler: function(item) {

					var i_editor = Ext.widget("dynapp.ui.table.manager", {
						title: ip2.i18n({
							en: "Edit table: ",
							da: "Rediger tabel: "
						}),
						width: 800,
						height: 600,
						grid: i_grid
					});

					i_editor.show();
				}
			});

			a_items.push(i_tableEditor);
		}

		if (ip2.isAccessible("app.dynapp.functions.application.save")) {
			var i_save = Ext.widget("menuitem", {
				text: ip2.i18n({
					en: "Save application",
					da: "Gem applikation"
				}),
				itemId: "item-save",
				disabled: o_options.library === "" || o_options.file === "",
				handler: function(item) {

					var i_grid = i_self.getGrid();

					var o_options = i_grid.options || {};

					var i_editor = Ext.widget("dynapp.ui.schema.save", {
						title: ip2.i18n({
							en: "Save application:",
							da: "Gem applikation:"
						}),
						width: 800,
						height: 600,
						schema: o_options.library,
						table: o_options.file,
						appTitle: o_options.file

					});

					i_editor.show();
				}
			});

			a_items.push(i_save);
		}

		var o_menu = {
			items: a_items
		};

		return o_menu;
	},

	createAdd: function(i_grid) {

		var i_self = this;
		var o_options = i_self.options || {};
		var a_items = [];

		if (ip2.isAccessible("app.dynapp.functions.schema.create")) {
			a_items.push({
				xtype: "menuitem",
				text: ip2.i18n({
					en: "Schema",
					da: "Skema"
				}),
				handler: function() {
					var i_editor = Ext.widget("dynapp.ui.schema.add", {
						title: ip2.i18n({
							en: "New schema:",
							da: "Ny skema:"
						}),
						options: o_options,
						width: 400,
						grid: i_grid
					});
					i_editor.show();
				}
			});
		}

		if (ip2.isAccessible("app.dynapp.functions.table.create")) {
			a_items.push({
				xtype: "menuitem",
				text: ip2.i18n({
					en: "Table",
					da: "Tabel"
				}),
				handler: function() {
					var i_editor = Ext.widget("dynapp.ui.table.add", {
						title: ip2.i18n({
							en: "New table:",
							da: "Ny tabel:"
						}),
						options: o_options,
						width: 400,
						grid: i_grid
					});
					i_editor.show();
				}
			});
		}

		if (ip2.isAccessible("app.dynapp.functions.columns.create")) {
			a_items.push({
				xtype: "menuitem",
				text: ip2.i18n({
					en: "Column",
					da: "Kolonne"
				}),
				itemId: "item-column",
				disabled: o_options.library === "" || o_options.file === "",
				handler: function() {
					var i_editor = Ext.widget("dynapp.ui.column.editor", {
						title: ip2.i18n({
							en: "New column:",
							da: "Ny kolonne:"
						}),
						action: "new",
						options: o_options,
						width: 400,
						grid: i_grid
					});
					i_editor.show();
				}
			});
		}
		return a_items;
	},

	createLabels: function(o_options) {

		var i_self = this;
		var a_items = [];

		if (o_options.library) {
			var o_labelSchema = {
				xtype: "tbtext",
				text: ip2.i18n({
					da: "Skema",
					en: "Schema"
				}) + ": " + o_options.library
			};

			a_items.push(o_labelSchema);
		}

		if (o_options.file) {
			var o_labelTable = {
				xtype: "tbtext",
				text: ip2.i18n({
					da: "Tabel",
					en: "Table"
				}) + ": " + o_options.file
			};

			a_items.push(o_labelTable);
		}

		return a_items;
	},

	createLinkButton: function() {

		var i_self = this;

		var o_model = {
			dynapp: i_self.options
		};

		var i_button = Ext.widget("button", {
			itemId: "button-link",
			iconCls: "pf-link",
			handler: function() {

				var i_window = Ext.widget("link.window", {
					model: o_model
				});
				i_window.show();
			}
		});

		i_self.add(i_button);

		return i_button;
	},

	getActionBar: function() {

		var i_self = this;
		return i_self._actionBar;
	},

	doUpdate: function(o_options) {

		var i_self = this;
		var i_actionBar = i_self.getActionBar();

		var a_items = i_actionBar.query("[xtype=tbtext]");
		Ext.Array.each(a_items, function(i_item) {
			i_actionBar.remove(i_item, true);
		});
		i_actionBar.insert(2, i_self.createLabels(o_options));

		var i_filemenu = i_actionBar.down("#menu-file");

		if (i_filemenu) {
			var i_plugin = i_filemenu.down("#item-plugin");
			var i_save = i_filemenu.down("#item-save");
			var i_column = i_filemenu.down("#item-column");
			if (i_plugin) {
				if (o_options.library && o_options.file) {
					i_plugin.enable();
				}
				else {
					i_plugin.disable();
				}
			}
			if (i_save) {
				if (o_options.library && o_options.file) {
					i_save.enable();
				}
				else {
					i_save.disable();
				}
			}
			if (i_column) {
				if (o_options.library && o_options.file) {
					i_column.enable();
				}
				else {
					i_column.disable();
				}
			}
		}

		var s_title = o_options.library + " - " + o_options.file;

		if (!i_self.ownerCt.ownerCt.originalTitle) {
			var s_old = i_self.ownerCt.ownerCt.getTitle();
			i_self.ownerCt.ownerCt.originalTitle = s_old;
		}

		i_self.setTitle(s_title);
	}
});
