using System.Runtime.InteropServices;

namespace DodoSSH.Client.Ssh;

/// <summary>
/// A pseudo-terminal's dimensions.
/// </summary>
/// <remarks>
/// Both the character grid and the pixel extent, because the SSH <c>pty-req</c> and
/// <c>window-change</c> requests carry both. Pixel dimensions are what let a remote program draw
/// sixel graphics or size an image correctly; sending zeroes is legal and tells the remote there is
/// no pixel information, which is not the same as telling it the terminal is zero pixels wide.
/// </remarks>
/// <param name="Columns">Character columns.</param>
/// <param name="Rows">Character rows.</param>
/// <param name="PixelWidth">Width in pixels, or 0 when unknown.</param>
/// <param name="PixelHeight">Height in pixels, or 0 when unknown.</param>
[StructLayout(LayoutKind.Auto)]
public readonly record struct TerminalSize(
    ushort Columns,
    ushort Rows,
    ushort PixelWidth = 0,
    ushort PixelHeight = 0)
{
    /// <summary>The conventional default, for a session opened before the UI has measured itself.</summary>
    public static TerminalSize Default => new(80, 24);

    /// <summary>Whether the size is usable as a terminal.</summary>
    /// <remarks>
    /// A zero dimension is worth rejecting rather than forwarding: a resize to 0×0 arrives naturally
    /// when a pane is collapsed or a window minimised, and passing it on makes the remote's idea of
    /// the terminal nonsensical until the next resize.
    /// </remarks>
    public bool IsUsable => Columns > 0 && Rows > 0;
}
