using System.IO; using System.Text; using System.Windows.Media; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Size = System.Windows.Size; using Point = System.Windows.Point; namespace Azaion.Annotator.DTO; public class Config { public string VideosDirectory { get; set; } public string LabelsDirectory { get; set; } public string ImagesDirectory { get; set; } public string ResultsDirectory { get; set; } public List AnnotationClasses { get; set; } = []; private Dictionary? _annotationClassesDict; public Dictionary AnnotationClassesDict => _annotationClassesDict ??= AnnotationClasses.ToDictionary(x => x.Id); public Size WindowSize { get; set; } public Point WindowLocation { get; set; } public bool FullScreen { get; set; } public double LeftPanelWidth { get; set; } public double RightPanelWidth { get; set; } public bool ShowHelpOnStart { get; set; } public List VideoFormats { get; set; } public List ImageFormats { get; set; } } public interface IConfigRepository { public Config Get(); public void Save(Config config); } public class FileConfigRepository(ILogger logger) : IConfigRepository { private const string CONFIG_PATH = "config.json"; private const string DEFAULT_VIDEO_DIR = "video"; private const string DEFAULT_LABELS_DIR = "labels"; private const string DEFAULT_IMAGES_DIR = "images"; private const string DEFAULT_RESULTS_DIR = "results"; private static readonly Size DefaultWindowSize = new(1280, 720); private static readonly Point DefaultWindowLocation = new(100, 100); private static readonly List DefaultVideoFormats = ["mp4", "mov", "avi"]; private static readonly List DefaultImageFormats = ["jpg", "jpeg", "png", "bmp"]; public Config Get() { var exePath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)!; var configFilePath = Path.Combine(exePath, CONFIG_PATH); if (!File.Exists(configFilePath)) { return new Config { VideosDirectory = Path.Combine(exePath, DEFAULT_VIDEO_DIR), LabelsDirectory = Path.Combine(exePath, DEFAULT_LABELS_DIR), ImagesDirectory = Path.Combine(exePath, DEFAULT_IMAGES_DIR), ResultsDirectory = Path.Combine(exePath, DEFAULT_RESULTS_DIR), WindowLocation = DefaultWindowLocation, WindowSize = DefaultWindowSize, ShowHelpOnStart = true, VideoFormats = DefaultVideoFormats, ImageFormats = DefaultImageFormats }; } var str = File.ReadAllText(CONFIG_PATH); return JsonConvert.DeserializeObject(str) ?? new Config(); } public void Save(Config config) { File.WriteAllText(CONFIG_PATH, JsonConvert.SerializeObject(config, Formatting.Indented), Encoding.UTF8); } }