using System.Text.Json.Serialization; using Moli.Desktop.Diagnostics; using Moli.Desktop.Layout; using Moli.Desktop.Plugins; using Moli.Desktop.Runtime; namespace Moli.Desktop.Api; public sealed record LocalApiStatusSnapshot { public bool IsListening { get; init; } public string Host { get; init; } = "127.0.0.1"; public int Port { get; init; } = 8765; public string? ErrorCode { get; init; } public string? Message { get; init; } } public interface ILocalApiStatus { LocalApiStatusSnapshot GetStatus(); } public sealed record ApiErrorResponse(string ErrorCode, string Message); public sealed record ModeRequest(bool? Enabled); public sealed record RectDto(double X, double Y, double Width, double Height) { public RectD ToRect() => new(X, Y, Width, Height); public static RectDto From(RectD rect) => new(rect.X, rect.Y, rect.Width, rect.Height); } public sealed record WidgetLayoutPatchRequest { public RectDto? Bounds { get; init; } public string? MonitorId { get; init; } public bool? IsVisible { get; init; } public bool? IsLocked { get; init; } public double? Opacity { get; init; } public bool? ClickThrough { get; init; } public double? Scale { get; init; } public DisplayLayer? DisplayLayer { get; init; } public WidgetLayoutPatch ToPatch() => new() { Bounds = Bounds?.ToRect(), MonitorId = string.IsNullOrWhiteSpace(MonitorId) ? null : new MonitorId(MonitorId), IsVisible = IsVisible, IsLocked = IsLocked, Opacity = Opacity, ClickThrough = ClickThrough, Scale = Scale, DisplayLayer = DisplayLayer }; } public sealed record ApiStatusResponse { public bool IsEditMode { get; init; } public bool IsGlobalLocked { get; init; } public bool IsSafeInputMode { get; init; } public LocalApiStatusSnapshot Api { get; init; } = new(); public WallpaperResponse Wallpaper { get; init; } = new(); public IReadOnlyList Plugins { get; init; } = []; public IReadOnlyList Widgets { get; init; } = []; public IReadOnlyList Diagnostics { get; init; } = []; public static ApiStatusResponse From(AppRuntimeState state, LocalApiStatusSnapshot api) => new() { IsEditMode = state.IsEditMode, IsGlobalLocked = state.IsGlobalLocked, IsSafeInputMode = state.IsSafeInputMode, Api = api, Wallpaper = WallpaperResponse.From(state.Wallpaper), Plugins = [.. state.Plugins.Select(PluginResponse.From)], Widgets = [.. state.Widgets.Select(WidgetResponse.From)], Diagnostics = state.Diagnostics }; } public sealed record WallpaperResponse { public bool IsEnabled { get; init; } public bool IsPaused { get; init; } public bool IsMuted { get; init; } public double Opacity { get; init; } public bool ClickThrough { get; init; } public string? PluginId { get; init; } public static WallpaperResponse From(WallpaperRuntimeState state) => new() { IsEnabled = state.IsEnabled, IsPaused = state.IsPaused, IsMuted = state.IsMuted, Opacity = state.Opacity, ClickThrough = state.ClickThrough, PluginId = state.PluginId?.Value }; } public sealed record PluginResponse { public required string Id { get; init; } public required string Name { get; init; } [JsonConverter(typeof(JsonStringEnumConverter))] public PluginType Type { get; init; } [JsonConverter(typeof(JsonStringEnumConverter))] public PluginState State { get; init; } public string? ErrorCode { get; init; } public string Entry { get; init; } = ""; public static PluginResponse From(PluginRuntimeState state) => new() { Id = state.Id.Value, Name = state.Name, Type = state.Type, State = state.State, ErrorCode = state.ErrorCode, Entry = state.Entry ?? "" }; } public sealed record WidgetResponse { public required string InstanceId { get; init; } public required string PluginId { get; init; } public required RectDto Bounds { get; init; } public required string MonitorId { get; init; } public bool IsVisible { get; init; } public bool IsLocked { get; init; } public double Opacity { get; init; } public bool ClickThrough { get; init; } [JsonConverter(typeof(JsonStringEnumConverter))] public DisplayLayer DisplayLayer { get; init; } public static WidgetResponse From(WidgetRuntimeState state) => new() { InstanceId = state.InstanceId.Value, PluginId = state.PluginId.Value, Bounds = RectDto.From(state.Bounds), MonitorId = state.MonitorId.Value, IsVisible = state.IsVisible, IsLocked = state.IsLocked, Opacity = state.Opacity, ClickThrough = state.ClickThrough, DisplayLayer = state.DisplayLayer }; } public sealed record RuntimeEventResponse(string Name, DateTimeOffset Timestamp, object? Payload) { public static RuntimeEventResponse From(RuntimeEvent runtimeEvent) => new(runtimeEvent.Name, runtimeEvent.Timestamp, runtimeEvent.Payload); }