@*   Xafari Framework version : 17.2.3011.1336   *@

@using System.Collections
@using DevExpress.ExpressApp.Actions
@using Xafari.Mvc.MVCx.Templates.ActionContainer
@using Xafari.Mvc.Templates.ActionContainers
@using Xafari.Mvc.Templates.ActionContainers.Args
@using ActionDescription = Xafari.Mvc.Templates.ActionContainers.ActionDescription
@using GroupDescription = Xafari.Mvc.Templates.ActionContainers.GroupDescription
@using MenuItem = DevExpress.Web.MenuItem

@model MVCxActionContainerHolder

@{
	// Code of methods OnInit() in ActionContainerHolder(XAF)
	if ((this.Model.ActionContainers.Count == 0) && !string.IsNullOrEmpty(this.Model.Categories))
	{
		this.Model.SetActionContainers(this.Model.Categories);
	}
	this.Model.FrameTemplate.RegisterActionContainers(this.Model.ActionContainers);
	this.Model.ActionContainers.SetReadOnly(true);

	// Code of methods BuildMenu() in ActionContainerHolder(XAF)
	this.Model.CreateMenuModel();
}

@{
	this.ViewData.Add("MenuItemNameToClickHandler", new Dictionary<string, string>());

	var settings = this.Model.Menu ?? new MenuSettings();

	// Begin Init
	settings.EnableClientSideAPI = true;
	if (this.Model.ContainerStyle != DevExpress.ExpressApp.Web.Templates.ActionContainers.ActionContainerStyle.ToolBar)
	{
		settings.SkinID = string.Format("Menu{0}", this.Model.ContainerStyle);
	}
	settings.ShowAsToolbar = true;
	settings.Name = string.Format("{0}_{1}", this.Model.Name, this.Model.GetHashCode());
	settings.AccessibilityCompliant = true;
	settings.Init += (sender, args) =>
	{
		var mvcxMenu = sender as MVCxMenu;
		if (mvcxMenu == null) return;

		mvcxMenu.EnableViewState = false;
		mvcxMenu.ItemWrap = false;
		if (!string.IsNullOrEmpty(this.Model.Name))
			mvcxMenu.ClientInstanceName = this.Model.Name.Split('_')[0];
	};
	// End Init

	settings.CustomJSProperties += (sender, args) => { args.Properties.Remove("cpDropDownContainers"); };

	settings.Items.Clear();
	foreach (GroupDescription groupDescription in this.Model.menuModel)
	{
		if (groupDescription.Visible)
		{
			if (groupDescription.IsDropDown)
			{
				//CreateDropDownMenuItemHierarchy

				//CreateDropDownMenuItem
				MVCxMenuItem menuItem;
				ActionItem actionItem = null;
				if (!string.IsNullOrEmpty(groupDescription.Container.DefaultItemCaption))
				{
					menuItem = new MVCxMenuItem();
					menuItem.Name = groupDescription.Container.ContainerId;
					menuItem.Text = groupDescription.Container.DefaultItemCaption;
					if (!string.IsNullOrEmpty(groupDescription.Container.DefaultItemImageName))
					{
						menuItem.Image.SpriteProperties.SetImageProperties(groupDescription.Container.DefaultItemImageName);
					}
					menuItem.DropDownMode = false;

				}
				else
				{
					ActionBase defaultAction = groupDescription.DefaultAction;
					actionItem = this.Model.CreateActionItem(defaultAction);
					menuItem = new MVCxMenuItem();

					@MVCx.ActionContainerHolder.Helpers.CreateMenuItem(this, settings, actionItem, menuItem)

					if (defaultAction is SingleChoiceAction)
					{
						menuItem.Items.Clear();
					}

					menuItem.DropDownMode = true;
				}

				settings.Items.Insert(groupDescription.MenuItemIndex, menuItem);
				if (actionItem != null)
				{
					ActionItemCreatedEventArgs args = new ActionItemCreatedEventArgs(actionItem);
					this.Model.OnMenuActionItemCreated(args);
				}

				//CreateDropDownMenuSubItems
				foreach (ActionDescription description in (IEnumerable)groupDescription)
				{
					if (description.Visible)
					{
						var actionItem1 = this.Model.CreateMenuActionItemBase(description.Action, groupDescription.Container.ContainerId);
						if (actionItem1 != null)
						{
							this.Model.actionItems[actionItem1.Action] = actionItem1;
							var mvcxMenuItem = new MVCxMenuItem();

							@MVCx.ActionContainerHolder.Helpers.CreateMenuItem(this, settings, actionItem1, mvcxMenuItem)

							menuItem.Items.Insert(description.InnerPosition, mvcxMenuItem);
							ActionItemCreatedEventArgs args = new ActionItemCreatedEventArgs(actionItem1);
							this.Model.OnMenuActionItemCreated(args);
						}

					}
				}
				this.Model.SetupCssClasses(menuItem, groupDescription.Container);
				this.Model.SetupDropDownMenuClientSettings(settings, menuItem, groupDescription.Container.ContainerId);
			}
			else
			{
				foreach (ActionDescription actionDescription in groupDescription)
				{
					if (actionDescription.Visible)
					{
						var actionItem = this.Model.CreateMenuActionItemBase(actionDescription.Action, groupDescription.Container.ContainerId);
						if (actionItem != null)
						{
							this.Model.actionItems[actionItem.Action] = actionItem;
							var mvcxMenuItem = new MVCxMenuItem();
							mvcxMenuItem.BeginGroup = actionDescription.BeginGroup;

							@MVCx.ActionContainerHolder.Helpers.CreateMenuItem(this, settings, actionItem, mvcxMenuItem)

							if (mvcxMenuItem.DropDownMode)
							{
								this.Model.SetupDropDownMenuClientSettings(settings, mvcxMenuItem, actionDescription.Action.Id);
							}
							settings.Items.Insert(actionDescription.MenuItemIndex, mvcxMenuItem);
							ActionItemCreatedEventArgs args = new ActionItemCreatedEventArgs(actionItem);
							this.Model.OnMenuActionItemCreated(args);

						}
					}
				}
			}
		}
		this.Model.OnMenuItemsCreated();
	}

	settings.CustomJSProperties += (sender, args) =>
	{
		var mvcxMenu = sender as MVCxMenu;
		if (mvcxMenu == null) return;

		var menuItemNameToClickHandler = this.ViewData["MenuItemNameToClickHandler"] as Dictionary<string, string>;
		if (menuItemNameToClickHandler == null) return;

		args.Properties["cpClickHandlers"] = MVCxActionContainerHolder.GetAllMenuItems(mvcxMenu.RootItem.Items).Where(menuItem => menuItemNameToClickHandler.ContainsKey(menuItem.Name)).ToDictionary(menuItem => menuItem.IndexPath, menuItem => menuItemNameToClickHandler[menuItem.Name]);

	};
	settings.ClientSideEvents.ItemClick = "function(s,e) {\r\n    var indexPath = ''+ e.item.indexPath + '';\r\n    var clickHandlerScript = s.cpClickHandlers[indexPath];\r\n    var processCallback = false;\r\n    if(clickHandlerScript) {\r\n        if(typeof xaf == 'undefined' || !xaf.ConfirmUnsavedChangedController || xaf.ConfirmUnsavedChangedController.CanProcessCallbackForAction(s,e)){\r\n            processCallback = true;\r\n            window.xafImmediatePostDataCallbackScript = null;\r\n            xafWaitForCallback(clickHandlerScript);\r\n        }\r\n    }\r\n    if(processCallback !== true){\r\n        CallImmediatePostDataCallbackIfNeed();\r\n    }\r\n}";
}


@{
	//SendStateToClient
	settings.PreRender += (sender, args) =>
	{
		var mvcxMenu = sender as MVCxMenu;
		if (mvcxMenu == null) return;

		this.Model.OnCustomUpdateMenu(mvcxMenu);
		this.Model.SendStateToClient("State_" + this.Model.Name, DevExpress.ExpressApp.Web.Templates.ActionContainers.MenuPartialRenderHelper.GetMenuState(mvcxMenu));
	};
	//

	if (settings.Items.Count > 0)
	{
		var str = new MvcHtmlString((!string.IsNullOrWhiteSpace(this.Model.CssClass) ? " class=\"" + this.Model.CssClass + "\"" : "") +
									(!string.IsNullOrWhiteSpace(this.Model.Style) ? " style=\"" + this.Model.Style + "\"" : ""));
		<div @str>
			@Html.DevExpress().Menu(settings).GetHtml()
		</div>
	}
}

@{
	// Code of methods OnUnload() in ActionContainerHolder(XAF)
	this.Model.FrameTemplate.UnregisterActionContainers(this.Model.ActionContainers);
}