using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Zerolauncher.AboutDialog;
using Zerolauncher.controls;
using Zerolauncher.dialog;
using Zerolauncher.Manager;

namespace Zerolauncher
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static MainWindow Instance { get; private set; }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //实现窗口的拖动
            DragMove();
        }

        public MainWindow()
        {
            InitializeComponent();
            Instance = this;

            memberBG.ContextMenu = CreateContextMenu();
            int i = 0;
            foreach (var account in AccountManager.accountsList)
            {
                var member = new MemberControl();
                member.memberId = i++;
                member.text.Content = ServicesStaticInfo.ServicesShortName[account.providerId] + "-" + account.nickName;
                mLayout.Children.Add(member);
            }
        }

        public void ReloadBtn()
        {
            //foreach (var btn in mLayout.Children.)
            mLayout.Children.Clear();
            int i = 0;
            foreach (var account in AccountManager.accountsList)
            {
                var member = new MemberControl();
                member.memberId = i++;
                member.text.Content = ServicesStaticInfo.ServicesShortName[account.providerId] + "-" + account.nickName;
                mLayout.Children.Add(member);
            }
        }

        private ContextMenu CreateContextMenu()
        {
            ContextMenu contextMenu = new ContextMenu();
            MenuItem addMemberItem = new MenuItem { Header = "添加账号" };
            addMemberItem.Click += addMemberItem_Click;
            contextMenu.Items.Add(addMemberItem);

            MenuItem addMembersItem = new MenuItem { Header = "导入账号" };
            contextMenu.Items.Add(addMembersItem);
            addMembersItem.Click += addMembersItem_Click;

            MenuItem addTeamItem = new MenuItem { Header = "新增队伍" };
            contextMenu.Items.Add(addTeamItem);
            addTeamItem.Click += addTeamItem_Click;

            MenuItem editTeamItem = new MenuItem { Header = "编辑队伍" };
            contextMenu.Items.Add(editTeamItem);
            editTeamItem.Click += editTeamItem_Click;

            MenuItem changeTeamItem = new MenuItem { Header = "切换队伍" };
            contextMenu.Items.Add(changeTeamItem);
            changeTeamItem.MouseEnter += (sender, args) =>
            {
                // 清空ContextMenu的items
                changeTeamItem.Items.Clear();

                // 假设你有一个字符串数组
                string[] items = TeamManager.GetAllTeamName();

                // 遍历字符串数组，为每个元素创建一个MenuItem
                for (int i = 0; i < items.Length; i++)
                {
                    MenuItem menuItem = new MenuItem { Header = items[i] };
                    menuItem.Tag = i;
                    menuItem.Click += (sender, args) =>
                    {
                        // 输出被点击的item的index
                        MenuItem menuItem = sender as MenuItem;
                        int i = (int)menuItem.Tag;
                        Trace.WriteLine("Clicked item index: " + i);
                        AccountManager.ChangeTeam(i);
                        ReloadBtn();
                    };
                    changeTeamItem.Items.Add(menuItem);
                }
            };

            return contextMenu;
        }

        private void addMemberItem_Click(object sender, RoutedEventArgs e)
        {
            EditMemberDialog.CreateDailog(null);
        }

        private void addMembersItem_Click(object sender, RoutedEventArgs e)
        {
            AddMemebersDialog.CreateDailog();
        }

        private void addTeamItem_Click(object sender, RoutedEventArgs e)
        {
            EditTeamDialog.CreateDailog();
        }

        private void editTeamItem_Click(object sender, RoutedEventArgs e)
        {
            EditTeamDialog.CreateDailog(AccountManager.teamName);
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void AirButton_Click(object sender, RoutedEventArgs e)
        {
            var ab = new About1();
            ab.ShowDialog();
        }

        private void AirButton_Click_1(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }
    }
}