Ext.define("Portfolio.registry.view.Copy", {

	extend: "Ext.window.Window",

	alias: "widget.registry.view.copy",

	statics: {
		regexKey: /^([a-zA-Z]\w*)(\.[a-zA-Z\-0-9]*)*$/
	},

	collapsible: true,
	viewModel: {
		type: "registry"
	},

	controller: "registry.view.detail",

	layout: "fit",
	constrain: true,
	readOnly: false,
	record: null,
	tree: null,

	alternateClassName: [
		"Ext.registry.view.Copy"
	],

	requires: [
		"Portfolio.registry.viewmodel.Registry",
		"Portfolio.registry.controller.view.Detail"
	],

	initComponent: function() {

		var i_self = this;

		i_self.callParent(arguments);

		var i_viewModel = i_self.getViewModel();
		var o_options = i_self.options;

		i_viewModel.setData({
			record: i_self.record,
			tree: i_self.tree
		});

		delete i_self.record;
		delete i_self.tree;

		var i_form = i_self.createLayout();
		i_self.add(i_form);
		i_self._form = i_form;

		return i_self;
	},

	createLayout: function() {

		var i_self = this;

		var i_form = Ext.widget("form", {
			layout: "anchor",
			bodyPadding: 5,
			trackResetOnLoad: true,
			defaults: {
				labelWidth: 140,
				anchor: "100%"
			},
			items: i_self.createItems(),
			buttons: i_self.createButtons()
		});

		i_form.on({
			dirtychange: {
				fn: function(i_form, b_dirty) {
					var i_button = i_form.owner.down("#btn-ok");
					if (i_button) {
						if (!b_dirty) {
							i_button.disable();
						}
						else {
							i_button.enable();
						}
					}
				}
			},
			validitychange: {
				fn: function(i_form, b_valid) {
					var i_button = i_form.owner.down("#btn-ok");
					if (i_button) {
						if (!b_valid) {
							i_button.disable();
						}
						else {
							var b_dirty = i_form.isDirty();
							if (b_dirty) {
								i_form.fireEvent("dirtychange", i_form, b_dirty);
							}
						}
					}
				}
			}
		});

		return i_form;
	},

	createItems: function() {

		var i_self = this;
		var i_viewModel = i_self.getViewModel();
		var i_record = i_viewModel.get("record");
		var a_items = [];

		var i_keyField = Ext.widget("textfield", {
			fieldLabel: ip2.i18n({
				da: "Fra",
				en: "From"
			}),
			name: "from",
			value: i_record.get("key"),
			readOnly: true
		});

		var i_replaceField = Ext.widget("textfield", {
			fieldLabel: ip2.i18n({
				da: "Til",
				en: "To"
			}),
			name: "to",
			readOnly: false,
			allowBlank: false,
			value: i_record.get("key")
		});

		i_replaceField.getErrors = (function(f_old) {
			return function(value) {

				value = value || this.getValue();
				var a_errors = f_old.apply(this, arguments) || [];

				if (value) {
					if (!Portfolio.registry.view.Copy.regexKey.test(value)) {
						a_errors.push(ip2.i18n({
							da: "Ikke en valid værdi",
							en: "Not a valid value"
						}));
					}
				}
				return a_errors;
			};
		})(i_replaceField.getErrors);

		a_items.push(i_keyField);
		a_items.push(i_replaceField);

		return a_items;
	},

	createButtons: function() {

		var i_self = this;
		var i_controller = i_self.getController();
		var i_viewModel = i_self.getViewModel();

		var i_tree = i_viewModel.get("tree");
		var i_record = i_viewModel.get("record");

		var a_buttons = [
			{
				xtype: "button",
				text: ip2.i18n({
					da: "Ok",
					en: "Ok"
				}),
				itemId: "btn-ok",
				disabled: true,
				handler: function() {
					var s_msg = ip2.i18n({
						da: "Kopier....",
						en: "Copying...."
					});
					i_self.el.mask(s_msg);
					var i_form = i_self._form.getForm();
					var o_values = i_form.getFieldValues();
					i_controller.doCopy(o_values, function(b_result) {
						i_self.el.unmask();
						if (b_result) {
							i_controller.doParentReload();
							i_self.close();
						}
					});
				}
			},
			{
				xtype: "button",
				text: ip2.i18n({
					da: "Luk",
					en: "Close"
				}),
				itemId: "btn-close",
				handler: function() {
					i_self.close();
				}
			}
		];

		return a_buttons;
	}
});
