// Copyright © 2017-2018 Atomic Software, LLC. All Rights Reserved.
// See LICENSE.md for full license information.

using Atom.Core;
using Atom.Core.Local;
using Atom.LoginServer.Services;
using System.Linq;

namespace Atom.LoginServer.Engines
{
    internal class WMEngine : IEngine
    {
        public void Main(long now)
        {
            // Unregister all worlds that are no longer connected.
            WMService.Worlds.Where(x => !x.Client.IsConnected).ToList().ForEach(WMService.UnregisterWorld);

            lock (WMService.Worlds)
            {
                foreach (var World in WMService.Worlds)
                {
                    if (now - World.LastStatusUpdate < 5000)
                    {
                        continue;
                    }

                    World.LastStatusUpdate = now;

                    switch (World.Load)
                    {
                        case -1:
                            World.Status = WorldStatus.Maintenance;
                            break;
                        case -2:
                            World.Status = WorldStatus.Offline;
                            break;
                        default:
                            if (World.Load < 2)
                            {
                                World.Status = WorldStatus.Low;
                            }
                            else if (World.Load < 5)
                            {
                                World.Status = WorldStatus.Medium;
                            }
                            else
                            {
                                World.Status = WorldStatus.High;
                            }
                            break;
                    }
                }
            }
        }
    }
}
