using System;
using System.Collections.Generic;
using System.Linq;

namespace Web.Services;

public class RegionState
{
    public string Name { get; set; } = "";
    public string Slug { get; set; } = "";
    public string Terrain { get; set; } = "";
    public int X { get; set; }
    public int Y { get; set; }
    public List<string> Connections { get; set; } = new();
    public int PredatorMeeples { get; set; }
    public int PreyMeeples { get; set; }
    public int FloraMeeples { get; set; }

    public RegionState Clone()
    {
        return new RegionState
        {
            Name = Name,
            Slug = Slug,
            Terrain = Terrain,
            X = X,
            Y = Y,
            Connections = new List<string>(Connections),
            PredatorMeeples = PredatorMeeples,
            PreyMeeples = PreyMeeples,
            FloraMeeples = FloraMeeples
        };
    }
}

public class TurnEvent
{
    public int TurnNumber { get; set; }
    public string MeepleType { get; set; } = "";
    public List<string> RegionTypes { get; set; } = new();
    public List<string> Details { get; set; } = new();
}

public class SimulationData
{
    public List<RegionState> Regions { get; set; } = new();
    public List<RegionState> InitialRegions { get; set; } = new();
    public List<TurnEvent> Events { get; set; } = new();
    public List<List<RegionState>> Timeline { get; set; } = new();
    public int CurrentTurn { get; set; }
    public bool IsInitialized { get; set; }
}

public class GameSimulationService
{
    private readonly Random _rng = new();

    public SimulationData Data { get; private set; } = new();

    public void RunSimulation()
    {
        Data = new SimulationData();
        var regions = CreateRegions();
        Data.Regions = regions;
        AddInitialFlora();
        AddRandomMeepleCombos();
        Data.InitialRegions = Data.Regions.Select(r => r.Clone()).ToList();
        GenerateAndApplyEvents();
        Data.IsInitialized = true;
    }

    public void RandomizeEvents()
    {
        var initialRegions = Data.InitialRegions;
        Data = new SimulationData();
        Data.Regions = initialRegions.Select(r => r.Clone()).ToList();
        Data.InitialRegions = initialRegions;
        GenerateAndApplyEvents();
        Data.IsInitialized = true;
    }

    public List<RegionState> GetCurrentState()
    {
        return Data.Timeline[Data.CurrentTurn].Select(r => r.Clone()).ToList();
    }

    public TurnEvent? GetCurrentEvent()
    {
        return Data.Events.ElementAtOrDefault(Data.CurrentTurn - 1);
    }

    private void GenerateAndApplyEvents()
    {
        Data.Timeline.Add(Data.Regions.Select(r => r.Clone()).ToList());

        for (int turn = 1; turn <= 20; turn++)
        {
            var turnEvent = GenerateTurnEvent(turn);
            ApplyTurnEvent(turnEvent);
            Data.Events.Add(turnEvent);
            Data.Timeline.Add(Data.Regions.Select(r => r.Clone()).ToList());
        }

        Data.CurrentTurn = 1;
    }

    private TurnEvent GenerateTurnEvent(int turnNumber)
    {
        string[] meepleTypes = { "Predator", "Prey", "Flora" };
        var meepleType = meepleTypes[_rng.Next(3)];

        string[] regionTypes = { "Grass", "Forest", "Mountain", "Water", "Wasteland" };
        var numTypes = _rng.Next(1, 4);
        var selectedTypes = regionTypes.OrderBy(_ => _rng.Next()).Take(numTypes).ToList();

        return new TurnEvent
        {
            TurnNumber = turnNumber,
            MeepleType = meepleType,
            RegionTypes = selectedTypes,
            Details = new List<string>()
        };
    }

    private void ApplyTurnEvent(TurnEvent turnEvent)
    {
        var details = new List<string>();

        foreach (var region in Data.Regions)
        {
            if (!turnEvent.RegionTypes.Contains(region.Terrain))
                continue;

            switch (turnEvent.MeepleType)
            {
                case "Flora":
                    region.FloraMeeples++;
                    details.Add($"{region.Name}: +1 Flora");
                    break;

                case "Predator":
                    ApplyPredatorActivation(region, details);
                    break;

                case "Prey":
                    ApplyPreyActivation(region, details);
                    break;
            }
        }

        turnEvent.Details = details;
    }

