Ext.define("Portfolio.settings.control.controller.Main", {
	extend: "Ext.app.ViewController",

	alias: "controller.portfolio-settings-control-controller-main",

	loadData: function(i_panel) {

		var i_controller = this;

		var i_tabpanel = i_panel.getTabPanel();
		var i_urlPanel = i_panel.getUrlPanel();
		var i_appPanel = i_panel.getAppPanel();

		i_controller.getFile(function(o_result) {
			i_urlPanel.form.setValues(o_result);
			i_appPanel.form.setValues(o_result);

			i_tabpanel.setActiveTab(o_result.active || 0);
		});
	},

	saveToFile: function() {

		var i_controller = this;
		var s_data = i_controller.save();

		Ext.Ajax.request({
			url: "/ip2-services/webBios.aspx",
			params: {
				func: "writestmf",
				data: s_data,
				file: "/portfolio.json"
			}
		});
	},

	getFile: function(f_callback) {

		Ext.Ajax.request({
			url: "/ip2-services/webBios.aspx",
			params: {
				func: "readstmf",
				file: "/portfolio.json"
			},
			success: function (o_response) {

				var o_result = Ext.decode(o_response.responseText, true) || {
					url: "",
					appName: "",
					appFolder: "",
					main: "",
					requires: [],
					appConfig: "{}"
				};

				f_callback(o_result);
			}
		});
	},

	save: function() {

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

		var i_tabpanel = i_view.getTabPanel();

		var i_urlPanel = i_view.getUrlPanel();
		var i_appPanel = i_view.getAppPanel();

		var o_url = i_urlPanel.getValues();
		var o_app = i_appPanel.getValues();

		var o_data = Ext.Object.merge({}, o_url, o_app);

		o_data.active = i_tabpanel.items.indexOf(i_tabpanel.getActiveTab());

		return JSON.stringify(o_data, null, 4);
	}
});
