
using System.Diagnostics;
using System.Windows.Input;

namespace Zerolauncher.Manager
{
    internal static class AccountManager
    {
        static int _teamId;

        public static string TeamName = "";

        public static List<AccountNew>? AccountsList;

        public static void InitLoadData()
        {
            _teamId = AccountData.TeamIndex;
            foreach (var pair in GroupsData.Data)
            {
                Trace.WriteLine(pair.Key, pair.Value);
            }
			TeamName = GroupsData.Data[_teamId];
            AccountsList = AccountData.Data[_teamId];
        }

        public static void ChangeTeam(int index)
        {
            _teamId = TeamManager.Index2Key(index);
			AccountData.TeamIndex = _teamId;
			TeamName = GroupsData.Data[_teamId];
            if (!AccountData.Data.ContainsKey(_teamId))
                AccountData.Data.Add(_teamId, []);
			AccountsList = AccountData.Data[_teamId];
			DataStreamNew.Save();
		}

        public static void SaveEdit()
        {
            DataStreamNew.Save();
            MainWindow.Instance.ReloadBtn();
        }

        public static bool AddAccount(AccountNew account)
        {
            account.GroupId = _teamId;
			AccountData.AddAccount(account);

			DataStreamNew.Save();
            MainWindow.Instance.ReloadBtn();
            return true;
        }

        public static bool AddAccounts(AccountNew account)
        {
			account.GroupId = _teamId;
			AccountData.AddAccount(account);
            return true;
        }

        public static void MoveAccount(int memberId, int newIndex)
        {
            var groupId = TeamManager.Index2Key(newIndex);
            if (AccountsList != null)
            {
                var acc = AccountsList[memberId];
                AccountsList.RemoveAt(memberId);
                if (!AccountData.CheckGroup(groupId)) return;
                AccountData.Data[groupId].Add(acc);
            }

            DataStreamNew.Save();
            MainWindow.Instance.ReloadBtn();
        }

        public static void DeleteAccount(int index) {
            if (AccountsList != null)
            {
                var nick = AccountsList[index].nickName;
                AccountsList.RemoveAt(index);
                AccountData.Dict.Remove(nick);
            }

            DataStreamNew.Save();
            MainWindow.Instance.ReloadBtn();
        }

        public static void EditTeamName(string teamName0)
        {
            TeamName = teamName0;
            GroupsData.Data[_teamId] = TeamName;
            DataStreamNew.Save();
        }

        public static bool DeleteTeam()
        {
            if (GroupsData.Data.Keys.Count == 1)
            {
                return false;
            }
            AccountData.RemoveGroup();
            ChangeTeam(GroupsData.Data.Keys.First());
			DataStreamNew.Save();
            MainWindow.Instance.ReloadBtn();
            return true;
        }

    }

    internal static class TeamManager
    {

        public static int Index2Key(int index) { return GroupsData.Data.ElementAt(index).Key; }
        public static string[] GetAllTeamName()
        {
            return GroupsData.Data.Values.ToArray();
        }

        public static void AddTeam(string teamName)
        {
            var key = GroupsData.Data.Keys.LastOrDefault() + 1;
			GroupsData.Data.Add(key, teamName);
            DataStreamNew.Save();
        }

        //运行时才能决定是否执行内联
        [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        public static AccountNew? Nick2Acc(string nickName)
        {
            return AccountData.Dict.GetValueOrDefault(nickName);
        }

    }

}