    private void ApplyPredatorActivation(RegionState region, List<string> details)
    {
        if (region.PredatorMeeples <= 0)
            return;

        if (region.PreyMeeples > 0)
        {
            int eaten = Math.Min(region.PredatorMeeples, region.PreyMeeples);
            region.PreyMeeples -= eaten;
            region.PredatorMeeples += eaten;
            details.Add($"{region.Name}: Predators ate {eaten} prey, now {region.PredatorMeeples}P / {region.PreyMeeples}R");
        }
        else
        {
            var target = FindBestTravelTarget(region, r => r.PreyMeeples > 0);
            if (target != null)
            {
                int traveling = region.PredatorMeeples;
                target.PredatorMeeples += traveling;
                region.PredatorMeeples = 0;
                details.Add($"{region.Name}: {traveling} predators traveled to {target.Name}");
            }
        }
    }

    private void ApplyPreyActivation(RegionState region, List<string> details)
    {
        if (region.PreyMeeples <= 0)
            return;

        if (region.FloraMeeples > 0)
        {
            int consumed = Math.Min(region.PreyMeeples, region.FloraMeeples);
            region.FloraMeeples -= consumed;
            region.PreyMeeples += consumed;
            details.Add($"{region.Name}: Prey consumed {consumed} flora, now {region.PreyMeeples}R / {region.FloraMeeples}F");
        }
        else
        {
            var target = FindBestTravelTarget(region, r => r.FloraMeeples > 0);
            if (target != null)
            {
                int traveling = region.PreyMeeples;
                target.PreyMeeples += traveling;
                region.PreyMeeples = 0;
                details.Add($"{region.Name}: {traveling} prey traveled to {target.Name}");
            }
        }
    }

    private RegionState? FindBestTravelTarget(RegionState region, Func<RegionState, bool> hasResource)
    {
        var direct = region.Connections
            .Select(name => Data.Regions.FirstOrDefault(r => r.Name == name))
            .OfType<RegionState>()
            .Where(r => hasResource(r))
            .ToList();

        if (direct.Count > 0)
        {
            return direct.OrderByDescending(r => r.PreyMeeples + r.FloraMeeples).First();
        }

        var visited = new HashSet<string> { region.Name };
        var queue = new Queue<string>(region.Connections);
        while (queue.Count > 0)
        {
            var currentName = queue.Dequeue();
            if (!visited.Add(currentName)) continue;
            var current = Data.Regions.FirstOrDefault(r => r.Name == currentName);
            if (current == null) continue;
            if (hasResource(current))
                return current;
            foreach (var conn in current.Connections)
            {
                if (!visited.Contains(conn))
                    queue.Enqueue(conn);
            }
        }

        return null;
    }

    private void AddInitialFlora()
    {
        var excluded = new HashSet<string>
        {
            "Grass 1", "Grass 2", "Wasteland 1",
            "Mountain 1", "Mountain 2", "Mountain 3", "Mountain 4", "Mountain 5"
        };

        foreach (var region in Data.Regions)
        {
            if (!excluded.Contains(region.Name))
            {
                region.FloraMeeples = 1;
            }
        }
    }

    private void AddRandomMeepleCombos()
    {
        var combos = new List<(int pred, int prey, int flora)>();
        for (int total = 1; total <= 3; total++)
        {
            for (int p = 0; p <= total; p++)
            {
                for (int r = 0; r <= total - p; r++)
                {
                    int f = total - p - r;
                    combos.Add((p, r, f));
                }
            }
        }

        var shuffled = combos.OrderBy(_ => _rng.Next()).ToList();
        for (int i = 0; i < Data.Regions.Count; i++)
        {
            var (p, r, f) = shuffled[i % shuffled.Count];
            Data.Regions[i].PredatorMeeples = p;
            Data.Regions[i].PreyMeeples = r;
            Data.Regions[i].FloraMeeples = f;
        }
    }

