using UnityEngine;
using System.Collections.Generic;

public class FeatureGenerator
{
    private SettlementGenerator settlementGenerator;
    private RoadGenerator roadGenerator;

    public FeatureGenerator()
    {
        settlementGenerator = new SettlementGenerator();
        roadGenerator = new RoadGenerator();
    }

    public void GenerateFeatures(MapData mapData)
    {
        // Generate settlements first
        settlementGenerator.GenerateSettlements(mapData);

        // Generate roads connecting settlements (handles bridges and tunnels automatically)
        roadGenerator.GenerateRoads(mapData);

        // Generate harbours
        GenerateHarbours(mapData);
    }

    private void GenerateHarbours(MapData mapData)
    {
        var settlements = mapData.GetTowns();
        settlements.AddRange(mapData.GetVillages());

        foreach (var settlement in settlements)
        {
            // Check if settlement is near water
            if (IsNearWater(mapData, settlement.position.x, settlement.position.y, 3))
            {
                // Find a suitable position for harbour
                var harbourPos = FindHarbourPosition(mapData, settlement.position);
                if (harbourPos != Vector2Int.zero)
                {
                    mapData.GetTile(harbourPos.x, harbourPos.y).featureType = FeatureType.Harbour;
                }
            }
        }
    }

    private Vector2Int FindHarbourPosition(MapData mapData, Vector2Int settlementPos)
    {
        for (int range = 1; range <= 3; range++)
        {
            for (int x = settlementPos.x - range; x <= settlementPos.x + range; x++)
            {
                for (int y = settlementPos.y - range; y <= settlementPos.y + range; y++)
                {
                    if (mapData.IsValidPosition(x, y))
                    {
                        MapTile tile = mapData.GetTile(x, y);
                        if (tile.terrainType == TerrainType.Shore &&
                            IsAdjacentToWater(mapData, x, y))
                        {
                            return new Vector2Int(x, y);
                        }
                    }
                }
            }
        }
        return Vector2Int.zero;
    }

    private bool IsNearWater(MapData mapData, int x, int y, int range)
    {
        for (int dx = -range; dx <= range; dx++)
        {
            for (int dy = -range; dy <= range; dy++)
            {
                int checkX = x + dx;
                int checkY = y + dy;

                if (mapData.IsValidPosition(checkX, checkY))
                {
                    if (mapData.GetTile(checkX, checkY).IsWater())
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private bool IsAdjacentToWater(MapData mapData, int x, int y)
    {
        var neighbors = mapData.GetNeighbors(x, y);
        foreach (var neighbor in neighbors)
        {
            if (mapData.GetTile(neighbor.x, neighbor.y).IsWater())
            {
                return true;
            }
        }
        return false;
    }
}