using System.Xml;
using Hawkeye.VisionBuilder.Workflow;

// Check if directory argument is provided
if (args.Length == 0)
{
    Console.WriteLine("Usage: RecipeConverter.exe <directory>");
    Console.WriteLine("Example: RecipeConverter.exe C:\\MyRecipes");
    return;
}

string targetDirectory = args[0];

// Validate directory exists
if (!Directory.Exists(targetDirectory))
{
    Console.WriteLine($"Error: Directory '{targetDirectory}' does not exist.");
    return;
}

var files = Directory.GetFiles(targetDirectory, "*.hrcp", SearchOption.TopDirectoryOnly);

if (files.Length == 0)
{
    Console.WriteLine($"No *.hrcp files found in directory '{targetDirectory}'");
    return;
}

Console.WriteLine($"Found {files.Length} recipe file(s) in '{targetDirectory}'");

foreach (var file in files)
{
    try
    {
        Console.WriteLine($"Converting {file}");
        var f = File.OpenRead(file);
        WorkflowList list = new WorkflowList();
        list.Load(new BinaryReader(f), new OperationDiscoveryService(list));
        
        // Save the converted file in the same directory as the source
        string outputPath = Path.Combine(targetDirectory, Path.GetFileNameWithoutExtension(file) + ".jhrcp");
        list.SaveJSON(outputPath);
        
        Console.WriteLine($"Saved: {outputPath}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error processing file {file}: {ex.Message}");
    }
}



