using KdbndpTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace ReZero.SuperAPI
{
public class MenuBuilder
{
///
/// Generate the menu based on the provided tree list and current category.
///
/// The list of interface categories.
/// The current interface category.
/// The generated menu HTML.
public static string GenerateMenu(List treeList, ZeroInterfaceCategory current)
{
StringBuilder htmlBuilder = new StringBuilder();
int i = 0;
foreach (var tree in treeList.OrderBy(it=>it.SortId))
{
var isOpen = IsOpen(current, tree);
var active = isOpen ? " active " : null;
var isHidden = SuperAPIModule._apiOptions?.InterfaceOptions?.Jwt?.Enable==true&&tree.Id>200;
if (tree.SubInterfaceCategories != null && tree.SubInterfaceCategories.Count > 0)
{
if (active != null)
active = $" {active} open ";
if (isHidden)
active = $"hide manager {active}";
htmlBuilder.AppendLine("");
htmlBuilder.AppendLine($" {tree.Name}");
htmlBuilder.AppendLine(" ");
GenerateSubMenu(tree.SubInterfaceCategories, htmlBuilder, current);
htmlBuilder.AppendLine("
");
htmlBuilder.AppendLine("");
}
else
{
if (isHidden)
active = $"hide manager {active}";
htmlBuilder.AppendLine("");
htmlBuilder.AppendLine($" {tree.Name}");
htmlBuilder.AppendLine("");
}
++i;
}
return htmlBuilder.ToString();
}
private static void GenerateSubMenu(List subTreeList, StringBuilder htmlBuilder, ZeroInterfaceCategory current)
{
foreach (var subTree in subTreeList.OrderBy(it=>it.SortId))
{
var isOpen = IsOpen(current, subTree);
var active = isOpen ? " active " : "";
if (subTree.SubInterfaceCategories != null && subTree.SubInterfaceCategories.Count > 0)
{
htmlBuilder.AppendLine(" ");
htmlBuilder.AppendLine($" {subTree.Name}");
htmlBuilder.AppendLine(" ");
GenerateSubMenu(subTree.SubInterfaceCategories, htmlBuilder, current);
htmlBuilder.AppendLine("
");
htmlBuilder.AppendLine(" ");
}
else
{
isOpen = current.Id.ToString().Equals(subTree.Id.ToString());
active = isOpen ? " active " : "";
htmlBuilder.AppendLine(" ");
htmlBuilder.AppendLine($" {subTree.Name}");
htmlBuilder.AppendLine(" ");
}
}
}
private static bool IsOpen(ZeroInterfaceCategory current, ZeroInterfaceCategory subTree)
{
return current.ParentId.ToString().Equals(subTree.Id.ToString()) || current.Id == subTree.Id;
}
}
}