Ext.define('Ext.ux.importManager.Grid', {
	extend: 'Ext.grid.Panel',
	alias: 'widget.importManagerGrid',
	border: 0,
	layout: {
		type: 'fit'
	},
	autoScroll: true,
	width: '100%',
	height: '100%',
	columnLines: true,
	viewConfig: {
		stripeRows: true
	},
	initComponent: function() {
		var me = this;

		me.setupI18N();

		var modelName = 'importManagerRows';

		if (!Ext.ClassManager.isCreated(modelName)) {
			Ext.define(modelName, {
				extend: 'Ext.data.Model',
				proxy: {
					type: 'ajax',
					url: '/wsh-services/wshImpMgr.aspx?action=list',
					reader: {
						type: 'json',
						rootProperty: 'rows',
						totalProperty: 'totalRows'
					}
				}
			});
		}

		me.columns = [{
			header: me.getI18N('orginal'),
			columns: [
				{ header: me.getI18N('type'), dataIndex: 'type', width: 80 },
				{ header: me.getI18N('subType'), dataIndex: 'subType', width: 80 },
				{ header: me.getI18N('description'), dataIndex: 'title', width: 300 },
				{ header: me.getI18N('server'),
					columns: [
						{ header: me.getI18N('name'), dataIndex: 'server', width: 100 },
						{ header: me.getI18N('description'), dataIndex: 'serverDesc', width: 200 }
					]
				},
				{ header: me.getI18N('Export'),
					columns: [
						{ header: me.getI18N('languages'), dataIndex: 'exportlanguages', width: 100, renderer: me.rendererLanguages },
						{ header: me.getI18N('time'), dataIndex: 'exportTime', width: 160 }
					]
				}
			]
		},{
			header: me.getI18N('Import'),
			columns: [
				{ header: me.getI18N('token'), dataIndex: 'importToken', width: 80 },
				{ header: me.getI18N('languages'), dataIndex: 'importlanguages', width: 100, renderer: me.rendererLanguages },
				{ header: me.getI18N('time'), dataIndex: 'importTime', width: 160 }
			]
		}];

		me.store = Ext.create('Ext.data.Store', {
			model: modelName,
			autoLoad: true
		});

		me.dockedItems = [{
			xtype: 'toolbar',
			dock: 'top',
			items: [{
				xtype: 'button',
				text: me.getI18N('refresh'),
				iconCls: 'pf-refresh',
				handler: function() {
					me.store.load();
				}
			}]
		}];

		var rowMenu = Ext.create('Ext.menu.Menu', {
			items: [{
				xtype: 'menuitem',
				text: me.getI18N('doImport'),
				iconCls: 'pf-arrow_down',
				handler: function() {
					me.doImport(me.node);
				}
			}]
		});

		me.listeners = {
			itemdblclick: function(view, record, item, index, e) {
				e.preventDefault();
				me.doImport(record);
			},
			itemcontextmenu: function(view, records, item, index, e) {
				e.stopEvent();

				me.getSelectionModel().select(records);

				var coords = e.getXY();
				rowMenu.showAt([coords[0], coords[1]]);
			}
		};

		me.callParent();
	},

	/* ----------------------------------------------------------------------------------------------------
	    	Renderer languages
	   ---------------------------------------------------------------------------------------------------- */
	rendererLanguages: function(languages) {
		var html = '';

		if (Ext.isArray(languages)) {
			for (var i = 0; i < languages.length; i++) {
				html += ip2.util.renderer.countryFlags(languages[i]);
			}
		}
		else {
			html = ip2.util.renderer.countryFlags(languages);
		}
		return html;
	},

	/* ----------------------------------------------------------------------------------------------------
	    	Import
	   ---------------------------------------------------------------------------------------------------- */
	doImport: function(record) {
		var me = this;

		var records = record ? [record] : me.getSelectionModel().getSelection();
		if (records.length == 0) {
			Ext.MessageBox.show({
				msg: 'Du skal vælge en række først.',
				icon: Ext.MessageBox.WARNING
			});

			setTimeout(function() {
				Ext.MessageBox.hide();
			}, 1000);
			return;
		}

		Ext.create('Ext.ux.importManager.Window', {
			record: records[0],
			mainOwner: me,
			success: function(data) {
				for (var key in data) {
					this.record.set(key, data[key]);
				}
			}
		}).show();
	},

	/* ----------------------------------------------------------------------------------------------------
		Setup I18N
	---------------------------------------------------------------------------------------------------- */
	setupI18N: function() {
		var texts = {
			ns: 'IP2',
			lang: ['da', 'en'],
			enums: {},
			text: {
				refresh: {
					value: { da: 'Opfrisk', en: 'Refresh' }
				},
				orginal: {
					value: { da: 'Oprindelse', en: 'Orginal' }
				},
				token: {
					value: { da: 'Token', en: 'Token' }
				},
				description: {
					value: { da: 'Beskrivelse', en: 'Description' }
				},
				server: {
					value: { da: 'Server', en: 'Server' }
				},
				name: {
					value: { da: 'Navn', en: 'Name' }
				},
				description: {
					value: { da: 'Beskrivelse', en: 'Description' }
				},
				type: {
					value: { da: 'Type', en: 'Type' }
				},
				subType: {
					value: { da: 'Under type', en: 'Subtype' }
				},
				languages: {
					value: { da: 'Sprog', en: 'Languages' }
				},
				exportTime: {
					value: { da: 'Eksport tidspunkt', en: 'Export time' }
				},
				Export: {
					value: { da: 'Eksport', en: 'Export' }
				},
				Import: {
					value: { da: 'Import', en: 'Import' }
				},
				time: {
					value: { da: 'Tidspunkt', en: 'Time' }
				},
				doImport: {
					value: { da: 'Importér', en: 'Import' }
				},
				doUpdate: {
					value: { da: 'Opdateres', en: 'Update' }
				},
				doCreate: {
					value: { da: 'Oprettes som nye', en: 'Add as new' }
				},
				close: {
					value: { da: 'Luk', en: 'Close' }
				},
				existing: {
					value: { da: 'Eksistende', en: 'Existing' }
				},
				overwrite: {
					value: { da: 'Overskrives', en: 'Overwrite' }
				},
				images: {
					value: { da: 'Billeder', en: 'Images' }
				},
				importImages: {
					value: { da: 'Importér billeder', en: 'Import images' }
				},
				existingImages: {
					value: { da: 'Eksistende billeder', en: 'Existing images' }
				},
				importLanguages: {
					value: { da: 'Importér sprog', en: 'Import languages' }
				},
				existingTexts: {
					value: { da: 'Eksistende tekster', en: 'Existing texts' }
				},
				confirmImportTitle: {
					value: { da: 'Bekræft import', en: 'Confirm import' }
				},
				confirmImport: {
					value: { da: 'Bekræft venligst import af: ', en: 'Please confirm import of: ' }
				}
			}
		};

		ip2.namespace('ip2.importmanager');
		ip2.importmanager = new ip2.DynamicNS(texts);
		ip2.importmanager.setLanguage(ip2.session.getLanguage());
	},

	/* ----------------------------------------------------------------------------------------------------
		get I18N by key
	---------------------------------------------------------------------------------------------------- */
	getI18N: function(key) {
		return ip2.i18n(ip2.importmanager.getTextValue(key));
	}
});
