using UnityEngine;
using System.Collections.Generic;

public static class FantasyNameGenerator
{
    // Town name components
    private static readonly string[] townPrefixes = {
        "North", "South", "East", "West", "Upper", "Lower", "Old", "New", "High", "Deep",
        "Iron", "Gold", "Silver", "Stone", "Wood", "Green", "Red", "White", "Black", "Blue",
        "Dragon", "Eagle", "Wolf", "Bear", "Lion", "Oak", "Pine", "Ash", "Elm", "Willow",
        "Storm", "Wind", "Sun", "Moon", "Star", "Shadow", "Light", "Dark", "Bright", "Swift"
    };

    private static readonly string[] townSuffixes = {
        "haven", "ford", "bridge", "gate", "hill", "dale", "vale", "moor", "field", "wood",
        "burg", "ton", "ham", "wick", "thorpe", "by", "stead", "hurst", "marsh", "brook",
        "glen", "ridge", "peak", "crest", "hollow", "bend", "crossing", "falls", "rapids", "port",
        "watch", "guard", "hold", "keep", "tower", "wall", "stone", "rock", "cliff", "shore"
    };

    // Village name components (smaller, more rural)
    private static readonly string[] villagePrefixes = {
        "Little", "Small", "Quiet", "Peaceful", "Hidden", "Lost", "Forgotten", "Ancient", "Humble", "Simple",
        "Moss", "Fern", "Ivy", "Rose", "Lily", "Daisy", "Thistle", "Heather", "Sage", "Mint",
        "Rabbit", "Fox", "Deer", "Sheep", "Goat", "Duck", "Goose", "Crow", "Sparrow", "Robin",
        "Brook", "Creek", "Stream", "Pond", "Spring", "Well", "Mill", "Farm", "Orchard", "Garden"
    };

    private static readonly string[] villageSuffixes = {
        "hollow", "grove", "glen", "meadow", "vale", "dell", "nook", "corner", "bend", "twist",
        "green", "common", "field", "yard", "patch", "plot", "clearing", "glade", "thicket", "copse",
        "end", "side", "edge", "rim", "foot", "base", "rest", "peace", "quiet", "still"
    };

    // Forest name components
    private static readonly string[] forestPrefixes = {
        "Ancient", "Dark", "Deep", "Wild", "Mystic", "Enchanted", "Whispering", "Silent", "Sacred", "Forbidden",
        "Elder", "Old", "Primeval", "Twisted", "Tangled", "Dense", "Thick", "Shadowy", "Moonlit", "Sunlit",
        "Emerald", "Verdant", "Lush", "Green", "Golden", "Amber", "Crimson", "Silver", "Misty", "Foggy"
    };

    private static readonly string[] forestSuffixes = {
        "Wood", "Woods", "Forest", "Grove", "Thicket", "Copse", "Brake", "Weald", "Wildwood", "Greenwood",
        "Timberland", "Woodland", "Glade", "Dell", "Hollow", "Vale", "Glen", "Chase", "Park", "Reserve"
    };

    // Water body name components
    private static readonly string[] waterPrefixes = {
        "Crystal", "Clear", "Deep", "Blue", "Azure", "Sapphire", "Emerald", "Silver", "Golden", "Mirror",
        "Calm", "Still", "Peaceful", "Serene", "Tranquil", "Misty", "Foggy", "Hidden", "Secret", "Lost",
        "Dragon", "Serpent", "Swan", "Eagle", "Raven", "Heron", "Crane", "Trout", "Salmon", "Pike"
    };

    private static readonly string[] lakeSuffixes = {
        "Lake", "Pond", "Pool", "Mere", "Tarn", "Loch", "Water", "Waters", "Basin", "Reservoir"
    };

    private static readonly string[] oceanSuffixes = {
        "Sea", "Ocean", "Bay", "Gulf", "Sound", "Strait", "Channel", "Depths", "Expanse", "Waters"
    };

    // Male character name components
    private static readonly string[] maleFirstNames = {
        "Aedric", "Aldwin", "Baelor", "Cedric", "Dorian", "Edmund", "Gareth", "Henrik", "Ivar", "Jasper",
        "Kieran", "Leoric", "Magnus", "Nolan", "Osric", "Percival", "Quinton", "Roderick", "Soren", "Theron",
        "Ulric", "Victor", "Willem", "Xavier", "Yorick", "Zander", "Alaric", "Bastian", "Caspian", "Darius",
        "Elias", "Felix", "Gideon", "Hadrian", "Ivan", "Joren", "Kael", "Lucian", "Matthias", "Nikolai",
        "Orion", "Phoenix", "Quin", "Raphael", "Sebastian", "Tristan", "Uther", "Varian", "Warden", "Xander"
    };

