
using System.Diagnostics;

namespace Zerolauncher.Manager
{
    internal class AccountManager
    {
        static int teamId=0;

        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);
			teamId = AccountData.teamIndex;
			teamName = GroupsData.data[teamId];
			accountsList = AccountData.data[teamId];
		}

        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 group_id = TeamManager.Index2Key(newIndex);
			var acc = accountsList[memberId];
            accountsList.RemoveAt(memberId);
            AccountData.CheckGroup(group_id);
			AccountData.data[group_id].Add(acc);
            DataStreamNew.Save();
            MainWindow.Instance.ReloadBtn();
        }

        public static void DeleteAccount(int index) {
            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 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)
        {
            GroupsData.data.Add(GroupsData.data.Keys.LastOrDefault() + 1, teamName);
            DataStreamNew.Save();
        }

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

    }

}
