Ext.define("IceCap.controller.Window", {
	extend: "Ext.app.ViewController",

	alias: "controller.icecap.view.window",

	onSendData: function(o_config, o_data) {

		var i_controller = this;
		var o_saved = Ext.clone(o_config);
		var i_view = i_controller.getView();
		var s_session = i_view.getSessionValue();

		var o_options = i_view.getOptions();

		o_data = Ext.apply(o_data, {
			i18nLearning: false
		});

		Ext.apply(o_config, {
			session: s_session
		});

		Ext.apply(o_config, o_options);

		if (!i_view.getWorking()) {
			i_view.setWorking(true);
			i_view.doTimer();
			IceCap.util.Handler.getData(o_config, o_data, function(o_inputData) {

				if (o_inputData.statusUpdate) {

					i_view.setWorking(false);
					i_view.updateMessage(o_inputData);
					if (o_inputData.status > -1) {
						i_view.doCreateFromData(o_inputData);
					}
					i_controller.onSendData(o_saved, o_data);
				}
				else {

					i_view.setLoading(false);
					i_view.cancelTimer();

					var f_callback = i_view.getResponseCallBack() || Ext.emptyFn;

					if (f_callback != Ext.emptyFn) {

						f_callback(o_inputData);
					}

					var o_plugin = o_inputData.plugin || Ext.apply({}, {
						require: []
					});

					var a_require = o_plugin.require || [];
					var s_run = o_plugin.run || "";

					i_controller.doRequire(a_require, function() {

						if (s_run) {
							eval(s_run);
						}

						if (o_inputData.close) {
							i_view.fireEvent("applicationClose", true);
						}
						else {

							i_view.setWorking(false);
							i_view.doCreateFromData(o_inputData);
						}
					});
				}
			});
		}
	},

	onWheel: function(i_event) {

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

		var b_hasGrid = i_view.query("gridpanel").length > 0;
		var b_mappanel = i_view.query("icecap-gmappanel").length > 0;

		var n_delta = i_event.getWheelDelta();
		var s_event = "";

		if (n_delta > 0) {
			s_event = "ROLLDOWN";
		}
		else {
			s_event = "ROLLUP";
		}

		if (!b_hasGrid && !b_mappanel && !i_event.ctrlKey) {
			i_controller.doKeyEvent(s_event);
		}
	},

	onDblClick: function(i_event) {

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

		var b_fireEvent = i_view.getEnterOnDblClick();

		if (b_fireEvent) {

			var i_body = i_view.body;
			var a_local = i_body.getOffsetsTo(window);

			var o_click = Ext.apply({
				x: i_event.pageX - a_local[0],
				y: i_event.pageY - a_local[1]
			});

			var o_position = IceCap.util.Handler.toColAndLine(o_click);

			var i_currentField = i_view.getCurrentField();

			if (i_currentField) {
				o_position = Ext.apply(o_position, {
					lin: i_currentField.lin,
					col: i_currentField.col
				});
				if (i_currentField.inputEl && i_currentField.getValue()) {
					o_position = IceCap.util.Handler.toColAndLine(o_click);
				}
			}
			else {
				i_cursor.moveCursor(o_position);
				i_cursor.show();
			}

			i_view.setCurrentPosition(o_position);
			i_controller.doKeyEvent("ENTER");
		}
	},

	onClick: function (i_event) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var o_inputData = i_view.getInputData();
		var i_cursor = i_view.getCursor();
		var i_content = i_view.getContent();

		var i_activeComponent = Ext.ComponentManager.getActiveComponent();

		var i_body = i_view.body;
		var a_local = i_body.getOffsetsTo(window);

		var o_click = Ext.apply({
			x: i_event.pageX - a_local[0],
			y: i_event.pageY - a_local[1]
		});

		var o_position = IceCap.util.Handler.toColAndLine(o_click);

		if (i_content.contains(i_activeComponent) || i_content.ownerCt == i_activeComponent) {

			var i_currentField = i_view.getCurrentField();

			var a_metaFields = o_inputData.metaFields || [];

			if (i_currentField) {
				o_position = Ext.apply(o_position, {
					lin: i_currentField.lin,
					col: i_currentField.col
				});
				if (i_currentField.inputEl && i_currentField.getValue()) {
					o_position = IceCap.util.Handler.toColAndLine(o_click);
				}
			}
			else {

				var a_inputs = IceCap.util.Handler.getInputFields(i_view);

				var o_box = Ext.apply({
					x: IceCap.util.Handler.getX(o_position.col),
					y: IceCap.util.Handler.getY(o_position.lin),
					width: IceCap.util.Handler.getColSize(),
					height: 1
				});

				var b_intersect = false;

				Ext.Array.each(a_inputs, function (i_field) {

					var i_element = i_field.getEl();

					if (i_element) {

						var o_bound = i_element.dom.getBoundingClientRect();

						var o_rect = Ext.apply({}, {
							x: IceCap.util.Handler.getX(i_field.col),
							y: IceCap.util.Handler.getY(i_field.lin),
							width: o_bound.width,
							height: o_bound.height
						});

						if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {

							b_intersect = true;
							i_field.focus(false, 10);

							var n_start = o_position.col - i_field.col;
							var i_inputEl = i_field.inputEl;
							i_inputEl.dom.selectionStart = i_inputEl.dom.selectionEnd = n_start;

							return false;
						}
					}
				});

				if (!b_intersect) {
					i_cursor.moveCursor(o_position);

					i_cursor[a_metaFields.length === 0 ? "show" : "hide"]();
				}
			}
		}

		i_view.setCurrentPosition(o_position);
	},

	doArrowKeys: function(n_direction) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var a_fields = IceCap.util.Handler.getInputFields(i_view);
		var n_tabIndex = i_view.getCurrentTabIndex();
		var n_lastTabIndex = i_view.getLastTabIndex();
		var n_part = n_direction ? 1 : -1;

		var i_lastFocus;

		Ext.Array.each(a_fields, function(i_field) {
			if (i_field.getTabIndex() === n_tabIndex) {
				i_lastFocus = i_field;
				return false;
			}
		});

		var b_found = false;

		if (i_lastFocus) {

			var n_lin = i_lastFocus.lin;
			var n_col = i_lastFocus.col;
			var n_next = n_lin + n_part;

			Ext.Array.each(a_fields, function(i_field) {
				if (i_field.lin === n_next && i_field.col === n_col) {
					b_found = true;
					n_tabIndex = i_field.getTabIndex();
					i_field.focus(false, 10);
					return false;
				}
			});
		}

		if (!b_found) {

			n_tabIndex = i_view.getCurrentTabIndex() + n_part;

			if (n_direction) {
				if (n_tabIndex > n_lastTabIndex) {
					n_tabIndex = 1;
				}
			}
			else {
				if (n_tabIndex < 1) {
					n_tabIndex = n_lastTabIndex;
				}
			}

			Ext.Array.each(a_fields, function(i_field) {
				if (i_field.getTabIndex() === n_tabIndex) {
					i_field.focus(false, 10);
					return false;
				}
			});
		}

		i_view.setCurrentTabIndex(n_tabIndex);
	},

	doTabKey: function(n_direction) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var a_fields = IceCap.util.Handler.getInputFields(i_view);
		var n_tabIndex = i_view.getCurrentTabIndex();
		var n_lastTabIndex = i_view.getLastTabIndex();
		var n_part = n_direction ? 1 : -1;

		n_tabIndex = i_view.getCurrentTabIndex() + n_part;

		if (n_direction) {
			if (n_tabIndex > n_lastTabIndex) {
				n_tabIndex = 1;
			}
		}
		else {
			if (n_tabIndex < 1) {
				n_tabIndex = n_lastTabIndex;
			}
		}

		Ext.Array.each(a_fields, function(i_field) {
			if (i_field.getTabIndex() === n_tabIndex) {
				i_field.focus(false, 10);
				return false;
			}
		});

		i_view.setCurrentTabIndex(n_tabIndex);
	},

	doKeyEvent: function(s_value) {

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

		var i_currentField = i_view.getCurrentField();
		var o_position = i_view.getCurrentPosition();

		var o_data = {
			key: s_value
		};

		var i_content = i_view.getContent();
		var i_form = i_content.getForm();
		var i_collectionField = i_form.getFields();
		var o_values = {};

		Ext.Array.each(i_collectionField.items, function(i_field) {
			if (i_field.isDirty()) {
				var value = i_field.getSubmitValue ? i_field.getSubmitValue() : i_field.getValue();
				o_values[i_field.itemId || i_field.initialConfig.id || i_field.name] = value;
			}
		});

		Ext.Object.each(o_values, function(s_key, value) {
			var s_id = "ses2.win." + s_key;
			o_data[s_id] = value;
		});

		if (i_currentField) {

			o_data = Ext.apply(o_data, {
				csrLin: i_currentField.lin,
				csrCol: i_currentField.col
			});

			if (i_currentField.inputEl && i_currentField.getValue()) {

				o_data = Ext.apply(o_data, {
					csrCol: o_position.col > 1 ? o_position.col : i_currentField.col
				});
			}
		}
		else {

			o_data = Ext.apply(o_data, {
				csrLin: o_position.lin,
				csrCol: o_position.col
			});
		}

		var f_callback = i_view.getRequestCallBack() || Ext.emptyFn;

		if (f_callback != Ext.emptyFn) {

			f_callback(o_data);
		}

		i_controller.onSendData({
			func: ""
		}, o_data);
	},

	doFKey: function(s_value) {

		var i_controller = this;

		i_controller.doKeyEvent(s_value);
	},

	doRequire: function(a_require, f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		var a_require = a_require || [];

		if (a_require.length > 0) {

			var a_external = Ext.Array.filter(Ext.Array.map(a_require, function(s_value) {
				if (s_value.indexOf("/") > -1) {
					return s_value;
				}
			}), function(value) {
				return value;
			});

			var a_internal = Ext.Array.filter(Ext.Array.map(a_require, function(s_value) {
				if (s_value.indexOf("/") < 0) {
					return s_value;
				}
			}), function(value) {
				return value;
			});

			if (a_external.length > 0) {
				Ext.Loader.loadScriptsSync(a_external);
			}

			if (a_internal.length > 0) {
				Ext.syncRequire(a_internal);
			}
		}

		f_callback();
	}
});
