Ext.define("Portfolio.settings.language.view.Main", {

	xtype: "portfolio-settings-language-main",

	title: ip2.i18n({
		da: "Sprog",
		en: "Languages"
	}),

	extend: "Ext.panel.Panel",

	config: {
		grid: null,
		actionBar: null,
		localeStore: null
	},

	layout: "fit",

	controller: "portfolio-settings-language-main",

	requires: [
		"Portfolio.settings.language.controller.Main",
		"Portfolio.settings.language.view.Window"
	],

	initComponent: function() {

		var i_self = this;

		i_self.callParent(arguments);

		i_self.createLocaleStore();

		var o_access = ip2.getUser().get("ACCESS");

		ip2.getUser().set("ACCESS", true);

		var i_grid = Ext.widget("entity.grid", {
			dmd: {
				path: "dmd/portfolio.dmd",
				entityName: "NAVLNG"
			},
			uiName: "portfolio",
			listeners: {
				afterrender: {
					fn: "onAfterRender"
				}
			}
		});

		ip2.getUser().set("ACCESS", o_access);

		i_grid.setTitle("");
		i_grid.addDocked(i_self.createToolbar());

		i_self.createColumns(i_grid);

		i_self.setGrid(i_grid);

		i_self.add(i_grid);
	},

	createToolbar: function() {

		var i_self = this;

		var i_toolbar = Ext.widget("toolbar", {
			dock: "top",
			items: [
				{
					xtype: "button",
					iconCls: "pf-add",
					text: ip2.i18n({
						da: "Tilføj",
						en: "Add"
					}),
					handler: "onAdd"
				}
			]
		});

		i_self.setActionBar(i_toolbar);

		return i_toolbar;
	},

	createLocaleStore: function() {

		var i_self = this;

		var i_store = Ext.create("Ext.ux.data.store.Locale", {});

		i_self.setLocaleStore(i_store);
	},

	createColumns: function(i_grid) {

		var i_self = this;

		var i_localeStore = i_self.getLocaleStore();

		var i_columnManager = i_grid.getColumnManager();
		var a_columns = i_columnManager.getColumns();

		var a_result = [];

		Ext.Array.each(a_columns, function(i_column) {
			a_result.push(i_column.initialConfig);
		});

		var a_config = [
			{
				dataIndex: "country",
				width: 80,
				align: "center",
				sortable: false,
				renderer: function(value, i_meta, i_record) {

					var i_template = Ext.create("Ext.XTemplate",
						"<div ",
							"style=\"",
								"height:10px; ",
								"width:32px; ",
								"display:inline-block; ",
								"margin:0px 2px 0px 0px; ",
								"padding:0px;\" ",
							"class=\"",
								"AIL-icon AIL-icon-{width} icon-{flag}\">",
						"</div>"
					);

					i_template.compile();

					var s_lang = i_record.get("LNGID");

					var i_locale = i_localeStore.findRecord("code", s_lang);

					var s_return = s_lang;

					if (i_locale) {
						s_return = i_template.apply({
							flag: i_locale.get("flag"),
							width: 16
						});
					}

					return s_return;
				}
			},
			{
				dataIndex: "language",
				header: ip2.i18n({
					da: "Sprog",
					en: "Language"
				}),
				flex: 1,
				sortable: false,
				renderer: function(value, i_meta, i_record) {

					var s_lang = i_record.get("LNGID");

					var i_locale = i_localeStore.findRecord("code", s_lang);

					var s_return = s_lang;

					if (i_locale) {
						s_return = ip2.i18n(i_locale.get("language"));
					}

					return s_return;
				}
			},

		];

		a_result = a_config.concat(a_result);

		i_grid.reconfigure(null, a_result);
	}
});
