<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Xml.dll" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>

<#
    var path = Path.GetDirectoryName(Host.TemplateFile);
    var localPath = Host.ResolvePath(Path.Combine(path,"../../GameIdeas/Server/GameIdeas.WebAPI/Files/GameIdeas.fr.json"));

    var json = File.ReadAllText(localPath);

    var index = 0;


    Dictionary<string, string?> ParseToDictionary()
    {
        SkipWhitespace();
        if (index >= json.Length || json[index] != '{')
            throw new Exception("Invalid JSON: Root must be an object.");

        return ParseObjectToDictionary();
    }

    Dictionary<string, string?> ParseObjectToDictionary()
    {
        var dict = new Dictionary<string, string?>();
        Consume('{');

        while (true)
        {
            SkipWhitespace();
            if (json[index] == '}') break;

            var key = ParseString();
            SkipWhitespace();
            Consume(':');
            SkipWhitespace();
            var value = ParseValueAsString();
            dict[key] = value;

            SkipWhitespace();
            if (json[index] == ',') Consume(',');
            else if (json[index] == '}') break;
            else throw new Exception("Expected ',' or '}' in object.");
        }

        Consume('}');
        return dict;
    }

    string ParseString()
    {
        Consume('"');
        var start = index;
        while (json[index] != '"')
        {
            if (json[index] == '\\') index++; // Skip escape character
            index++;
        }

        var result = json.Substring(start, index - start);
        Consume('"');
        return result.Replace("\\\"", "\""); // Handle escaped quotes
    }

    string? ParseValueAsString()
    {
        SkipWhitespace();

        if (json[index] == '"') return ParseString();
        if (char.IsDigit(json[index]) || json[index] == '-' || json[index] == '.') return ParseNumber();
        if (json[index] == 't') { ConsumeLiteral("true"); return "true"; }
        if (json[index] == 'f') { ConsumeLiteral("false"); return "false"; }
        if (json[index] == 'n') { ConsumeLiteral("null"); return null; }
        if (json[index] == '{' || json[index] == '[')
            throw new Exception("Nested objects or arrays are not supported in this implementation.");

        throw new Exception($"Unexpected character at index {index}");
    }

    string ParseNumber()
    {
        var start = index;
        if (json[index] == '-') index++; // Handle negative sign
        while (index < json.Length && char.IsDigit(json[index])) index++;
        if (index < json.Length && json[index] == '.') // Handle fractional part
        {
            index++;
            while (index < json.Length && char.IsDigit(json[index])) index++;
        }
        return json.Substring(start, index - start);
    }

    void ConsumeLiteral(string literal)
    {
        for (int i = 0; i < literal.Length; i++)
        {
            if (index >= json.Length || json[index] != literal[i])
                throw new Exception($"Expected '{literal}' but found '{json[index]}'");
            index++;
        }
    }

    void Consume(char expected)
    {
        if (json[index] != expected)
            throw new Exception($"Expected '{expected}' but found '{json[index]}'");
        index++;
    }

    void SkipWhitespace()
    {
        while (index < json.Length && char.IsWhiteSpace(json[index]))
            index++;
    }
    // Deserialize the JSON content to a dictionary
    //var translations = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonContent);
    var translations = ParseToDictionary();

    // Generate the class
#>
namespace GameIdeas.Resources;

public class Translations (TranslationService translationService)
{
<#
    // Iterate through the dictionary and generate properties
    foreach (var translation in translations)
    {
#>
    public string <#= translation.Key #> => translationService.Translate(nameof(<#= translation.Key #>));
<#
    }
#>
}

public static class ResourcesKey
{
    private static Translations? _instance;

    public static void Initialize(Translations translations)
    {
        _instance = translations;
    }

<#
    // Iterate through the dictionary and generate properties
    foreach (var translation in translations)
    {
#>
    public static string <#= translation.Key #> => _instance?.<#= translation.Key #> ?? throw new InvalidOperationException("ResourcesKey.<#= translation.Key #> is not initialized.");
<#
    }
#>
}