using Mediachase.Commerce.Security;
using Mediachase.Commerce.Storage;
using Mediachase.MetaDataPlus;
using Mediachase.MetaDataPlus.Configurator;
using Mediachase.Web.Console.BaseClasses;
using Mediachase.Web.Console.Interfaces;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Nansen.Commerce.Additions.Apps.Core.ExtensionMetaData
{
	public partial class EditTab : CoreBaseUserControl, IAdminTabControl, IDynamicParamControl, IAdminContextControl
	{
		public readonly string _MetaObjectContextKey = "MetaObjectContext-";
		public readonly string _OrderMetaObjectContextKey = "OrderMetaObjectContext-";
		private string[] _Languages = new string[1]
		{
		  "en-US"
		};
		private string[] _EditLanguages = new string[1]
		{
				  "en-US"
		};
		private string _ValidationGroup = string.Empty;
		private int _ObjectId;
		private int _MetaClassId;
		private Dictionary<string, MetaObject> _metaObjects;

		public int ObjectId
		{
			get
			{
				return this._ObjectId;
			}
			set
			{
				this._ObjectId = value;
			}
		}

		public int MetaClassId
		{
			get
			{
				return this._MetaClassId;
			}
			set
			{
				this._MetaClassId = value;
			}
		}

		public string[] Languages
		{
			get
			{
				return this._Languages;
			}
			set
			{
				this._Languages = value;
			}
		}

		public string[] EditLanguages
		{
			get
			{
				return this._EditLanguages;
			}
			set
			{
				this._EditLanguages = value;
			}
		}

		public string ValidationGroup
		{
			get
			{
				return this._ValidationGroup;
			}
			set
			{
				this._ValidationGroup = value;
			}
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
		}

		protected void Page_Load(object sender, EventArgs e)
		{
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
		}

		protected override void CreateChildControls()
		{
			if (!this.ChildControlsCreated)
			{
				base.CreateChildControls();
				this.ChildControlsCreated = true;
			}
		}

		public override void DataBind()
		{
			base.DataBind();
			this.CreateChildControlsInternal();
		}

		public void PopulateMetaControls()
		{
		}

		private void CreateChildControlsInternal()
		{
			this.MetaControls.EnableViewState = false;
			if (this.MetaControls.Controls.Count <= 0)
			{
				this.MetaControls.Controls.Clear();
				MetaClass metaClass = MetaClass.Load(this.MDContext, this.MetaClassId);
				if (metaClass != null)
				{
					Dictionary<string, MetaObject> dictionary = new Dictionary<string, MetaObject>();
					if (this._metaObjects != null)
					{
						dictionary = this._metaObjects;
					}
					else
					{
						this.MDContext.UseCurrentUICulture = false;
						foreach (string index in this.EditLanguages)
						{
							this.MDContext.UseCurrentUICulture = false;
							this.MDContext.Language = index;
							MetaObject metaObject = (MetaObject)null;
							if (this.ObjectId > 0)
							{
								metaObject = MetaObject.Load(this.MDContext, this.ObjectId, metaClass);
								if (metaObject == null)
								{
									metaObject = MetaObject.NewObject(this.MDContext, this.ObjectId, this.MetaClassId, SecurityContext.Current.CurrentUserName);
									metaObject.AcceptChanges(this.MDContext);
								}
							}
							dictionary[index] = metaObject;
						}
						this.MDContext.UseCurrentUICulture = true;
					}
					foreach (MetaField metaField in (ReadOnlyCollectionBase)MetaField.GetList(this.MDContext, this.MetaClassId))
					{
						if (metaField.IsUser)
						{
							int num = 0;
							foreach (string index in this.EditLanguages)
							{
								string virtualPath = this.ResolveMetaControl(metaClass, metaField);
								Control child = this.MetaControls.FindControl(metaField.Name);
								if (child == null)
								{
									child = this.Page.LoadControl(virtualPath);
									this.MetaControls.Controls.Add(child);
								}
								CoreBaseUserControl coreBaseUserControl = child as CoreBaseUserControl;
								if (coreBaseUserControl != null)
									coreBaseUserControl.MDContext = this.MDContext;
								((IMetaControl)child).MetaField = metaField;
								if (dictionary[index] != null && dictionary[index][metaField] != null)
									((IMetaControl)child).MetaObject = dictionary[index];
								((IMetaControl)child).LanguageCode = index;
								((IMetaControl)child).ValidationGroup = this.ValidationGroup;
								child.DataBind();
								if (metaField.MultiLanguageValue)
									++num;
								else
									break;
							}
						}
					}
				}
			}
		}

		private string ResolveMetaControl(MetaClass metaClass, MetaField metaField)
		{
			string str = "~/Apps/Core/MetaData/Controls/";
			string controlNameForMetaType = this.GetControlNameForMetaType(metaField.DataType);
			string path1 = string.Format("{0}{1}.{2}.{3}.ascx", (object)str, (object)metaClass.Name, (object)metaField.Name, (object)controlNameForMetaType);
			if (File.Exists(this.Server.MapPath(path1)))
			{
				return path1;
			}
			else
			{
				string path2 = string.Format("{0}{1}.{2}.ascx", (object)str, (object)metaField.Name, (object)controlNameForMetaType);
				if (File.Exists(this.Server.MapPath(path2)))
				{
					return path2;
				}
				else
				{
					string path3 = string.Format("{0}{1}.{2}.ascx", (object)str, (object)metaClass.Name, (object)controlNameForMetaType);
					if (File.Exists(this.Server.MapPath(path3)))
						return path3;
					else
						return string.Format("{0}{1}.ascx", (object)str, (object)controlNameForMetaType);
				}
			}
		}

		public string GetControlNameForMetaType(MetaDataType type)
		{
			switch (type)
			{
				case MetaDataType.DateTime:
					return "DateTimeMetaControl";
				case MetaDataType.Decimal:
					return "DecimalControl";
				case MetaDataType.Float:
					return "FloatControl";
				case MetaDataType.Int:
				case MetaDataType.Integer:
					return "IntegerControl";
				case MetaDataType.Money:
					return "MoneyControl";
				case MetaDataType.Boolean:
					return "BooleanControl";
				case MetaDataType.Date:
					return "DateTimeMetaControl";
				case MetaDataType.Email:
					return "EmailControl";
				case MetaDataType.URL:
					return "URLControl";
				case MetaDataType.ShortString:
					return "ShortStringControl";
				case MetaDataType.LongString:
					return "LongStringControl";
				case MetaDataType.LongHtmlString:
					return "LongHTMLStringControl";
				case MetaDataType.DictionarySingleValue:
				case MetaDataType.EnumSingleValue:
					return "DicSingleValueControl";
				case MetaDataType.DictionaryMultiValue:
				case MetaDataType.EnumMultiValue:
					return "MultiValueControl";
				case MetaDataType.StringDictionary:
					return "StringDictionaryControl";
				case MetaDataType.File:
					return "FileControl";
				case MetaDataType.ImageFile:
					return "ImageFileControl";
				default:
					return "";
			}
		}

		public void SaveChanges(IDictionary context)
		{
			if (this.ObjectId != 0)
			{
				string currentUserName = SecurityContext.Current.CurrentUserName;
				this.MDContext.UseCurrentUICulture = false;
				MetaObjectSerialized objectSerialized = new MetaObjectSerialized();
				foreach (string str in this.Languages)
				{
					this.MDContext.Language = str;
					MetaObject metaObject = (MetaObject)null;
					bool flag = true;
					if (context != null && context.Contains((object)(this._MetaObjectContextKey + str)))
					{
						flag = false;
						metaObject = (MetaObject)context[(object)(this._MetaObjectContextKey + str)];
					}
					if (metaObject == null)
						metaObject = MetaObject.Load(this.MDContext, this.ObjectId, this.MetaClassId);
					if (metaObject == null)
					{
						metaObject = MetaObject.NewObject(this.MDContext, this.ObjectId, this.MetaClassId, currentUserName);
					}
					else
					{
						metaObject.ModifierId = currentUserName;
						metaObject.Modified = DateTime.UtcNow;
					}
					foreach (Control control in this.MetaControls.Controls)
					{
						if (string.Compare(((IMetaControl)control).LanguageCode, str, true) == 0)
						{
							((IMetaControl)control).MetaObject = metaObject;
							((IMetaControl)control).Update();
						}
					}
					if (flag)
						metaObject.AcceptChanges(this.MDContext);
					if (context != null)
						objectSerialized.AddMetaObject(str, metaObject);
				}
				if (context != null)
					context.Add((object)"MetaObjectSerialized", (object)objectSerialized);
				this.MDContext.UseCurrentUICulture = true;
			}
		}

		public void LoadContext(IDictionary context)
		{
			if (context.Contains((object)"MetaObjectsContext"))
				this._metaObjects = (Dictionary<string, MetaObject>)context[(object)"MetaObjectsContext"];
		}
	}
}
