using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Xml.Linq;
using Xunit;
namespace ytLive.Tests;
/// TASK 61 — the creator (2026-10-21): *"the Y and H boxes under webcam
/// properties should be the same width as the X and W boxes and those widths should be
/// fixed to accommodate the max number of possible digits providing the geometry
/// values."*
///
/// One test: the four geometry fields carry the SAME explicit width (the Y/H
/// fields used to stretch to fill the row, because they were the last child of a
/// DockPanel, while X/W sized themselves to their content), and that width really fits
/// the widest value the panel can show — the largest geometry value (1920 master
/// pixels) with a sign, measured in the app's own font at the field's size.
public class GeometryFieldWidthTests
{
/// The widest value a geometry field can show: the master frame is
/// 1920×1080, so four digits — plus a sign the creator may be typing.
private const string WidestValue = "-1920";
[Fact]
public void The_webcam_geometry_fields_share_one_fixed_width_that_fits_the_largest_value()
{
var doc = XDocument.Load(SourceAssert.RepoPath("Controls", "LeftPanel.xaml"));
var geometry = doc.Descendants()
.Where(e => e.Name.LocalName == "TextBlock" && (string?)e.Attribute("Text") == "Geometry")
.Single(e => ((string?)e.Parent?.Attribute("Visibility") ?? "").Contains("SelectedElement.IsDraggable"));
var rows = geometry.Parent!.Elements().Where(e => e.Name.LocalName == "DockPanel").ToList();
Assert.Equal(2, rows.Count);
var fields = rows.SelectMany(r => r.Elements().Where(e => e.Name.LocalName == "TextBox")).ToList();
Assert.Equal(4, fields.Count);
// Every field carries the SAME explicit width — a field without one sizes
// itself to its content, and the last one in the row stretches to fill it.
var widths = fields.Select(f => (string?)f.Attribute("Width")).ToList();
Assert.All(widths, w => Assert.False(string.IsNullOrWhiteSpace(w),
"a geometry field has no fixed width — it sizes itself to its content"));
Assert.Single(widths.Distinct());
// And that width fits the widest value the panel can show, in the app's font
// at the field's size, plus the field's own padding and border.
var width = double.Parse(widths[0]!, CultureInfo.InvariantCulture);
var needed = MeasureWidestValue() + 8 /* YtTextBoxCompact padding */ + 2 /* border */;
Assert.True(width >= needed,
$"the geometry fields are {width:0}px wide; {needed:0.#}px are needed for '{WidestValue}'");
}
/// The width of the widest possible value, in the app's font at 11px.
private static double MeasureWidestValue()
{
var typeface = new Typeface(SystemFonts.MessageFontFamily,
FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
return new FormattedText(WidestValue, CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, typeface, 11, Brushes.Black, pixelsPerDip: 1.0).Width;
}
}