Ext.define("Portfolio.designer.ui.views.grid.controller.Manager", {
	extend: "Ext.app.ViewController",

	alias: "controller.designer.ui.views.grid.controller.manager",

	onSqlPreview: function(s_sqlCmd, f_callBack) {

		var i_controller = this;
		var i_view = i_controller.getView();

		i_view.setLoading(true);

		f_callBack = f_callBack || Ext.emptyFn;

		var a_batch = [{
			type: "sql",
			cmd: s_sqlCmd,
			meta: true,
			start: 0,
			limit: 100
		}];

		Ext.Ajax.request({
			url: "/ip2-services/ip2dmdproxy.aspx",
			jsonData: {
				batch: a_batch
			},
			success: function (i_response) {

				i_view.setLoading(false);

				Ext.USE_NATIVE_JSON = false;

				var o_result = Ext.decode(i_response.responseText);
				var o_data = o_result.batch[0];

				//Ext.USE_NATIVE_JSON = true;

				f_callBack(o_data);
			},
			failure: function(i_err) {

				i_view.setLoading(false);

				f_callBack({
					message: i_err.message,
					success: false
				});
			}
		});
	},

	onShowPreview: function(o_result, a_current, f_callBack) {

		f_callBack = f_callBack || Ext.emptyFn;

		a_current = Ext.Array.from(a_current);

		if (o_result.success) {

			var o_meta = o_result.metaData;
			var a_metaFields = o_meta.fields;
			var a_rows = o_result[o_result.root];

			var a_fields = [];
			var a_columns = [];

			Ext.Array.each(a_metaFields, function(o_field) {

				var o_config = {
					name: o_field.name,
					type: "auto"
				};

				var o_column = {
					header: o_field.name,
					dataIndex: o_field.name,
					autoSizeColumn: true
				};

				a_columns.push(o_column);
				a_fields.push(o_config);
			});

			var i_grid = Ext.widget("gridpanel", {
				columns: a_columns,
				columnLines: true,
				flex: 1,
				store: Ext.create("Ext.data.Store", {
					fields: a_fields,
					data: a_rows,
					autoLoad: true
				}),
				viewConfig: {
					listeners: {
						refresh: {
							fn: function(i_dataView) {

								var i_grid = i_dataView.grid;
								var a_columns = i_grid.getColumnManager().getColumns();

								Ext.Array.each(a_columns, function(i_column) {
									if (i_column.autoSizeColumn === true) {
										i_column.autoSize();
									}
								});
							}
						}
					}
				}
			});

			var i_window = Ext.widget("window", {
				title: "Sql result",
				width: 800,
				height: 400,
				layout: {
					type: "hbox",
					align: "stretch"
				},
				modal: true,
				items: [
					i_grid
				]
			});

			var i_toolbar = Ext.widget("toolbar", {
				dock: "top",
				items: [
					{
						xtype: "button",
						iconCls: "pf-gearwheel",
						text: ip2.i18n({
							da: "Overføre via",
							en: "Transfere via"
						}),
						menu: {
							items: [
								{
									xtype: "menuitem",
									iconCls: "pf-link",
									text: ip2.i18n({
										da: "Sammenlæg",
										en: "Merge"
									}),
									handler: function(i_menuitem) {

										var a_return = [];
										var a_columns = i_grid.getColumnManager().getColumns();

										Ext.Array.each(a_columns, function(i_column) {

											var i_found;

											i_found = Ext.Array.findBy(a_current, function(i_search) {
												return i_search.dataIndex == i_column.dataIndex;
											});

											if (!i_found) {

												i_found = {
													dataIndex: i_column.dataIndex,
													name: i_column.dataIndex,
													title: i_column.text,
													header: i_column.text,
													flex: 1,
													showned: true,
													isTitle: false,
													dataOnly: false,
													hidden: false
												};

												i_found.columnConfig = Ext.apply({}, i_found);
											}
											else {
												i_found = Ext.apply(i_found.initialConfig, {
													showned: true,
													isTitle: false,
													dataOnly: false,
													hidden: false
												});
											}

											a_return.push(i_found);
										});

										Ext.Array.each(a_current, function(i_current) {

											var i_found = Ext.Array.findBy(a_return, function(i_search) {

												return i_search.dataIndex == i_current.dataIndex;
											});

											if (!i_found) {
												a_return.push(i_current.initialConfig);
											}
										});

										f_callBack(a_return, 0);

										i_window.close();
									}
								},
								{
									xtype: "menuitem",
									iconCls: "pf-arrow_right",
									text: ip2.i18n({
										da: "Erstat",
										en: "Replace"
									}),
									handler: function(i_menuitem) {

										var a_return = [];
										var a_columns = i_grid.getColumnManager().getColumns();

										Ext.Array.each(a_columns, function(i_column) {

											var i_found;

											i_found = Ext.Array.findBy(a_current, function(i_search) {
												return i_search.dataIndex == i_column.dataIndex;
											});

											if (i_found) {

												i_found = {
													dataIndex: i_column.dataIndex,
													name: i_column.dataIndex,
													title: i_column.text,
													header: i_column.text,
													flex: 1,
													showned: true,
													isTitle: false,
													dataOnly: false,
													hidden: false
												};

												i_found.columnConfig = Ext.apply({}, i_found);

												a_return.push(i_found);
											}
										});

										Ext.Array.each(a_current, function(i_current) {

											var i_found = Ext.Array.findBy(a_return, function(i_search) {
												return i_search.dataIndex == i_current.dataIndex;
											});

											if (!i_found) {
												a_return.push(i_current.initialConfig);
											}
										});

										f_callBack(a_return, 1);

										i_window.close();
									}
								}
							]
						}
					}
				]
			});

			i_grid.addDocked(i_toolbar);

			i_window.show();
		}
		else {
			ip2.messageBox({
				icon: Ext.MessageBox.INFO,
				title: ip2.i18n({
					da: "Sql fejl",
					en: "Sql Error"
				}),
				message: o_result.message,
				yesText: ip2.i18n({
					da: "Ok",
					en: "OK"
				})
			});
		}
	},

	onTransferColumns: function(a_transfered, i_grid, n_type) {

		var a_result = [];

		var a_columns = i_grid.getColumnManager().getColumns();

		a_result = a_transfered;

		i_grid.reconfigure(null, a_result);
	}
});
