Ext.define("Portfolio.designer.ui.domainModelCardinalityComboBox", {

	extend: "Ext.form.field.ComboBox",

	requires: [
		"Portfolio.designer.model.domainModelCardinalityModel"
	],

	alias: [
		"widget.ip2DomainModelCardinalityComboBox"
	],

	queryMode: "local",

	matchFieldWidth: false,

	displayField: "value",

	valueField: "key",

	listConfig: {
		getInnerTpl: function() {
			return "<div><center><img src=\"/portfolio/styles/icon/flatui/designer/{value}.png\" /></center></div>";
		}
	},

	initComponent: function() {

		var me = this;

		me.store = Ext.create("Ext.data.Store", {
			model: "Portfolio.designer.model.domainModelCardinalityModel",
			data: [
				{
					"key": "one2many",
					"value": "one2many"
				},
				{
					"key": "many2one",
					"value": "many2one"
				},
				{
					"key": "many2many",
					"value": "many2many"
				},
				{
					"key": "one2one",
					"value": "one2one"
				}
			]
		});

		me.callParent();
	},

	setRawValue: function(value) {

		// set image before and after selection
		var me = this;
		var s_value = "/portfolio/styles/icon/flatui/designer/{value}.png".replace("{value}", value);
		me.setIconCls(me.getCSS(s_value));
		return "";
	},

	getRawValue: function() {
		var me = this;
		var v = me.callParent();
		if (!v) {
			v = "";
		}
		if (v === me.emptyText) {
			v = "";
		}
		return v;
	},

	getCSS: function(value) {
		var i_cmp = this;
		var s_name = i_cmp.getId() + "-css";
		var o_css = Ext.util.CSS.getRule("." + s_name);
		var a_value = value.split(".");
		var i_date = new Date();
		var n_time = i_date.getTime();
		var s_extensions = a_value.pop().toLowerCase();
		if (!o_css) {
			var s_url = value;
			if (Ext.ux.file.util.Handler.imageExtension.indexOf(s_extensions) == -1) {
				s_url = Ext.ux.file.util.Handler.getUrlByExtension(value);
			}
			var s_css = "." + s_name + "{" +
				"background-image: url(\"" + s_url + "\") !important;" +
				"background-size: 16px;" +
				"padding-left : 25px !important;" +
				"}";
			o_css = Ext.util.CSS.createStyleSheet(s_css, s_name);
		}
		else {
			if (Ext.ux.file.util.Handler.imageExtension.indexOf(s_extensions) == -1) {
				o_css.style.setProperty("background-image", "url(\"" + Ext.ux.file.util.Handler.getUrlByExtension(value) + "\")");
			}
			else {
				o_css.style.setProperty("background-image", "url(\"" + value + "?_dc=" + n_time + "\")");
			}
		}
		return s_name;
	}
});
