using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace TuneBoss.Core.Models;
///
/// Which bike models have VVA (variable valve actuation), so the RPM Settings page can
/// gate the VVA RPM Trigger. Mirrors the legacy vvaModels.xml / checkVvaModels.
///
/// The list is loaded from an EDITABLE external file at
/// %APPDATA%\TuneBoss\vvaModels.xml so it can be changed without recompiling.
/// The file is auto-created (seeded with the defaults below) on first use if missing.
/// If it can't be read, the built-in defaults are used.
///
/// XML schema (same as legacy): <vvaModels><ym>14</ym>…<hn>…</hn>
/// <su>…</su><kw>…</kw></vvaModels>.
/// Element → manufacturer code: ym=1 Yamaha, hn=2 Honda, kw=3 Kawasaki, su=6 Suzuki.
///
public static class VvaModelRegistry
{
private static readonly object _lock = new();
private static Dictionary>? _map;
/// Editable list file. Sits beside settings.json in %APPDATA%\TuneBoss.
public static string FilePath => System.IO.Path.Combine(
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData),
"TuneBoss", "vvaModels.xml");
// Element name → manufacturer code.
private static readonly (string Elem, byte Mfr)[] Makes =
{
("ym", 1), ("hn", 2), ("kw", 3), ("su", 6),
};
// Built-in defaults (legacy vvaModels.xml): only these Yamaha models carry VVA.
private static Dictionary> Defaults() => new()
{
[1] = new HashSet { 14, 17, 21, 22 },
};
/// True when the given make/model is known to have VVA.
public static bool IsVvaModel(byte manufacturer, byte model)
{
var map = EnsureLoaded();
return map.TryGetValue(manufacturer, out var models) && models.Contains(model);
}
/// Force a re-read of the file on the next lookup (call after editing it).
public static void Reload()
{
lock (_lock) { _map = null; }
}
private static Dictionary> EnsureLoaded()
{
lock (_lock)
{
if (_map != null) return _map;
try
{
if (System.IO.File.Exists(FilePath))
{
_map = Parse(XDocument.Load(FilePath));
return _map;
}
// First run — seed the editable file with the defaults, then use them.
_map = Defaults();
TrySeedFile();
}
catch
{
// Missing / malformed / unreadable → fall back to the built-in list.
_map = Defaults();
}
return _map;
}
}
private static Dictionary> Parse(XDocument doc)
{
var map = new Dictionary>();
foreach (var (elem, mfr) in Makes)
{
var set = new HashSet();
foreach (var node in doc.Descendants(elem))
{
if (byte.TryParse(node.Value.Trim(), out byte m))
set.Add(m); // a 999 placeholder simply never matches a real model
}
if (set.Count > 0) map[mfr] = set;
}
return map.Count > 0 ? map : Defaults();
}
private static void TrySeedFile()
{
try
{
var dir = System.IO.Path.GetDirectoryName(FilePath);
if (!string.IsNullOrEmpty(dir)) System.IO.Directory.CreateDirectory(dir);
var doc = new XDocument(new XElement("vvaModels",
new XElement("ym", 14), new XElement("ym", 17),
new XElement("ym", 21), new XElement("ym", 22),
new XElement("hn", 999), new XElement("su", 999), new XElement("kw", 999)));
doc.Save(FilePath);
}
catch { /* seeding is best-effort; defaults still apply in memory */ }
}
}