using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Zerolauncher.dialog;
using Zerolauncher.Manager;

namespace Zerolauncher.controls
{
    /// <summary>
    /// MemberControl.xaml 的交互逻辑
    /// </summary>
    public partial class MemberControl : UserControl
    {
        public int memberId { get; set; }

        private bool isMouseDown;


        public MemberControl()
        {
            InitializeComponent();
            isMouseDown = false;
            // 鼠标按下事件
            MouseDown += (s, e) =>
            {
                RenderTransform = new TranslateTransform(3, 3);
                isMouseDown = true;
            };

            // 鼠标松开事件
            MouseUp += (s, e) =>
            {
                if (!isMouseDown) return;
                RenderTransform = new TranslateTransform(0, 0);
                if (e.ChangedButton == MouseButton.Right) return;
                if (!EngineManager.CreateGame(memberId)) MessageBox.Show("账号已启动！请勿重复启动", "提示");
            };

            // 鼠标离开事件
            MouseLeave += (s, e) =>
            {
                if (Mouse.LeftButton == MouseButtonState.Pressed)
                {
                    RenderTransform = new TranslateTransform(0, 0);
                    isMouseDown = false;
                }
            };

            // 鼠标捕获丢失事件
            LostMouseCapture += (s, e) =>
            {
                RenderTransform = new TranslateTransform(0, 0);
                isMouseDown = false;
            };
            ContextMenu = CreateContextMenu();
        }

        private ContextMenu CreateContextMenu()
        {
            ContextMenu contextMenu = new ContextMenu();

            MenuItem editItem = new MenuItem { Header = "编辑" };
            editItem.Click += editItem_Click;
            contextMenu.Items.Add(editItem);

            MenuItem delItem = new MenuItem { Header = "删除" };
            delItem.Click += delItem_Click;
            contextMenu.Items.Add(delItem);

            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.MoveAccount(memberId, i);
                    };
                    changeTeamItem.Items.Add(menuItem);
                }
            };
            return contextMenu;
        }

        private void delItem_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show($"即将删除账号{text.Content}", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel);
            if (result == MessageBoxResult.OK)
            {
                AccountManager.DeleteAccount(memberId);
            }
        }

        private void editItem_Click(object sender, RoutedEventArgs e)
        {
            //Trace.WriteLine("edit member " + memberId.ToString());
            EditMemberDialog.CreateDailog(memberId);
        }
    }
}