    private static List<RegionState> CreateRegions()
    {
        return new List<RegionState>
        {
            new() { Name = "Grass 1", Slug = "grass-1", Terrain = "Grass", X = 15, Y = 260, Connections = new() { "Mountain 1", "Water 2", "Forest 1" } },
            new() { Name = "Grass 2", Slug = "grass-2", Terrain = "Grass", X = 150, Y = 400, Connections = new() { "Forest 1", "Forest 2" } },
            new() { Name = "Grass 3", Slug = "grass-3", Terrain = "Grass", X = 300, Y = 230, Connections = new() { "Water 2", "Mountain 2", "Mountain 3" } },
            new() { Name = "Grass 4", Slug = "grass-4", Terrain = "Grass", X = 510, Y = 30, Connections = new() { "Mountain 4", "Wasteland 1" } },
            new() { Name = "Grass 5", Slug = "grass-5", Terrain = "Grass", X = 550, Y = 290, Connections = new() { "Mountain 5", "Water 5", "Wasteland 1", "Forest 4" } },
            new() { Name = "Forest 1", Slug = "forest-1", Terrain = "Forest", X = 60, Y = 330, Connections = new() { "Grass 1", "Grass 2", "Water 1", "Forest 2" } },
            new() { Name = "Forest 2", Slug = "forest-2", Terrain = "Forest", X = 250, Y = 370, Connections = new() { "Grass 2", "Water 2", "Forest 1" } },
            new() { Name = "Forest 3", Slug = "forest-3", Terrain = "Forest", X = 150, Y = 125, Connections = new() { "Water 2", "Water 3" } },
            new() { Name = "Forest 4", Slug = "forest-4", Terrain = "Forest", X = 420, Y = 100, Connections = new() { "Grass 4", "Wasteland 1", "Grass 5" } },
            new() { Name = "Forest 5", Slug = "forest-5", Terrain = "Forest", X = 470, Y = 410, Connections = new() { "Water 5" } },
            new() { Name = "Mountain 1", Slug = "mountain-1", Terrain = "Mountain", X = 20, Y = 120, Connections = new() { "Grass 1", "Water 3", "Forest 3" } },
            new() { Name = "Mountain 2", Slug = "mountain-2", Terrain = "Mountain", X = 260, Y = 110, Connections = new() { "Water 3", "Forest 3" } },
            new() { Name = "Mountain 3", Slug = "mountain-3", Terrain = "Mountain", X = 380, Y = 180, Connections = new() { "Mountain 5", "Forest 4", "Grass 3" } },
            new() { Name = "Mountain 4", Slug = "mountain-4", Terrain = "Mountain", X = 370, Y = 30, Connections = new() { "Forest 4", "Grass 4" } },
            new() { Name = "Mountain 5", Slug = "mountain-5", Terrain = "Mountain", X = 430, Y = 330, Connections = new() { "Grass 5", "Mountain 3" } },
            new() { Name = "Water 1", Slug = "water-1", Terrain = "Water", X = 30, Y = 410, Connections = new() { "Forest 1" } },
            new() { Name = "Water 2", Slug = "water-2", Terrain = "Water", X = 140, Y = 260, Connections = new() { "Forest 1", "Forest 2", "Grass 1", "Forest 3", "Grass 3" } },
            new() { Name = "Water 3", Slug = "water-3", Terrain = "Water", X = 50, Y = 50, Connections = new() { "Mountain 1", "Mountain 2", "Forest 3" } },
            new() { Name = "Water 4", Slug = "water-4", Terrain = "Water", X = 640, Y = 110, Connections = new() { "Grass 4", "Wasteland 1" } },
            new() { Name = "Water 5", Slug = "water-5", Terrain = "Water", X = 600, Y = 400, Connections = new() { "Grass 5", "Forest 5" } },
            new() { Name = "Wasteland 1", Slug = "wasteland-1", Terrain = "Wasteland", X = 560, Y = 120, Connections = new() { "Forest 4", "Grass 4", "Grass 5", "Water 4" } }
        };
    }
}
