ip2.declare("DFD.widgets.SpeedReport", {
	xtype: "container",
	closable: true,
	layout: "fit",
	requires: [
		"Ext.dfd.ux.reports.speed.model.Speed",
		"Ext.dfd.ux.reports.speed.view.Chart"
	],
	onInitialize: function(i_widget, i_owner) {

		var i_self = i_widget;

		var i_store = Ext.create("Ext.data.Store", {
			model: "Ext.dfd.ux.reports.speed.model.Speed",
			remoteFilter: true,
			listeners: {
				beforeload: {
					fn: function(i_store, i_operation) {

						var i_proxy = i_store.getProxy();
						var o_extraParams = i_proxy.getExtraParams();
						var i_form = i_self.form.getForm();
						var o_values = i_form.getFieldValues();
						var i_picker = i_form.findField("lgrmtip");
						var i_store = i_picker.getStore();
						var i_model = i_store.getModel();

						var i_record = i_picker.getSelectedRecord();

						o_values[i_model.idProperty] = -1;

						if (i_record) {

							var s_value = i_record.get("SPDTCPIP") || i_record.get("SPDTAGSRV");

							o_values.lgrmtip = s_value;

							if (o_values.lgrmtip) {
								var s_regex = /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/g;
								var a_match = o_values.lgrmtip.match(s_regex);
								if (!a_match) {
									o_values.lgrmtip = "172.22.1.70";
								}
								else if (a_match.length > 0) {
									o_values.lgrmtip = a_match[0];
								}
							}

							o_values[i_model.idProperty] = i_record.get(i_model.idProperty);
						}
						else {
							o_values.lgrmtip = "";
						}

						Ext.apply(o_extraParams, {
							action: "flwRespRap.listResponseTime",
							lastHours: 1,
							icebreakserver: 2,
							orderby: "scantime asc"
						}, o_values);
					}
				}
			}
		});

		var i_panel = Ext.widget("panel", {
			layout: "border",
			items: i_self.createItems(i_store),
			listeners: {
				afterrender: {
					fn: function(i_panel) {
						i_store.load();
					}
				}
			}
		});

		i_self.add(i_panel);
	},

	createItems: function(i_store) {

		var i_self = this;

		var a_items = [];

		var i_chart = i_self.createChart(i_store);

		a_items.push(i_chart);
		a_items.push(i_self.createNorth(i_chart));
		a_items.push(i_self.createGrid(i_store));

		return a_items;
	},

	createNorth: function(i_chart) {

		var i_self = this;

		var i_date = new Date();

		i_date.setHours(i_date.getHours() - 1);

		var s_first = ip2.date2dbDate(i_date);

		i_date.setHours(i_date.getHours() + 1);

		var s_second = ip2.date2dbDate(i_date);

		var i_panel = Ext.widget("form", {
			layout: {
				type: "hbox",
				align: "middle"
			},
			region: "north",
			height: 100,
			bodyPadding: 5,
			items: [
				{
					xtype: "entitycombo",
					dmd: "dmd/sitemuleflow.dmd",
					entity: "FLWSPDET00",
					view: "portfolio",
					itemId: "filter",
					name: "lgrmtip",
					fieldLabel: ip2.i18n({
						da: "Scanner",
						en: "Scanner"
					}),
					flex: 1,
					labelWidth: 80,
					getRenderFn: function() {

						var i_field = this;
						var a_fields = i_field.getDisplayFields();

						var f_renderer = function (v) {

							var s_value = null;
							var a_values = [];

							Ext.Array.each(a_fields, function(s_field) {
								if (v[s_field]) {
									a_values.push(ip2.i18n(v[s_field]));
								}
							});

							var s_value = v.SPDTCPIP || v.SPDTAGSRV;

							if (s_value) {

								var s_regex = /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/g;
								var a_match = s_value.match(s_regex);
								if (!a_match) {
									a_values.push("172.22.1.70");
								}
								else if (a_match.length > 0) {
									a_values.push(a_match[0]);
								}
							}
							if (a_values.length > 0) {
								if (a_values.length == 1) {
									s_value = a_values[0];
								}
								else {
									s_value = a_values.join(" - ");
								}
							}

							return s_value;
						};

						return f_renderer;
					}
				},
				{
					itemId: "fromTS",
					name: "fromTS",
					fieldLabel: "Fra",
					labelWidth: 30,
					value: s_first
				},
				{
					itemId: "toTS",
					name: "toTS",
					fieldLabel: "Til",
					labelWidth: 30,
					value: s_second
				},
				{
					xtype: "button",
					text: "Hent",
					width: 120,
					handler: function() {

						var i_grid = i_self.grid;
						var i_store = i_grid.getStore();
						i_store.load();
					}
				},
				{
					xtype: "button",
					text: ip2.i18n({
						da: "Zoom ud",
						en: "Zoom out"
					}),
					width: 120,
					handler: function() {

						var i_innerchart = i_chart.items.get(0);
						var i_interaction = i_innerchart.getInteractions()[0];
						var i_undoButton = i_interaction.getUndoButton();
						var f_handler = i_undoButton.handler;
						if (f_handler) {
							f_handler();
						}
					}
				}
			],
			defaults: {
				xtype: "isotimestampfield",
				margin: "0 0 0 10",
				width: 220
			}
		});

		i_self.form = i_panel;

		return i_panel;
	},

	createChart: function(i_store) {

		var i_self = this;

		var i_chart = Ext.widget("dfd.ux.reports.speed.chart", {
			flex: 2,
			region: "center",
			layout: "fit",
			store: i_store,
			itemHandler: function(i_record) {
				i_self.itemHandler(i_record);
			}
		});

		return i_chart;
	},

	createGrid: function(i_store) {

		var i_self = this;

		var i_panel = Ext.widget("panel", {
			region: "south",
			split: true,
			flex: 1,
			layout: "border",
			defaults: {
				margin: "0 5 0 0"
			}
		});

		var i_gridpanel = Ext.widget("gridpanel", {
			store: i_store,
			region: "center",
			flex: 2,
			columns: [
				{
					dataIndex: "deviceid",
					header: "deviceid",
					flex: 1,
					hidden: true
				},
				{
					dataIndex: "spddesc",
					header: "spddesc",
					flex: 1
				},
				{
					dataIndex: "lgrmtip",
					header: "lgrmtip",
					flex: 1,
					hidden: true
				},
				{
					dataIndex: "scantime",
					header: "scantime",
					flex: 1
				},
				{
					dataIndex: "wsstrtime",
					header: "wsstrtime",
					flex: 1
				},
				{
					dataIndex: "wsendtime",
					header: "wsendtime",
					flex: 1
				},
				{
					dataIndex: "scan2wsstr",
					header: ip2.i18n({
						da: "Extern arbejdstid",
						en: "External worktime"
					}),
					flex: 1
				},
				{
					dataIndex: "wsworktime",
					header: ip2.i18n({
						da: "Icebreak arbejdstid",
						en: "Icebreak worktime"
					}),
					flex: 1
				}
			],
			listeners: {
				select: {
					fn: function(i_view, i_record) {

						var i_grid = i_self.grid;
						var i_textarea = i_grid.textValues;

						if (i_textarea) {
							var s_data = "";
							var data = i_record.get("scannerdata");
							if (data && Ext.isArray(data)) {

								Ext.Array.each(data, function(o_value) {
									s_data += JSON.stringify(o_value, null, 4) + "\n";
								});
							}
							i_textarea.setValue(s_data);
						}
					}
				}
			}
		});

		var i_textarea = Ext.widget("acetextarea", {
			anchor: "100% 100%",
			mode: "json",
			useWorker: false
		});

		var i_fieldset = Ext.widget("fieldset", {
			title: "Scanner Data",
			flex: 1,
			layout: "anchor",
			region: "east",
			items: [
				i_textarea
			]
		});

		i_self.grid = i_gridpanel;

		i_gridpanel.textValues = i_textarea;

		i_panel.add(i_gridpanel);
		i_panel.add(i_fieldset);

		return i_panel;
	},

	itemHandler: function(i_record) {

		var i_self = this;
		var i_grid = i_self.grid;

		if (i_grid) {

			var i_store = i_grid.getStore();
			var n_index = i_store.indexOf(i_record);

			if (n_index) {

				i_grid.bufferedRenderer.scrollTo(n_index,
					{
						select: true,
						focus: false
					}
				);
			}
		}
	}
});
