using AntdUI;
using AntdUIDemo.Models;
using AntdUIDemo.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace DHSoftware.Views
{
    public partial class UserConfigFrm : UserControl
    {
        private UserControl currControl;
        private bool isUpdatingTabs = false;//用于阻止Tabs更新
        public UserConfigFrm()
        {
            InitializeComponent();
           
            LoadMenu();
            menu.Width = (int)(100 * Config.Dpi);

        }
        private void LoadMenu(string filter = "")
        {
            menu.Items.Clear();

            string lang = AntdUI.Localization.CurrentLanguage;
            var menuItems = DataUtil.Menu_decetion;
            //var menuIcons = DataUtil.MenuIcons_zhcn;
            //if (lang.StartsWith("en"))
            //{
            //    menuItems = DataUtil.MenuItems_enus;
            //    menuIcons = DataUtil.MenuIcons_enus;
            //}

            foreach (var rootItem in menuItems)
            {
                var rootKey = rootItem.Key.ToLower();
                var rootMenu = new AntdUI.MenuItem
                {
                    Text = rootItem.Key,
                    //IconSvg = menuIcons.TryGetValue(rootItem.Key, out var icon) ? icon : "UnorderedListOutlined",
                };
                bool rootVisible = false; // 用于标记是否显示根节点

                foreach (var item in rootItem.Value)
                {
                    var childText = item.Text.ToLower();

                    // 如果子节点包含搜索文本
                    if (childText.Contains(filter))
                    {
                        var menuItem = new AntdUI.MenuItem
                        {
                            Text = item.Text,
                            IconSvg = item.IconSvg,
                            Tag = item.Tag,
                        };
                        rootMenu.Sub.Add(menuItem);
                        rootVisible = true; // 如果有子节点包含,则显示根节点
                    }
                }

                // 如果根节点包含搜索文本,或有可见的子节点,则显示根节点
                if (rootKey.Contains(filter) || rootVisible)
                {
                    menu.Items.Add(rootMenu);
                }
            }
        }
        private void SelectMenu()
        {
            if (isUpdatingTabs) return;
            var text = tabs.SelectedTab?.Text; // 使用安全导航操作符,防止 SelectedTab 为 null
            if (string.IsNullOrEmpty(text)) // 检查 text 是否为 null 或空
            {
                return; // 如果 text 为空,直接退出方法
            }
            //首页
            if (text == AntdUI.Localization.Get("home", "主页"))
            {
                return;
            }
            var rootIndex = 0;
            var subIndex = 0;
            var menuItemsCopy = menu.Items.ToList(); // 创建副本
            for (int i = 0; i < menuItemsCopy.Count; i++)
            {
                for (int j = 0; j < menuItemsCopy[i].Sub.Count; j++)
                {
                    if (menuItemsCopy[i].Sub[j].Tag.ToString() == text)
                    {
                        rootIndex = i;
                        subIndex = j;
                        break;
                    }
                }
            }
            menu.SelectIndex(rootIndex, subIndex, true);
        }
        private void Menu_SelectChanged(object sender, MenuSelectEventArgs e)
        {
            string name = (string)e.Value.Tag;

            // 清理上一个浮动按钮窗体
            if (currControl is FloatButtonDemo floatButtonDemo)
            {
                floatButtonDemo.CloseFloatButtonForm();
            }

            // 检查是否已存在同名 TabPage
            foreach (var tab in tabs.Pages)
            {
                if (tab is AntdUI.TabPage existingTab && existingTab.Text == name)
                {
                    isUpdatingTabs = true;
                    tabs.SelectedTab = existingTab;
                    isUpdatingTabs = false;
                    currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
                    return;
                }
            }
            int width = tabs.Width;
            int height = tabs.Height;
            // 创建新 TabPage
            UserControl control =new UserDetetion(width, height);
            switch (name)
            {
                case "工位1":
                  //  control  = 
                    break;
            }

            if (control != null)
            {
                control.Dock = DockStyle.Fill;
                // AutoDpi(control); // 如果有 DPI 适配逻辑

                var tabPage = new AntdUI.TabPage
                {
                   Dock = DockStyle.Fill,
                    Text = name,
                };
                tabPage.Controls.Add(control);
                tabs.Pages.Add(tabPage);

                isUpdatingTabs = true;
                tabs.SelectedTab = tabPage;
                isUpdatingTabs = false;
                currControl = control;
            }
        }


        private void tabs_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            SelectMenu();
        }
    }
}