using System;
using System.Collections.Generic;
using Server;
using Server.Items;

namespace Server.Scripts.Custom
{
    public class TasksManager
    {
        public static void Initialize()
        {
            // Timer.DelayCall parameters: (Initial Delay, Interval between runs, Method to call)
            // We give the server 1 minute to finish booting up before running the first sweep, 
            // then it runs exactly every 1 hour after that.
            Timer.DelayCall(TimeSpan.FromMinutes(1), TimeSpan.FromHours(1), new TimerCallback(ExecuteTasks));
            
            Console.WriteLine("TasksManager: Hourly automated tasks initialized.");
        }

        public static void ExecuteTasks()
        {
            Console.WriteLine("TasksManager: Running scheduled hourly tasks...");

            if ( Settings.S_DungeonFloorTraps )
            { 
                ClearSpawnedTraps();
            }

            // FUTURE TASKS: 
            // Just add new methods here as your server grows!
            // Example: ClearAbandonedTents();
            // Example: AnnounceHourlyLottery();
        }

        private static void ClearSpawnedTraps()
        {
            // We must put them in a list first. If we delete them while actively 
            // searching the World.Items dictionary, it will cause a crash.
            List<Item> trapsToDelete = new List<Item>();

            // Scan the entire world for any item that acts as a floor trap
            foreach (Item item in World.Items.Values)
            {
                if (item is BaseTrap)
                {
                    trapsToDelete.Add(item);
                }
            }

            // Loop through our safe list and delete them all
            foreach (Item trap in trapsToDelete)
            {
                trap.Delete();
            }

            Console.WriteLine($"TasksManager: Wiped {trapsToDelete.Count} floor traps. Spawners will now replace them.");
        }
    }
}
