using System.Collections.Generic;
using UnityEngine;
using System;

public class NameParser : MonoBehaviour
{
    public static NameParser Instance;
    public TextAsset[] TextAssets;
    private System.Random rng = new System.Random();

    private List<string> Names = new List<string>();
    private int _indexToUse = 0;

    private void Awake()
    {
        Instance = this;

        foreach (var textAsset in TextAssets)
        {
            var data = textAsset.text.Split('\n');
            foreach (var dataEntry in data)
            {
                Names.Add(dataEntry);
            }
        }

        Names = Shuffle(Names);
    }

    public string GetRandomName()
    {
        var name = Names[_indexToUse];
        _indexToUse++;
        return name;
    }

    public List<T> Shuffle<T>(List<T> list)
    {
        int n = list.Count;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            T value = list[k];
            list[k] = list[n];
            list[n] = value;
        }

        return list;
    }
}
