Ext.define("Portfolio.designer.ui.wizard.controller.Window", {
	extend: "Ext.app.ViewController",

	alias: "controller.designer.ui.wizard.controller.window",

	showNext: function () {
		var i_controller = this;
		i_controller.doCardNavigation(1);
	},

	showPrevious: function () {
		var i_controller = this;
		i_controller.doCardNavigation(-1);
	},

	doCardNavigation: function (n_increment) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_stack = i_view.getStack();
		var i_layout = i_stack.getLayout();
		var n_index = i_layout.activeItem.name.split("card-")[1];
		var n_next = parseInt(n_index, 10) + n_increment;

		i_layout.setActiveItem(n_next);

		i_view.down("#card-prev").setDisabled(n_next === 0);
		i_view.down("#card-next").setDisabled(n_next === 4);
		i_view.down("#card-save").setVisible(n_next === 4);
	},

	onSave: function() {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_designer = i_view.getDesigner();
		var i_modelPanel = i_designer.getModelPanel();
		var o_model = i_designer.model;
		var i_stack = i_view.getStack();
		var a_relations = [];

		var o_entity = Ext.apply({}, {
			columns: [],
			uiFeatures: {},
			ui: {
			}
		});

		var i_columnManager = i_stack.getColumnManager();
		var i_keyManager = i_stack.getKeyManager();
		var i_relationManager = i_stack.getRelationManager();
		var i_constraintsManager = i_stack.getConstraintsManager();
		var i_entityPropertyManager = i_stack.getEntityPropertyManager();

		i_entityPropertyManager.doSave(o_entity);
		i_columnManager.doSave(o_entity);
		i_keyManager.doSave(o_entity);
		i_constraintsManager.doSave(o_entity);

		a_relations = i_relationManager.doSave(o_entity);

		var a_columns = Ext.clone(o_entity.columns) || [];

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

			Ext.apply(o_value, {
				type: o_value.type.split("(")[0],
				isTitle: false,
				showned: true,
				header: o_value.title,
				flex: 1
			});

			o_value.uitype = o_value.type;

			delete o_value.type;
		});

		// apply default portfolio view

		Ext.applyIf(o_entity.ui, {
			grids: {
				portfolio: {
					features: {},
					columns: a_columns
				}
			}
		});

		a_columns = Ext.clone(o_entity.columns) || [];

		var a_formColumns = [];

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

			a_formColumns.push({
				name: o_value.name
			});
		});

		var s_dmd = i_view.modelId ? "dmd/" + i_view.modelId :
					"dmd/" + ip2.cleanPath(i_designer.dmdFile).split("/").pop();

		var o_uirules = ip2.uirules;
		var o_uiTypes = ip2.getUiTypes();

		var a_fields = [];

		var o_properties = i_entityPropertyManager.getForm().getFieldValues();

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

			var o_field = Ext.apply({}, o_value, {
				fieldLabel: o_value.title
			});

			var a_keys = Ext.Object.getValues(o_properties.primaryKey);

			if (o_field.name === o_properties.idColumn || Ext.Array.contains(a_keys, o_field.name)) {
				o_field.readOnly = true;
			}

			o_field.type = o_value.type.split("(")[0];

			if (o_field.uiType) {
				o_field.uitype = o_field.uiType;
				delete o_field.uiType;
			}
			if (o_field.uitype) {
				var o_uiType = o_uiTypes[o_field.uitype];
				if (o_uiType.form && o_uiType.form.type) {
					var o_type = o_uiTypes[o_field.type];
					if (o_type) {
						o_field.type = o_uiType.form.type;
					}
				}
				delete o_field.uitype;
			}

			if (!o_field.type) {
				o_field.type = "varchar";
			}

			if (o_field.hasOwnProperty("title")) {
				o_field.fieldLabel = o_field.title;
			}

			var o_collection;
			var b_found = false;
			var s_type = o_field.type;

			while (!b_found) {

				var o_current = o_uirules.collections[s_type] || {
					apply: {
						type: "varchar"
					}
				};

				if (o_current.apply && o_current.apply.type) {
					s_type = o_current.apply.type;
				}
				else if (o_current.apply && !o_current.apply.type) {
					b_found = true;
				}
				else {
					b_found = true;
				}
				if (b_found) {
					o_collection = o_current;
				}
			}

			o_field.type = s_type;

			var a_props = Ext.Object.getKeys(o_field);

			Ext.Array.each(a_props, function(s_key) {

				if (Ext.isEmpty(o_field[s_key], false)) {

					delete o_field[s_key];
				}
			});

			Ext.Array.each(a_relations, function(o_relation) {

				if (o_relation.between.entity === o_entity.name) {

					if (o_relation.between.occurrence === "many") {

						if (o_relation.and.occurrence === "one") {

							if (o_value.name === o_relation.between.on[0].name) {

								o_field = {
									type: "entitypicker",
									dmd: s_dmd,
									entity: o_relation.and.entity,
									view: "portfolio",
									name: o_field.name,
									fieldLabel: o_field.title
								};

								return false;
							}
						}
					}
				}
			});

			a_fields.push(o_field);
		});

		Ext.applyIf(o_entity.ui, {
			forms: {
				portfolio: {
					features: {},
					columns: a_formColumns,
					items: {
						columns: a_fields
					}
				}
			}
		});

		Ext.apply(o_entity, o_properties);

		Ext.Object.each(o_entity, function(s_key, value) {

			if (Ext.isEmpty(value, false)) {
				delete o_entity[s_key];
			}
		});

		var i_entity = Ext.create("Portfolio.designer.ui.domainModelEntity", {
			designer: i_designer,
			entity: o_entity,
			width: i_designer.settings.entities.size[0],
			height: i_designer.settings.entities.size[1]
		});

		o_model.entities.push(o_entity);

		o_model.required.push(o_entity);

		o_model.relations = o_model.relations.concat(a_relations);

		i_modelPanel.add(i_entity);

		i_view.close();

		var i_entityEl = i_entity.getEl();

		i_entityEl.scrollIntoView(i_modelPanel.body, true, true);
	}

});
