Ext.define("Ext.ux.link.Window", {

	extend: "Ext.window.Window",

	title: ip2.i18n({
		da: "Direkte Link",
		en: "Direct Link"
	}),

	layout: "fit",
	alias: [
		"widget.link.window"
	],

	config: {
		urlField: null,
		hideField: null,
		model: null,
		activeTabIndex: 0,
		app: null
	},

	width: 650,
	modal: true,
	ui: "message",

	"static": {
		tpl: "{protocol}//{host}{pathname}{search}{hash}",
		appTpl: "{app}-{paths}-{config}"
	},

	initComponent: function() {

		var i_self = this;
		i_self.callParent(arguments);
		i_self.add(i_self.createLayout());
		return i_self;
	},

	createLayout: function() {

		var i_self = this;

		var i_panel = Ext.widget("panel", {
			layout: {
				type: "vbox",
				align: "stretch"
			},
			defaults: {
				anchor: "100%"
			},
			bodyPadding: 20,
			items: i_self.createItems()
		});

		return i_panel;
	},

	createItems: function() {

		var i_self = this;

		var a_fields = [];

		var i_hide = Ext.widget("checkboxgroup", {
			name: "hide",
			columns: 6,
			vertical: false,
			items: [
				{
					boxLabel: ip2.i18n({
						da: "Topbar",
						en: "Header"
					}),
					name: "check",
					inputValue: 1,
					checked: true
				},
				{
					boxLabel: ip2.i18n({
						da: "Navigation",
						en: "Navigation"
					}),
					name: "check",
					inputValue: 2,
					checked: true
				},
				{
					boxLabel: ip2.i18n({
						da: "Hjælp",
						en: "Help"
					}),
					name: "check",
					inputValue: 4,
					checked: true
				},
				{
					boxLabel: ip2.i18n({
						da: "Faneblade",
						en: "Tabs"
					}),
					name: "check",
					inputValue: 8,
					checked: true
				},
				{
					boxLabel: ip2.i18n({
						da: "Meddelelser",
						en: "Messages"
					}),
					name: "check",
					inputValue: 16,
					checked: true
				},
				{
					boxLabel: ip2.i18n({
						da: "Vindue",
						en: "Window"
					}),
					name: "window",
					inputValue: 1,
					checked: false
				}

			],
			listeners: {
				change: {
					fn: function(i_field) {

						var o_value = i_field.getValue();
						var n_hide = 31;
						var n_window = 0;
						var i_url = i_self.getUrlField();

						if (o_value.check) {

							var a_values = o_value.check;
							if (!Ext.isArray(o_value.check)) {
								a_values = Ext.Array.from(o_value.check);
							}

							Ext.Array.each(a_values, function(n_value) {
								n_hide -= n_value;
							});
						}
						if (o_value.window) {
							n_window = o_value.window;
						}
						i_url.setValue(i_self.getUrl(n_hide, n_window));
					}
				}
			}
		});

		var i_url = Ext.widget("textfield", {
			name: "url",
			readOnly: true,
			listeners: {
				afterrender: {
					fn: function(i_field) {

						i_field.setValue(i_self.getUrl(0));

						var i_copy = i_field.getTrigger("copy");

						if (i_copy) {
							i_copy.show();
							i_copy.field.readOnly = false;
						}
					},
					buffer: 10
				}
			},
			triggers: {
				copy: {
					cls: "x-ux-form-trigger-copy",
					tooltip: ip2.i18n({
						da: "Kopier til udklipsholderen",
						en: "Copy to clipboard"
					}),
					handler: function() {
						var i_field = i_self.getUrlField();
						var i_inputDom = i_field.inputEl.dom;
						i_inputDom.select();
						document.execCommand("copy");
					},
					isFieldEnabled: function() {
						return true;
					}
				}
			}
		});

		var i_email = Ext.widget("textfield", {
			name: "email",
			vtype: "email",
			allowBlank: true,
			emptyText: ip2.i18n({
				da: "Indtast modtagers e-mail og send",
				en: "Type in recievers e-mail and send"
			}),
			listeners: {
				change: {
					fn: function(i_field, s_value) {

						var i_trigger = i_field.getTrigger("send");
						var b_valid = i_field.isValid();

						if (i_trigger) {
							i_trigger[b_valid && s_value ? "show" : "hide"]();
						}
					}
				},
				validitychange: {
					fn: function(i_field, b_valid) {

						var i_trigger = i_field.getTrigger("send");
						
						var s_value = i_field.getValue();

						if (i_trigger) {
							i_trigger[b_valid && s_value ? "show" : "hide"]();
						}
					} 
				}
			},
			triggers: {
				send: {
					hidden: true,
					cls: "x-ux-form-trigger-send",
					handler: function() {
						if (i_email.isValid()) {

							var i_template = Ext.create("Ext.XTemplate",
								"mailto:{email}?body={[this.encodeBody(values.body)]}",
								{
									encodeBody: function(value) {
										return encodeURIComponent(value);
									}
								}
							);

							var s_mail = i_email.getValue();

							var s_value = i_template.apply({
								email: s_mail,
								body: i_url.getValue()
							});

							window.open(s_value, "_blank");
						}
						
					}
				}
			}
		});

		

		a_fields.push(i_hide);
		a_fields.push(i_url);
		a_fields.push(i_email);

		i_self.setUrlField(i_url);
		i_self.setHideField(i_hide);

		return a_fields;
	},

	getUrl: function(n_hide, n_window) {

		var i_self = this;

		var s_template = i_self["static"].tpl;

		var s_value = "";

		var o_config = {
			protocol: window.location.protocol,
			host: window.location.host,
			pathname: window.location.pathname,
			search: ""
		};

		var i_model = i_self.getModel();
		var o_app = i_self.getApp();

		s_value = s_value.replace("{protocol}", window.location.protocol);
		s_value = s_value.replace("{host}", window.location.host);
		s_value = s_value.replace("{pathname}", window.location.pathname);

		if (n_hide) {
			Ext.apply(o_config, {
				search: "?layout=" + n_hide
			});
		}

		if (i_model) {

			var s_hash;

			if (i_model.entity) {

				if (n_window) {
					s_hash = "#" + n_window + "-entity-" + i_model.entity.name;
				}
				else {
					s_hash = "#entity-" + i_model.entity.name;
				}

				if (i_model.get(i_model.idProperty)) {
					s_hash = s_hash + "-" + i_model.get(i_model.idProperty);
					if (i_self.getActiveTabIndex()) {
						s_hash = s_hash + "-" + i_self.getActiveTabIndex();
					}
				}
				else {
					s_hash = s_hash + "-" + i_model.view;
				}

				if (i_model.dmdFile) {
					s_hash = s_hash + "-" + i_model.dmdFile;
				}

				Ext.apply(o_config, {
					hash: s_hash
				});
			}
			else if (i_model.widget) {

				if (i_model.widget.name) {

					if (n_window) {
						s_hash = "#" + n_window + "-widget-" + i_model.widget.name;
					}
					else {
						s_hash = "#widget-" + i_model.widget.name;
					}
				}
				else {

					if (i_model.entity) {

						if (n_window) {
							s_hash = "#" + n_window + "-widget-" + i_model.entity.name;
						}
						else {
							s_hash = "#widget-" + i_model.entity.name;
						}

						if (i_model.get(i_model.idProperty)) {
							s_hash = s_hash + "-" + i_model.get(i_model.idProperty);
						}
						if (i_model.xpd) {
							s_hash = s_hash + "-" + i_model.xpd;
						}
						if (i_model.dmdFile) {
							s_hash = s_hash + "-" + i_model.dmdFile;
						}
					}
				}
			}
			else if (i_model.dynapp) {

				s_hash = "#dynapp-" + i_model.dynapp.library + "-" + i_model.dynapp.file;
			}

			Ext.apply(o_config, {
				hash: s_hash
			});
		}
		else if (o_app) {

			s_template = i_self["static"].appTpl;

			var s_apptpl = n_window ? "1-" + s_apptpl : s_apptpl;

			o_config = {
				app: o_app.main,
				paths: Ext.urlEncode(o_app.paths),
				config: Ext.urlEncode(o_app.config),
				hash: "#" + s_apptpl
			};
		}
		else {

			Ext.apply(o_config, {
				hash: window.location.hash
			});
		}

		var i_template = Ext.create("Ext.XTemplate", s_template);

		i_template.compile();

		s_value = i_template.apply(o_config);

		return s_value;
	}
});
