Ext.define("Generator.controller.Main", {

	extend: "Ext.app.ViewController",

	alias: "controller.generator-main",

	onSqlPreview: function(s_sqlCmd, n_limit, 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);

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

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

				i_view.setLoading(false);

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

	onShowPreview: function(o_result, f_callBack) {

		f_callBack = f_callBack || Ext.emptyFn;

		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();
									}
								});
							}
						}
					}
				}
			});

			f_callBack(i_grid);
		}
		else {
			Portfolio.util.Handler.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"
				})
			});
		}
	},

	onShowFormPreview: function(o_result, f_callBack) {

		f_callBack = f_callBack || Ext.emptyFn;

		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 = [];

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

				var o_config = {
					name: o_field.name,
					fieldLabel: o_field.header || o_field.name,
					isFormField: false
				};

				switch(o_field.datatype) {
					case "int":
					case "float":
					case "bigint":
					case "number":
					case "dec":
					case "decimal":
						Ext.apply(o_config, {
							xtype: "numberfield"
						});
						break;
					case "char":
					case "varchar":
						Ext.apply(o_config, {
							xtype: "textfield"
						});
						break;
					case "boolan":
					case "smallint":
					case "bool":
						Ext.apply(o_config, {
							xtype: "checkbox"
						});
						break;
					case "date":
						Ext.apply(o_config, {
							xtype: "datefield"
						});
						break;
					case "time":
						Ext.apply(o_config, {
							xtype: "isotimestampfield",
							date: false
						});
						break;
					case "datetime":
					case "timestamp":
					case "isotimestamp":
						Ext.apply(o_config, {
							xtype: "isotimestampfield"
						});
						break;
					default:
						break;
				}

				a_fields.push(o_config);

			});

			var i_form = Ext.widget("form", {
				layout: "anchor",
				bodyPadding: 10,
				items: a_fields,
				border: false,
				autoScroll: true,
				defaults: {
					labelWidth: 160,
					anchor: "100%"
				}
			});

			f_callBack(i_form);
		}
		else {
			Portfolio.util.Handler.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"
				})
			});
		}
	},

	onBuildApplication: function() {

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

		var i_searchField = i_view.getSearchField();
		var i_getField = i_view.getGetField();

		var o_values = i_view.getValues();

		Ext.apply(o_values, {
			searchStmt: i_searchField.getValue().join(" "),
			getStmt: i_getField.getValue().join(" ")
		});

		i_view.setLoading(true);

		Ext.Ajax.request({
			url: "http://dksrv206:60000/router/codeGen/genListAndForm?payload=" + Ext.encode(o_values),
			success: function (i_response) {

				i_view.setLoading(false);

				var o_result = Ext.decode(i_response.responseText);
				
				if (!o_result.success) {
					Portfolio.util.Handler.messageBox({
						icon: Ext.MessageBox.INFO,
						title: ip2.i18n({
							da: "Build fejl",
							en: "Build Error"
						}),
						message: o_result.message,
						yesText: ip2.i18n({
							da: "Ok",
							en: "OK"
						})
					});
				}

			},
			failure: function(i_err) {

				i_view.setLoading(false);

				Portfolio.util.Handler.messageBox({
					icon: Ext.MessageBox.INFO,
					title: ip2.i18n({
						da: "Build fejl",
						en: "Build Error"
					}),
					message: "",
					yesText: ip2.i18n({
						da: "Ok",
						en: "OK"
					})
				});
			}
		});
	}
});