    private static readonly string[] maleLastNames = {
        "Ironforge", "Stormwind", "Blackwater", "Goldleaf", "Silverstone", "Redmane", "Whitehawk", "Greycloak",
        "Dragonbane", "Wolfheart", "Bearclaw", "Lionmane", "Eaglefeather", "Ravencrest", "Swiftarrow", "Steelborn",
        "Frostbeard", "Fireborn", "Shadowbane", "Lightbringer", "Darkblade", "Brightshield", "Strongarm", "Swiftstrike",
        "Stonehammer", "Ironwill", "Goldenshield", "Silverwind", "Blackthorne", "Whitestone", "Redguard", "Greenvale",
        "Nightfall", "Dawnbreaker", "Moonwhisper", "Sunblade", "Stargazer", "Stormcaller", "Windrunner", "Earthshaker"
    };

    // Female character name components
    private static readonly string[] femaleFirstNames = {
        "Aria", "Brenna", "Celeste", "Diana", "Elara", "Fiona", "Gwendolyn", "Helena", "Iris", "Jasmine",
        "Kira", "Luna", "Mira", "Nora", "Ophelia", "Penelope", "Quinn", "Rosalind", "Sera", "Thalia",
        "Una", "Vera", "Willow", "Xara", "Yara", "Zara", "Adelina", "Beatrice", "Cordelia", "Delphine",
        "Evangeline", "Freya", "Guinevere", "Hazel", "Isolde", "Josephine", "Katarina", "Lyanna", "Morgana", "Natasha",
        "Octavia", "Persephone", "Quintessa", "Raven", "Selene", "Titania", "Ursula", "Vivienne", "Winifred", "Xylia"
    };

    private static readonly string[] femaleLastNames = {
        "Moonwhisper", "Starweaver", "Roseheart", "Silverleaf", "Goldenbraid", "Whitedove", "Redrose", "Greenvine",
        "Nightsong", "Dawnlight", "Shadowdancer", "Lightweaver", "Brightmoon", "Darkrose", "Swiftwind", "Gentleheart",
        "Silverblossom", "Goldenpetal", "Crystalbrook", "Pearlwater", "Sapphiregaze", "Emeraldwing", "Rubyflame", "Amberlight",
        "Ivythorn", "Willowbend", "Oakenshield", "Birchwood", "Elmshade", "Ashbloom", "Thornwick", "Briarrose",
        "Mistwalker", "Cloudwhisper", "Rainbringer", "Snowfall", "Frostbloom", "Iceheart", "Flamekeeper", "Embersong"
    };

    public static string GenerateTownName()
    {
        string prefix = townPrefixes[Random.Range(0, townPrefixes.Length)];
        string suffix = townSuffixes[Random.Range(0, townSuffixes.Length)];
        return prefix + suffix;
    }

    public static string GenerateVillageName()
    {
        string prefix = villagePrefixes[Random.Range(0, villagePrefixes.Length)];
        string suffix = villageSuffixes[Random.Range(0, villageSuffixes.Length)];
        return prefix + suffix;
    }

    public static string GenerateForestName()
    {
        string prefix = forestPrefixes[Random.Range(0, forestPrefixes.Length)];
        string suffix = forestSuffixes[Random.Range(0, forestSuffixes.Length)];
        return prefix + " " + suffix;
    }

    public static string GenerateLakeName()
    {
        string prefix = waterPrefixes[Random.Range(0, waterPrefixes.Length)];
        string suffix = lakeSuffixes[Random.Range(0, lakeSuffixes.Length)];
        return prefix + " " + suffix;
    }

    public static string GenerateOceanName()
    {
        string prefix = waterPrefixes[Random.Range(0, waterPrefixes.Length)];
        string suffix = oceanSuffixes[Random.Range(0, oceanSuffixes.Length)];
        return prefix + " " + suffix;
    }

    public static string GenerateMaleCharacterName()
    {
        string firstName = maleFirstNames[Random.Range(0, maleFirstNames.Length)];
        string lastName = maleLastNames[Random.Range(0, maleLastNames.Length)];
        return firstName + " " + lastName;
    }

    public static string GenerateFemaleCharacterName()
    {
        string firstName = femaleFirstNames[Random.Range(0, femaleFirstNames.Length)];
        string lastName = femaleLastNames[Random.Range(0, femaleLastNames.Length)];
        return firstName + " " + lastName;
    }

    public static string GenerateCharacterName(bool isMale = true)
    {
        return isMale ? GenerateMaleCharacterName() : GenerateFemaleCharacterName();
    }

    // Generate multiple names for different regions/clusters
    public static List<string> GenerateMultipleNames(int count, System.Func<string> generator)
    {
        List<string> names = new List<string>();
        HashSet<string> usedNames = new HashSet<string>();

        for (int i = 0; i < count; i++)
        {
            string name;
            int attempts = 0;
            do
            {
                name = generator();
                attempts++;
            } while (usedNames.Contains(name) && attempts < 50);

            if (!usedNames.Contains(name))
            {
                names.Add(name);
                usedNames.Add(name);
            }
            else
            {
                // If we can't generate a unique name, add a number
                names.Add(name + " " + (i + 1));
            }
        }

        return names;
    }
}
