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

namespace Server.Items
{
    // The class is now static, meaning it exists globally and not as an Item
    public static class FarmController
    {
        // Vegetable Plot Boundaries
        private static Rectangle2D CabbagePlot = new Rectangle2D(795, 771, 5, 4);
        private static Rectangle2D CarrotPlot = new Rectangle2D(795, 776, 5, 4);
        private static Rectangle2D LettucePlot = new Rectangle2D(795, 781, 5, 5);
        private static Rectangle2D OnionPlot = new Rectangle2D(801, 771, 5, 4);
        private static Rectangle2D TurnipPlot = new Rectangle2D(801, 776,5 ,4);
        private static Rectangle2D PumpkinPlot = new Rectangle2D(801, 781, 5, 5);
        private static Rectangle2D WheatPlot = new Rectangle2D(808, 771, 5, 4);
        private static Rectangle2D FlaxPlot = new Rectangle2D(808, 776, 5, 4);
        private static Rectangle2D CottonPlot = new Rectangle2D(808, 781, 5, 5);

        // Reagent Plot Boundaries
        //private static Rectangle2D ReagentRect = new Rectangle2D(820, 843, 23, 16);

        // Lists must be static now
        //private static List<Type> m_VeggieTypes = new List<Type> { typeof(FarmableCabbage), typeof(FarmableCarrot), typeof(FarmableCotton), typeof(FarmableFlax), typeof(FarmableLettuce), typeof(FarmableOnion), typeof(FarmablePumpkin), typeof(FarmableTurnip), typeof(FarmableWheat) };
        //private static List<Type> m_ReagentTypes = new List<Type> { typeof(FarmableBlackPearl), typeof(FarmableBloodmoss), typeof(FarmableGarlic), typeof(FarmableGinseng), typeof(FarmableMandrakeRoot), typeof(FarmableNightshade), typeof(FarmableSpidersSilk), typeof(FarmableSulfurousAsh) };

        // The core looks for 'Initialize' on startup and runs it automatically
        public static void Initialize()
        {
            double mySpawnVal = Server.ValidSettings.FarmSpawnTimer();
            TimeSpan spawnInterval = TimeSpan.FromMinutes(mySpawnVal);
            
            // Start the global background timer
            Timer.DelayCall(spawnInterval, spawnInterval, SpawnTick);
        }

        private static void SpawnTick()
        {
            // Spawn 2 vegetables and 2 reagents each tick if space allows
            SpawnInArea(CabbagePlot, new List<Type> { typeof(FarmableCabbage) }, 1);
            SpawnInArea(CarrotPlot, new List<Type> { typeof(FarmableCarrot) }, 1);
            SpawnInArea(LettucePlot, new List<Type> { typeof(FarmableLettuce) }, 1);
            SpawnInArea(OnionPlot, new List<Type> { typeof(FarmableOnion) }, 1);
            SpawnInArea(TurnipPlot, new List<Type> { typeof(FarmableTurnip) }, 1);
            SpawnInArea(PumpkinPlot, new List<Type> { typeof(FarmablePumpkin) }, 1);
            SpawnInArea(WheatPlot, new List<Type> { typeof(FarmableWheat) }, 1);
            SpawnInArea(FlaxPlot, new List<Type> { typeof(FarmableFlax) }, 1);
            SpawnInArea(CottonPlot, new List<Type> { typeof(FarmableCotton) }, 1);
        }

        private static void SpawnInArea(Rectangle2D rect, List<Type> types, int count)
        {
            // Safety net for empty lists
            if (types == null || types.Count == 0)
                return;

            for (int i = 0; i < count; i++)
            {
                int x = Utility.Random(rect.X, rect.Width);
                int y = Utility.Random(rect.Y, rect.Height);
                Point3D loc = new Point3D(x, y, 0);

                bool occupied = false;
                
                // Get the items in range
                IPooledEnumerable eable = Map.Felucca.GetItemsInRange(loc, 0);

                // If the loop runs even once, something is there
                foreach (Item item in eable)
                {
                    occupied = true;
                    break;
                }
                
                // Free the pool to prevent memory leaks
                eable.Free();

                // If the spot is empty, spawn the crop
                if (!occupied)
                {
                    Type type = types[Utility.Random(types.Count)];
                    Item crop = (Item)Activator.CreateInstance(type);
                    crop.MoveToWorld(loc, Map.Felucca);
                }
            }
        }
    }
}
