using System;
namespace TuneBoss.Core.Models
{
///
/// Display scaling for 1D tables: displayed value = raw × multiplier.
/// A multiplier of 0 or 1 means unscaled (×1). Editing is done in displayed units
/// and must land on a multiple of the multiplier.
///
/// This is the single source of truth for the rules the 1D view-model applies
/// (OneDimTablesViewModel can delegate to it so the UI and tests can't drift).
///
public static class OneDimDisplay
{
/// 0/1 => 1; otherwise the multiplier itself.
public static int EffectiveMultiplier(int multiplier) => multiplier < 1 ? 1 : multiplier;
/// Raw byte value → displayed value.
public static long ToDisplay(int raw, int multiplier) =>
(long)raw * EffectiveMultiplier(multiplier);
///
/// Displayed value → raw. Returns false if the displayed value is not an exact
/// multiple of the (effective) multiplier, so the caller can reject the edit.
///
public static bool TryFromDisplay(double display, int multiplier, out int raw)
{
int m = EffectiveMultiplier(multiplier);
double q = display / m;
if (Math.Abs(q - Math.Round(q)) > 1e-6)
{
raw = 0;
return false;
}
raw = (int)Math.Round(q);
return true;
}
}
}