Ext.define("Portfolio.Defaults.uitype", {

	singleton: true,

	alternateClassName: [
		"ip2.uitypes",
		"ip2.default.uitypes"
	],

	constructor: function() {

		var i_self = this;

		Ext.apply(i_self, {
			integer: {
				grid: {
					xtype: "numbercolumn",
					format: "0"
				},
				form: {
					xtype: "numberfield",
					decimalPrecision: 0
				}
			},
			smallint: {
				grid: {
					align: "center",
					renderer: function (value, i_meta) {
						var i_renderer = new Ext.grid.column.Check();
						return i_renderer.defaultRenderer(value, i_meta);
					}
				},
				form: {
					
				},
			},
			varchar: {
				grid: {
				},
				form: {
					type: "varchar"
				}
			},
			decimal: {
				grid: {
					xtype: "numbercolumn"
				},
				form: {
					xtype: "numberfield"
				}
			},
			char: {
				grid: {
				},
				form: {
					xtype: "textfield"
				}
			},
			date: {
				grid: {
					renderer: function(value) {

						var f_render = Ext.util.Format.dateRenderer(Ext.util.Format.dateFormat);

						if (Ext.isDate(value)) {
							value = f_render(value);
						}
						else {
							if (value) {
								var f_render = Ext.util.Format.dateRenderer(Ext.util.Format.dateFormat);
								var i_date = new Date(value);
								value = f_render(i_date);
							}
						}
						return value;
					}
				},
				form: {
					type: "date"
				}
			},
			isotimestamp: {
				grid: {
					renderer: function(value) {

						if (Ext.isDate(value)) {
							return Ext.Date.format(value, Ext.util.Format.dateFormat + " H:i:s");
						}
						else {
							if (value) {
								return Ext.Date.format(Ext.Date.parse(value, ip2.date2dbDate.parseFormat), Ext.util.Format.dateFormat + " H:i:s");
							}
							else {
								return "N/A";
							}
						}
					}
				},
				form: {
					type: "isotimestamp"
				}
			},
			fieldfilemanager: {
				grid: {
					renderer : function(value){
					   return value;
					}
				},
				form: {
					type: "fieldfilemanager"
				}
			},
			textarea: {
				grid: {
				},
				form: {
					type: "textarea"
				}
			},
			html: {
				grid: {
					renderer: function(v) {
						return Ext.htmlEncode(v);
					}
				},
				form: {
					type: "htmleditor",
					syncEvent: "sync"
				}
			},
			json: {
				grid: {
					grid: {
						renderer: function(v) {
							return v ? JSON.stringify(v) : v;
						}
					},
				},
				form: {
					type: "acetextarea",
					mode: "json",
					theme: "textmate"
				}
			},
			boolean: {
				grid: {
					align: "center",
					renderer: function (value, i_meta) {
						var i_renderer = new Ext.grid.column.Check();
						return i_renderer.defaultRenderer(value, i_meta);
					}
				},
				form: {},
				convert : function(v, record){
					if ( Ext.isString(v) ) {
							v = ( v == "true" ) ? true : false;
							return v ? 1 : 0;
					}
					else{
						return v ? 1 : 0;
					}

				}
			},
			enum: {
				grid: {
					renderer: function (v, o_meta, i_record, n_row, n_col) {

						var i_self = this;
						var o_col = i_self.columns[n_col];
						var s_enum = o_col["enum"];

						var o_ns = ip2.resource.get("adm.ns");
						var o_enums = o_ns.enums;
						var value = v;

						if (s_enum) {
							value = o_enums[s_enum].values[v];
						}

						return ip2.i18n(value);
					}
				},
				form: {
					type: "varchar"
				}
			},
			i18n: {
				title: {
					da: "I18N Text field (language)",
					en: "I18N Text field (language)"
				},
				grid: {
					renderer: function (v) {

						var s_lang = ip2.session.getLanguage(),
							s_i18n = v;

						if ( Ext.isObject(v) ) {
							s_i18n = v[s_lang];
						}
						else if ( Ext.isString(v) ) {

							var o_i18n;

							try { o_i18n = JSON.parse(v); }
							catch (e) {}

							if ( Ext.isObject(o_i18n) ){
								s_i18n = o_i18n[s_lang];
							}
							else {
								s_i18n = v;
							}
						}

						return s_i18n;
					}
				},
				form: {
					type: "i18n",
					asString: true
				},
				convert: function(value) {
					var s_return = value;
					if (Ext.isObject(value)) {
						s_return = Ext.encode(value);
					}
					return s_return;
				}
			},
			i18nhook: {
				title: {
					da: "I18N Text field with Hook (language)",
					en: "I18N Text field with Hook (language)"
				},
				grid: {
					renderer: function (v) {

						var s_lang = ip2.session.getLanguage(),
							s_i18n = v;

						if ( Ext.isObject(v) ) {
							s_i18n = v[s_lang];
						}
						else if ( Ext.isString(v) ) {

							var o_i18n;

							try { o_i18n = JSON.parse(v); }
							catch (e) {}

							if ( o_i18n ) s_i18n = o_i18n[s_lang];
						}

						return s_i18n;
					}
				},
				form: {
					type: "i18nhook",
					asString: true
				}
			},
			password: {
				grid: {
					renderer: function () {
						return "********";
					}
				},
				form: {
					type: "varchar",
					inputType: "password"
				}
			},
			preposttextfield: {
				grid: {
					renderer: function () {
						return "********";
					}
				},
				form: {
					type: "preposttextfield"
				}
			},
			basketItems: {
				grid: {},
				form: {
					type: "ip2BasketItems"
				}
			},
			xpd: {
				grid: {
					renderer: function (s_value, meta, i_record) {

						var s_out = Ext.htmlEncode(s_value);
						return s_out;
					}
				},
				form: {
					type: "xpd"
				}
			},
			pageType: {
				grid: {
					renderer: function(value, meta, record, rowIndex, colIndex) {

						var o_xpd = ip2.resource.get("pages.xpd", true),
							o_collection = o_xpd.collections,
							s_title = "";

						if ( o_collection[value] ) {

							s_title = o_collection[value].title;
							s_title = ip2.xpd.translate(o_xpd, s_title);
						}

						return s_title;
					}
				},
				form: {
					type: "number"
				}
			},
			pubstatus: {
				grid: {
					renderer: function(value, meta, record, rowIndex, colIndex) {

						if(value) {
							var o_enum = ip2.resource.get("adm.ns");
							var o_value = o_enum.enums.pubstatus.values[value];
							value = ip2.i18n(o_value);
						}

						return value;
					}
				},
				form: {
					type: "number"
				}
			},
			productType: {
				grid: {
					renderer: function(value, meta, record, rowIndex, colIndex) {

						var o_xpd = ip2.resource.get("product.xpd", true);
						var o_collection = o_xpd.collections;
						var s_title = "";

						if ( o_collection[value] ) {
							s_title = o_collection[value].title;
							s_title = ip2.xpd.translate(o_xpd, s_title);
						}

						return s_title;
					}
				},
				form: {
					type: "number"
				}
			},
			nameType: {
				grid: {
					renderer: function(value, meta, record, rowIndex, colIndex) {

						var o_xpd = ip2.resource.get("names.xpd", true),
							o_collection = o_xpd.collections,
							s_title = "";

						if ( o_collection[value] ) {

							s_title = o_collection[value].title;
							s_title = ip2.xpd.translate(o_xpd, s_title);
						}

						return s_title;
					}
				},
				form: {
					type: "number"
				}
			},
			folderType: {
				grid: {
					renderer: function(value, meta, record, rowIndex, colIndex) {

						var o_xpd = ip2.resource.get("folder.xpd", true),
							o_collection = o_xpd.collections,
							s_title = "";

						if ( o_collection[value] ) {

							s_title = o_collection[value].title;
							s_title = ip2.xpd.translate(o_xpd, s_title);
						}

						return s_title;
					}
				},
				form: {
					type: "number"
				}
			},
			webactive: {
				grid: {
					renderer: function (v) {

						if ( Ext.isString(v) ) {
							v = ( v == "true" ) ? true : false;
						}

						if ( v ) {
							return ip2.i18n({
								da: "Ja",
								en: "Yes"
							});
						}
						else {
							return ip2.i18n({
								da: "Nej",
								en: "No"
							});
						}
					}
				},
				form: {
					type: "varchar"
				}
			},
			webactivelanguage: {
				grid: {
					renderer: ip2.util.renderer.countryFlags,
					helpers: {
						"language": function (s_language, b_value, s_value) {


							s_value = s_value || "[]";

							var a_value = Ext.decode(s_value, true) || [];
							var n_index = Ext.Array.indexOf(a_value, s_language);

							// Push to array if does not exist and is true
							if (b_value && n_index == -1) {
								a_value.push(s_language);
							}
							// Remove from array if exists in array and is false
							else if (!b_value && n_index > -1) {
								a_value.splice(n_index, 1);
							}

							s_value = Ext.encode(a_value);

							return s_value;
						}
					}
				},
				form: {
					type: "varchar"
				}
			},
			subscription: {
				grid: {
					renderer: function (v) {

						var o_xpd;

						var o_value = {
							da: "Nej",
							en: "No"
						};

						o_xpd = Ext.decode(v, true);

						if (o_xpd && o_xpd.newsletter) {
							Ext.apply(o_value, {
								da: "Ja",
								en: "Yes"
							});
						}

						return ip2.i18n(o_value);
					}
				},
				form: {
					type: "boolean"
				}
			},
			languages: {
				grid: {
					renderer: ip2.util.renderer.countryFlags
				},
				form: {
					type: "varchar"
				}
			},
			FileImg : {
				grid: {
					renderer : function(value, i_meta, i_record) {

						var b_isExternal = i_record.get("FILEXTERNAL");
						var s_url  = i_record.get("FILPATH") + i_record.get("FILNAME");
						var s_extension = i_record.get("FILEXT").toLowerCase();
						var a_extension_image = ["jpg","jpeg","png","gif", "bmp"];

						if(b_isExternal){

							if(a_extension_image.indexOf(s_extension) == -1){
								s_url = Ext.ux.file.util.Handler.getUrlByExtension(s_url);
							}
							else{
								s_url = i_record.get("FILORIPATH");
							}
						}
						else{

							if(a_extension_image.indexOf(s_extension) != -1){

								if ( i_record.get("FILWIDTH") ) {
									s_url = "/ip2-services/scaleImage.aspx?".concat(
										Ext.Object.toQueryString({
											width: 48,
											height: 48,
											path: s_url,
											padCanvas: false,
											_dc: ip2.getDcTimestamp()
										})
									);
								}
							}
							else{
								s_url = Ext.ux.file.util.Handler.getUrlByExtension(s_url);
							}
						}
						var s_style = "".concat(
							"width: 48px;",
							"height: 48px;",
							"margin-left: auto;",
							"margin-right: auto;",
							"background: url('" + s_url + "') no-repeat 50% 50%;",
							"background-size: contain;"
						);

						return "<div style=\"" + s_style + "\"></div>";
					}
				},
				form: {
					type: "varchar"
				}
			},
			decimalnumber: {
				form : {
					type: "decimalnumber"
				}
			},
			markdown : {
				grid: {
					renderer : function(value, meta, record){
						return value;
					}
				},
				form: {
					type: "markdowneditor"
				}
			},
			FileSize : {
				grid: {
					renderer : function(value, meta, record){
						return ip2.humanFileSize(value);
					}
				},
				form: {
					type: "varchar"
				}
			},
			FileRel : {
				grid: {
					renderer :function(value, meta, record){
						var i_template = Ext.create("Ext.XTemplate",
							"<div style=\"",
								"background-image: url(\"/portfolio/styles/icon/flatui/filemanager/{[this.getImage(values)]}\");",
								"background-repeat:no-repeat;background-position:50% 50%;\">",
								"<strong>{[values]}</strong>",
							"</div>",
							{
								getImage: function(value) {

									var s_image = "reddot.png";
									if (value) {
										s_image = "greendot.png";
									}
									return s_image;
								}
							}
						);

						i_template.compile();
						return i_template.apply(value);
					}
				}
			},
			Thumbnail : {
				grid: {
					align: "center",
					renderer :function(value, i_meta, i_record){

						var a_value = value.split(".");
						var s_extensions = a_value.pop().toLowerCase();
						var s_url = value;
						if (s_url) {
							if (Ext.ux.file.util.Handler.imageExtension.indexOf(s_extensions) < 0) {
								s_url = Ext.ux.file.util.Handler.getUrlByExtension(value);
							}
						}

						s_url = "/ip2-services/scaleImage.aspx?".concat(
							Ext.Object.toQueryString({
								width: 48,
								height: 48,
								path: s_url,
								padCanvas: false,
								_dc: ip2.getDcTimestamp()
							})
						);

						var i_template = Ext.create("Ext.XTemplate",
							"<div style=\"",
								"height: 48px;",
								"width: 48px;",
								"background-image: url({url});",
								"background-repeat:no-repeat;",
								"background-position:50% 50%;",
								"background-size: contain;",
								"margin: auto auto;",
								"\">",
							"</div>"
						);

						i_template.compile();
						return i_template.apply({
							url: s_url
						});
					}
				}
			},
			Thumbnailraw : {
				grid: {
					align: "center",
					renderer :function(value, i_meta, i_record){

						var a_value = value.split(".");
						var s_extensions = a_value.pop().toLowerCase();
						var s_url = value;
						if (s_url) {
							if (Ext.ux.file.util.Handler.imageExtension.indexOf(s_extensions) < 0) {
								s_url = Ext.ux.file.util.Handler.getUrlByExtension(value);
							}
						}

						s_url = s_url + "?".concat(
							Ext.Object.toQueryString({
								_dc: ip2.getDcTimestamp()
							})
						);

						var i_template = Ext.create("Ext.XTemplate",
							"<div style=\"",
								"height: 48px;",
								"width: 48px;",
								"background-image: url({url});",
								"background-repeat:no-repeat;",
								"background-position:50% 50%;",
								"background-size: contain;",
								"margin: auto auto;",
								"\">",
							"</div>"
						);

						i_template.compile();
						return i_template.apply({
							url: s_url
						});
					}
				}
			},
			booleanStatus : {
				grid: {
					renderer :function(value, meta, record){

						var i_template = Ext.create("Ext.XTemplate",
							"<tpl for=\".\">",
								"<div style=\"",
									"height: 100%;",
									"width: 100%;",
									"text-align: center;",
								"\">",
									"<tpl if=\"this.hasValue(values)\">",
										"<span style=\"",
											"height: 10px;",
											"width: 10px;",
											"background-color: red;",
											  "border-radius: 50%;",
											  "display: inline-block;",
											  "transform: translateY(12.5%);",
										"\">",
										"</span>",
									"</tpl>",
								"</div>",
							"</tpl>",
							{
								hasValue: function(value) {
									return value;
								}
							}
						);

						i_template.compile();
						return i_template.apply(value);
					}
				},
				convert : function(v, record){
					if ( Ext.isString(v) ) {
							v = ( v == "true" ) ? true : false;
							return v ? 1 : 0;
					}
					else{
						return v ? 1 : 0;
					}

				}
			},
			array: {
				grid: {
					renderer: function (value) {

						var a_value = Ext.Array.from(value);
						return a_value.join(", ");
					}
				}
			},
			collection: {
				grid : {
					renderer: function(value, i_cellValues, i_record) {

						var i_column = i_cellValues.column;
						var o_config = i_column.config;
						var s_xpd = o_config.xpd;
						var a_split = s_xpd.split(".");
						if (a_split.length == 1) {
							a_split.push("xpd");
							s_xpd = a_split.join(".");
						}

						var o_xpd = ip2.resource.get(s_xpd);
						var o_collections = o_xpd.collections;

						var o_found = {
							title: ""
						};

						var s_value = "";

						Ext.Object.each(o_collections, function(s_key) {
							if (s_key != "all") {
								var o_collection = o_collections[s_key];
								if (o_collection.id === value) {
									o_found = o_collection;
									return false;
								}
							}
						});


						if (o_found.title) {
							s_value = ip2.xpd.translate(o_xpd, o_found.title);
						}

						return s_value;
					}
				}
			},
			entityName: {
				grid: {
					renderer: function (s_value, meta, i_record) {

						var s_title = s_value;

						if (s_value) {
							var a_entities = [];
							var o_baseConfig = ip2.resource.baseConfig;
							var o_cached = o_baseConfig.dmd;

							Ext.Object.each(o_cached, function(s_key, o_value) {

								var o_model = o_value.model || {
									entities: []
								};

								a_entities = a_entities.concat(Ext.Array.from(o_model.entities));
							});

							var o_entity = Ext.Array.findBy(a_entities, function(o_value) {
								return o_value.name == s_value;
							});

							if (!o_entity) {

								var o_config = ip2.sajax.request({
									url: "ip2-services/entity2dmd.aspx?entity=" + s_value,
									asJson: true
								});

								var o_result = ip2.resource.get(o_config.name);

								var o_model = o_result.model;

								a_entities = Ext.Array.from(o_model.entities);

								o_entity = Ext.Array.findBy(a_entities, function(o_value) {
									return o_value.name == s_value;
								});
							}

							if (o_entity) {

								s_title = ip2.i18n(o_entity.title);
							}
						}

						return s_title;
					}
				}
			},
			fieldParams: {
				grid: {
					renderer: function (value) {

						var a_value = Ext.Array.from(value);
						return a_value.join(", ");
					}
				}
			},
			pd: {
				grid: {
					align: "center",
					renderer: function (value, i_meta, i_record, n_index) {

						var i_grid = this;
						var i_column = i_meta.column;
						if (!Ext.isFunction(i_grid.getDmdCfg)) {
							i_grid = this.up("grid");
							i_column = this;
						}
						var o_dmdInfo = i_grid.getDmdCfg();
						
						var s_root = i_column.root;

						if (s_root) {
							var s_property = i_column.property;

							if (s_property) {

								var o_property = i_record.get(s_root) || {};

								var a_split = s_property.split(".");
								var temp = o_property;

								while(a_split.length > 0) {

									var s_part = a_split.shift();

									if (Ext.isObject(temp)) {
										if (temp.hasOwnProperty(s_part)) {
											temp = temp[s_part];
										}
									}
									else {
										a_split = [];
									}
								}

								value = temp;
							}
						}
						
						if (value) {

							var s_name = [
								i_column.entityName || o_dmdInfo.entityName,
								i_column.uiView || "portfolio",
								i_column.uiRoot || s_root
							].join(".");

							var a_collections = Ext.Array.from(Portfolio.pd.util.Cache.get(s_name));

							var o_found = Ext.Array.findBy(a_collections, function(o_collection) {
								return o_collection.name == value && o_collection.type == "formCollection";
							});
	
							if (o_found) {
								value = ip2.i18n(o_found.title || o_found.label);
							}
						}
						
						return value;
					}
				},
				form: {
					
				}
			}
		});
	}
});
