using CollabVMClient.ViewModels;
using CollabVMSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace CollabVMClient
{
public static class Utilities
{
///
/// Return view models for each node a cvm server offers
///
public static async Task GetNodes(string url)
{
var cvm = new CollabVMSharp.CollabVMClient(url);
await cvm.Connect();
var nodes = await cvm.ListVMs();
await cvm.Disconnect();
var nodelist = new List();
foreach (var node in nodes)
{
using var ms = new MemoryStream(node.Thumbnail);
nodelist.Add(new CVMNodeViewModel(url, node.ID, node.Name, new Avalonia.Media.Imaging.Bitmap(ms)));
}
return nodelist.ToArray();
}
public static void WriteDefaultConfig()
{
var cj = JsonSerializer.Serialize(Constants.DefaultConfig);
File.WriteAllText("config.json", cj);
}
public static IConfig LoadConfig()
{
try
{
var cr = File.ReadAllText("config.json");
var c = JsonSerializer.Deserialize(cr);
if (c == null)
{
Utilities.WriteDefaultConfig();
return Constants.DefaultConfig;
}
else return c;
}
catch (FileNotFoundException)
{
Utilities.WriteDefaultConfig();
return Constants.DefaultConfig;
}
}
}
}