// See https://aka.ms/new-console-template for more information

using IS_Lab2_JSON_CS;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

var serializer = new DeserializerBuilder()
    .WithNamingConvention(UnderscoredNamingConvention.Instance)
    .Build();

DeserializeJson newDeserializer = null!;
var config = serializer.Deserialize<YamlConfig>(File.ReadAllText("Assets/basic_config.yaml"));
var operations = new Dictionary<string, Action<string>>()
{
    { "deserialize", Deserialize },
    { "serialize", Serialize },
    { "convert", Convert }
};

foreach (var op in config.Launch.Operations)
{
    if (!operations.TryGetValue(op, out var callback))
        continue;

    callback(config.Launch.Source);
}

void Deserialize(string src)
{
    newDeserializer = new DeserializeJson($"{config.Paths.SourceFolder}{config.Paths.JsonSourceFile}");
    newDeserializer.SomeStats();
}

void Serialize(string src)
{
    switch (src)
    {
        case "object":
            SerializeJson.Run(newDeserializer, $"{config.Paths.SourceFolder}{config.Paths.JsonDestinationFile}");
            return;
        
        case "file":
            SerializeJson.Run($"{config.Paths.SourceFolder}{config.Paths.JsonSourceFile}", $"{config.Paths.SourceFolder}{config.Paths.JsonDestinationFile}");
            return;
        
        default:
            throw new ArgumentOutOfRangeException(nameof(src));
    }
}

void Convert(string src)
{
    switch (src)
    {
        case "object":
            ConvertJsonToYaml.Run(newDeserializer, $"{config.Paths.SourceFolder}{config.Paths.YamlDestinationFile}");
            return;
        
        case "file":
            ConvertJsonToYaml.Run($"{config.Paths.SourceFolder}{config.Paths.JsonSourceFile}", $"{config.Paths.SourceFolder}{config.Paths.YamlDestinationFile}");
            return;
        
        default:
            throw new ArgumentOutOfRangeException(nameof(src));
    }
}