Ext.define("Ext.ux.form.field.UploadPickerRelation", {
	extend: "Ext.form.FieldContainer",
	mixins: {
		field: 'Ext.form.field.Field'
	},
	alias: "widget.uploadpickerrelation",

	picker: null,
	layout: "fit",
	relation: null,
	model: null,

	value: '',

	initComponent: function() {

		var i_self = this;

		i_self.callParent();

		i_self.add(i_self.createPicker());

		i_self.initField();

		delete i_self.relation;

		i_self.on({
			afterrender: {
				fn: function(i_field) {
					var i_record = i_field.record;
					var s_key = i_record != undefined ? i_record.idProperty : '';
					var n_tkn = i_record != undefined ? i_record.get(s_key) : 0;
					i_self.getDBRelation(n_tkn, function(i_relation) {
						if (i_relation) {
							var i_picker = i_self.getPicker();
							i_picker.setValue(i_relation.get("RELCHDTKN"), function() {
								var i_xpdeditorfield = null;
								i_self.setValue(i_relation.get("RELCHDTKN"));
								i_picker.resetOriginalValue();
								i_self.resetOriginalValue();

								i_xpdeditorfield = i_self.up("xpdeditorfield");
								if (i_xpdeditorfield) {
									i_xpdeditorfield.resetOriginalValue();
									i_xpdeditorfield.fireEvent("reset", i_xpdeditorfield);
								}
								var i_entityform = i_self.up("entityform");
								if (i_entityform) {
									i_entityform.fireEvent("dirtychange", i_entityform);
								}
							});
						}
					});
					i_self.picker.on({
						change: {
							fn: function(i_picker, value) {
								i_self.setValue(value);
							}
						}
					});
				}
			}
		});
	},

	createPicker: function() {
		var i_self = this;
		var o_config = ip2.extend(i_self.initialConfig, {
			submitRaw: false,
			isFormField: false,
			isWidget: true
		});

		delete o_config.fieldLabel;
		var i_picker = Ext.widget("uploadpickermanager", o_config);
		i_self.picker = i_picker;
		return i_picker;
	},

	getModel: function() {
		var i_self = this;
		if (!i_self.model) {
			i_self.model = ip2.model({
				dmd: "dmd/webshop.dmd",
				entityName: "WSHRELET00",
				context: 'grid',
				uiName: "portfolio"
			});
		}
		return i_self.model;
	},

	getTypeEnumValue: function (s_key, enumValue) {

		var i_self = this;
		var s_lang = wsh.i18n.getPrimary();

		var i_ns = ip2.resource.get('adm.ns', true);
		var o_value = i_ns.getEnumValue(s_key + '~' + enumValue);

		return o_value[s_lang];
	},

	getRelation: function() {
		var i_self = this;
		return i_self.relation;
	},

	getDBRelation: function(n_tkn, f_callback) {
		var i_self = this;
		var n_parentrelationtype = i_self.getTypeEnumValue('parentRelationTypes', i_self.parentEnumType);
		var n_childrelationtype = i_self.getTypeEnumValue('childRelationTypes', i_self.childEnumType);
		var a_batch = [];

		a_batch.push({
			type: 'get',
			dmd: 'dmd/webshop.dmd',
			entity: 'WSHRELET00',
			context: 'grid',
			uiName: 'portfolio',
			where: 'RELPNTTKN = ' + n_tkn + " AND RELPNTTYP =" + n_parentrelationtype + " AND  RELCHDTYP = " + n_childrelationtype,
			page: 1,
			start: 0,
			limit: -1
		});

		Ext.Ajax.request({
			url: '/ip2-Services/ip2dmdproxy.aspx',
			jsonData: {
				transaction: {
					batch: a_batch
				}
			},
			success: function (o_response) {

				var o_result = JSON.parse(o_response.responseText);
				var o_batch = o_result.batch[0];
				var o_relation;
				var i_relation;
				var Model = i_self.getModel();
				if (o_batch && o_batch.rows) {
					if (o_batch.rows.length > 0) {
						o_relation = o_batch.rows[0];
						o_relation.RELPNTTYP = n_parentrelationtype;
						o_relation.RELCHDTYP = n_childrelationtype;
						o_relation.RELPNTTKN = n_tkn;

						i_relation = Model.create(o_relation);
						i_self.relation = i_relation;
					}
				}
				f_callback(i_relation);
			}
		});
	},

	getPicker: function() {
		var i_self = this;
		return i_self.picker;
	},

	getValue: function() {

		var i_self = this;
		var i_picker = i_self.getPicker();
		if (i_self.submitRaw) {
			var i_relation = i_self.getRelation();
			if (i_relation) {
				delete i_self.relation;
				i_self.deleteRelation(i_relation, function() {});
			}
			return i_picker.getRawValue();
		}
		else {
			return i_picker.getValue();
		}
	},

	setValue: function(value) {
		var i_self = this;
		if (i_self.getPicker()) {
			i_self.picker.setValue(value);
		}
		i_self.mixins.field.setValue.call(i_self, value);
	},

	doSave: function(f_callback) {
		var i_self = this;
		var i_picker = i_self.getPicker();
		var i_relation = i_self.getRelation();
		if (i_self.submitRaw) {
			i_self.deleteRelation(i_relation, function() {
				delete i_self.relation;
				f_callback(i_self.getValue());
			});
		}
		else {
			i_picker.doSave(function() {
				var n_value = i_self.getValue();
				var i_relation = i_self.getRelation();
				if (!i_relation) {
					var n_parentrelationtype = i_self.getTypeEnumValue('parentRelationTypes', i_self.parentEnumType);
					var n_childrelationtype = i_self.getTypeEnumValue('childRelationTypes', i_self.childEnumType);
					i_relation = i_self.getModel().create({
						RELPNTTYP: n_parentrelationtype,
						RELCHDTYP: n_childrelationtype,
						RELPNTTKN: i_self.record.get(i_self.record.idProperty)
					});
				}
				i_relation.set("RELCHDTKN", n_value);
				i_self.upsertRelation(i_relation, function(i_relation) {
					i_self.relation = i_relation;
					f_callback(i_relation);
				});
			});
		}
	},

	deleteRelation: function(i_record, f_callback) {

		if (i_record) {
			var i_self = this;
			var a_batch = [];

			var key = {};
			key[i_record.idProperty] = i_record.get(i_record.idProperty);
			a_batch.push({
				type: 'delete',
				dmd: 'dmd/webshop.dmd',
				entity: "WSHRELET00",
				key: key
			});

			Ext.Ajax.request({
				url: '/ip2-Services/ip2dmdproxy.aspx',
				jsonData: {
					transaction: {
						batch: a_batch
					}
				},
				success: function (o_response) {
					f_callback();
				}
			});
		}
		else {
			f_callback();
		}
	},

	upsertRelation: function(i_relation, f_callback) {
		var i_self = this;
		var a_batch = [];

		var key = {};
		key[i_relation.idProperty] = i_relation.get(i_relation.idProperty);
		a_batch.push({
			type: 'upsert',
			dmd: 'dmd/webshop.dmd',
			entity: "WSHRELET00",
			key: key,
			row: i_relation.data
		});

		Ext.Ajax.request({
			url: '/ip2-Services/ip2dmdproxy.aspx',
			jsonData: {
				transaction: {
					batch: a_batch
				}
			},
			success: function (o_response) {
				var o_result = JSON.parse(o_response.responseText);
				var o_batch = o_result.batch[0];
				var o_record = o_batch.row;
				i_relation.set(o_record);
				f_callback(i_relation);
			}
		});
	}
});
