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

	alias: "controller.icecap.controller.emulator",

	listen: {
		global: {
			onAddWebContainer: {
				fn: "onAddWebContainer"
			}
		}
	},

	onAfterRender: function() {

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

		i_controller.initKeys();
	},

	onNewSession: function() {

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

		var i_tabpanel = i_view.getTabArea();

		var i_icecapPanel = Ext.widget("icecap-panel", {
			title: "Icecap",
			closable: true,
			theme: i_view.getTheme(),
			options: i_view.getOptions()
		});

		var i_active = i_tabpanel.add(i_icecapPanel);
		i_tabpanel.setActiveTab(i_active);
	},

	onAddWebContainer: function (o_data) {

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

		var i_tabpanel = i_view.getTabArea();

		var i_webcontainer = Ext.widget("icecap-webcontainer", {
			src: o_data.cmd || o_data.url || o_data.src,
			title: o_data.cmdTitle || o_data.title,
			closable: true
		});

		var i_active = i_tabpanel.add(i_webcontainer);
		i_tabpanel.setActiveTab(i_active);
	},

	onStateNotify: function () {

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

		i_view.fireEvent("stateupdate", i_view);
	},

	initKeys: function () {

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

		var a_bindings = [
			{
				key: Ext.event.Event.ONE,
				alt: true,
				fn: function (n_key, i_event) {

					i_event.stopEvent();
					i_controller.handleKeys(1);
				}
			},
			{
				key: Ext.event.Event.TWO,
				alt: true,
				fn: function (n_key, i_event) {

					i_event.stopEvent();
					i_controller.handleKeys(2);
				}
			},
			{
				key: Ext.event.Event.THREE,
				alt: true,
				fn: function (n_key, i_event) {

					i_event.stopEvent();
					i_controller.handleKeys(3);
				}
			},
			{
				key: Ext.event.Event.FOUR,
				alt: true,
				fn: function (n_key, i_event) {

					i_event.stopEvent();
					i_controller.handleKeys(4);
				}
			},
			{
				key: Ext.event.Event.FIVE,
				alt: true,
				fn: function (n_key, i_event) {

					i_event.stopEvent();
					i_controller.handleKeys(5);
				}
			},
			{
				key: Ext.event.Event.SIX,
				alt: true,
				fn: function (n_key, i_event) {

					i_event.stopEvent();
					i_controller.handleKeys(6);
				}
			},
			{
				key: Ext.event.Event.SEVEN,
				alt: true,
				fn: function (n_key, i_event) {

					i_event.stopEvent();
					i_controller.handleKeys(7);
				}
			},
			{
				key: Ext.event.Event.EIGHT,
				alt: true,
				fn: function (n_key, i_event) {

					i_event.stopEvent();
					i_controller.handleKeys(8);
				}
			},
			{
				key: Ext.event.Event.NINE,
				alt: true,
				fn: function (n_key, i_event) {

					i_event.stopEvent();
					i_controller.handleKeys(9);
				}
			},
			{
				key: Ext.event.Event.F3,
				alt: true,
				fn: function (n_key, i_event) {

					i_event.stopEvent();

					var i_tabpanel = i_view.getTabArea();

					var i_active = i_tabpanel.getActiveTab();

					if (i_active) {
						var n_active = i_tabpanel.items.indexOf(i_active);

						n_active--;

						if (n_active > -1) {
							i_tabpanel.setActiveTab(n_active);
						}

						i_active.close();
					}
				}
			}
		];

		var i_keyMap = Ext.create("Ext.util.KeyMap", {
			target: Ext.getDoc().el,
			binding: a_bindings
		});

		var i_defaultMap = Ext.create("Ext.util.KeyMap", {
			target: Ext.getWin().el,
			binding: i_controller.getFKeys()
		});
	},

	handleKeys: function (n_key) {

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

		var n_length = i_tabpanel.items.getCount();

		var n_active = n_key - 1;

		i_tabpanel.setActiveTab(n_active);
	},

	getFKeys: function () {

		var i_controller = this;

		var a_keys = [
			{
				key: Ext.event.Event.F1,
				shift: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F2,
				shift: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F3,
				shift: false,
				alt: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F4,
				shift: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F5,
				shift: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F6,
				shift: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F7,
				shift: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F8,
				shift: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F9,
				shift: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F10,
				shift: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F11,
				shift: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F12,
				shift: false,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			}
		];

		a_keys = a_keys.concat(i_controller.getFShiftKeys());

		return a_keys;
	},

	getFShiftKeys: function () {

		var i_controller = this;

		var a_keys = [
			{
				key: Ext.event.Event.F1,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F2,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F3,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F4,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F5,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F6,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F7,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F8,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F9,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F10,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F11,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			},
			{
				key: Ext.event.Event.F12,
				shift: true,
				fn: function (n_key, i_event) {

					if (!i_controller.isAllowed(i_event)) {
						i_event.stopEvent();
					}
				}
			}
		];

		return a_keys;
	},

	isAllowed: function(i_event) {

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

		var i_element = Ext.get(i_event.browserEvent.target);
		var i_component = i_element.component;

		var b_allowed = true;

		if (i_component) {

			var s_xtype = i_component.xtype;

			if (Ext.getBody() == i_component.getEl()) {
					b_allowed = false;
			}
			else {
				b_allowed = i_view.getEl().contains(i_component.el);
			}
		}

		return b_allowed;
	}
});
