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

        public static string teamName = "";

        public static List<Account> accountsList = new List<Account>();

        public static void initLoadData()
        {
            teamIndex = DataStream.dataStream.teamIndex;
            var group = DataStream.dataStream.Groups[teamIndex];
            teamName = group.Name;
            foreach (var acc in group.Accounts)
            {
                accountsList.Add(acc);
            }
        }

        public static void reloadData(int index = -1)
        {
            if (index < 0) index = teamIndex;
            var group = DataStream.dataStream.Groups[index];
            if (teamIndex!=index)
            {
                teamIndex = index;
                teamName = group.Name;
                DataStream.dataStream.teamIndex = index;
                DataStream.write();
            }
            accountsList.Clear();
            foreach (var acc in group.Accounts)
            {
                accountsList.Add(acc);
            }
        }

        public static void saveEdit()
        {
            DataStream.write();
            MainWindow.Instance.ReloadBtn();
        }

        public static bool AddAccount(Account account)
        {
            if (TeamManager.Nick2Acc(account.nickName) != null)
            {
                return false;
            }
            var group = DataStream.dataStream.Groups[teamIndex];
            group.Accounts.Add(account);
            
            accountsList.Add(account);
            DataStream.write();
            MainWindow.Instance.ReloadBtn();
            return true;
        }

        public static bool AddAccounts(Account account)
        {
            if (TeamManager.Nick2Acc(account.nickName) != null)
            {
                return false;
            }
            var group = DataStream.dataStream.Groups[teamIndex];
            group.Accounts.Add(account);

            accountsList.Add(account);
            //DataStream.write(); 不写
            //MainWindow.Instance.ReloadBtn(); 不更新
            return true;
        }

        public static void MoveAccount(int memberId, int newIndex)
        {
            var group1 = DataStream.dataStream.Groups[newIndex];
            group1.Accounts.Add(accountsList[memberId]);
            var group = DataStream.dataStream.Groups[teamIndex];
            group.Accounts.RemoveAt(memberId);
            DataStream.write();
            MainWindow.Instance.ReloadBtn();
        }

        public static void DeleteAccount(int index) {
            var group = DataStream.dataStream.Groups[teamIndex];
            group.Accounts.RemoveAt(index);
            DataStream.write();
            reloadData();
            MainWindow.Instance.ReloadBtn();
        }

        public static void editTeamName(string teamName0)
        {
            teamName = teamName0;
            var group = DataStream.dataStream.Groups[teamIndex];
            group.Name = teamName;
            DataStream.write();
        }

        public static bool DeleteTeam()
        {
            if (DataStream.dataStream.Groups.Count == 1)
            {
                return false;
            }
            DataStream.dataStream.Groups.RemoveAt(teamIndex);
            DataStream.write();
            reloadData(0);
            MainWindow.Instance.ReloadBtn();
            return true;
        }

    }

    internal class TeamManager
    {
        public static string[] GetAllTeamName()
        {
            string[] names = new string[DataStream.dataStream.Groups.Count];
            int i = 0;
            foreach (var group in DataStream.dataStream.Groups)
            {
                names[i++] = group.Name;
            }
            return names;
        }

        public static void addTeam(string teamName)
        {
            var group = new Group { Name = teamName, Accounts = new List<Account> { } };
            DataStream.dataStream.Groups.Add(group);
            DataStream.write();
        }

        //运行时才能决定是否执行内联
        [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        public static Account? Nick2Acc(string nickName)
        {
            // find and return first take
            foreach (var group in DataStream.dataStream.Groups)
            {
                foreach (var acc in group.Accounts)
                {
                    if (acc.nickName.Equals(nickName)) return acc;
                }
            }
            return null;

        }

    }

}
