|
| 1 | +using Discord; |
| 2 | +using RustPlusBot.Domain.Switches; |
| 3 | + |
| 4 | +namespace RustPlusBot.Features.Switches.Rendering; |
| 5 | + |
| 6 | +/// <summary>Renders a Smart Switch as a Discord embed + control row, and the pairing-prompt embed + row. Pure.</summary> |
| 7 | +/// <param name="localizer">The switch localizer.</param> |
| 8 | +internal sealed class SwitchEmbedRenderer(ISwitchLocalizer localizer) |
| 9 | +{ |
| 10 | + /// <summary>Renders the switch embed and its control buttons. <paramref name="isActive"/> null ⇒ unreachable.</summary> |
| 11 | + /// <param name="sw">The switch.</param> |
| 12 | + /// <param name="isActive">On (true), off (false), or unreachable (null).</param> |
| 13 | + /// <param name="culture">The guild culture.</param> |
| 14 | + /// <returns>The embed and the component rows.</returns> |
| 15 | + public (Embed Embed, MessageComponent Components) RenderSwitch(SmartSwitch sw, bool? isActive, string culture) |
| 16 | + { |
| 17 | + ArgumentNullException.ThrowIfNull(sw); |
| 18 | + var unreachable = isActive is null; |
| 19 | + var statusKey = isActive switch |
| 20 | + { |
| 21 | + true => "switch.status.on", |
| 22 | + false => "switch.status.off", |
| 23 | + null => "switch.status.unreachable", |
| 24 | + }; |
| 25 | + |
| 26 | + var embed = new EmbedBuilder() |
| 27 | + .WithTitle(sw.Name) |
| 28 | + .WithDescription(localizer.Get(statusKey, culture)) |
| 29 | + .WithFooter(localizer.Get("switch.embed.footer", culture, sw.EntityId)) |
| 30 | + .Build(); |
| 31 | + |
| 32 | + var tail = $"{sw.ServerId}:{sw.EntityId}"; |
| 33 | + var components = new ComponentBuilder() |
| 34 | + .WithButton(localizer.Get("switch.button.on", culture), SwitchComponentIds.OnPrefix + tail, |
| 35 | + ButtonStyle.Success, disabled: unreachable || isActive == true) |
| 36 | + .WithButton(localizer.Get("switch.button.off", culture), SwitchComponentIds.OffPrefix + tail, |
| 37 | + ButtonStyle.Secondary, disabled: unreachable || isActive == false) |
| 38 | + .WithButton(localizer.Get("switch.button.strobe", culture), SwitchComponentIds.StrobePrefix + tail, |
| 39 | + ButtonStyle.Primary, disabled: unreachable) |
| 40 | + .WithButton(localizer.Get("switch.button.rename", culture), SwitchComponentIds.RenamePrefix + tail, |
| 41 | + ButtonStyle.Secondary, disabled: unreachable) |
| 42 | + .Build(); |
| 43 | + |
| 44 | + return (embed, components); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary>Renders the transient "New switch detected — Add it?" prompt.</summary> |
| 48 | + /// <param name="serverId">The server id.</param> |
| 49 | + /// <param name="entityId">The entity id.</param> |
| 50 | + /// <param name="defaultName">The generated default name.</param> |
| 51 | + /// <param name="culture">The guild culture.</param> |
| 52 | + /// <returns>The prompt embed and Accept/Dismiss row.</returns> |
| 53 | + public (Embed Embed, MessageComponent Components) RenderPrompt( |
| 54 | + Guid serverId, ulong entityId, string defaultName, string culture) |
| 55 | + { |
| 56 | + var embed = new EmbedBuilder() |
| 57 | + .WithTitle(localizer.Get("switch.prompt.title", culture)) |
| 58 | + .WithDescription(localizer.Get("switch.prompt.body", culture, defaultName)) |
| 59 | + .Build(); |
| 60 | + |
| 61 | + var tail = $"{serverId}:{entityId}"; |
| 62 | + var components = new ComponentBuilder() |
| 63 | + .WithButton(localizer.Get("switch.prompt.accept", culture), SwitchComponentIds.AcceptPrefix + tail, |
| 64 | + ButtonStyle.Success) |
| 65 | + .WithButton(localizer.Get("switch.prompt.dismiss", culture), SwitchComponentIds.DismissPrefix + tail, |
| 66 | + ButtonStyle.Secondary) |
| 67 | + .Build(); |
| 68 | + |
| 69 | + return (embed, components); |
| 70 | + } |
| 71 | +} |
0 commit comments