namespace TuneBoss.Core.Models; /// /// What the app should do after the post-unlock server status check. /// Mirrors the Flutter checkEmailStatus gate (doneAfterConnect): /// Pass → continue into the app, /// Warn → continue, but show a warning first, /// Block → show the message, disconnect, do NOT load tables. /// public enum EcuStatusOutcome { Pass, Warn, Block, } /// /// Result of the post-unlock ECU status check against the TracBoss account server. /// public sealed record EcuStatusResult( EcuStatusOutcome Outcome, string Message, int? SuccessCode, bool ServerReachable) { public bool IsBlocked => Outcome == EcuStatusOutcome.Block; } /// /// Pure interpretation of the server's success code into an app outcome. /// No HTTP or UI here so it can be unit-tested and reused. /// /// Ported from the Flutter app's checkEmailStatus (lib/services/tb_interactor.dart), /// MINUS the ECU activation-bit (reg1Reg2) second chance and the global payment/systemSync /// side effects — those are a later phase. Where the Flutter code relied on reg1Reg2 to /// rescue a "not activated" verdict, this build hard-blocks instead. /// public static class EcuStatusInterpreter { /// FW below this is a legacy unit that predates activation — always allowed. public const int LegacyFwCeiling = 150; /// /// Interpret a definitive server response. Mirrors the legacy Android app's /// CheckEmailStatus success-code handling, including the ECU activation-bit /// () rescue on the "not registered" verdicts (5 and 0). /// /// The server's success status code. /// This unit's firmware version (for the legacy-ECU branch). /// ECU self-reported activation (Reg1_Reg2 > 0). /// Optional human message echoed from the server. public static EcuStatusResult Interpret(int success, byte fwVersion, bool isActivated, string? serverMessage = null) { string msg = string.IsNullOrWhiteSpace(serverMessage) ? "" : serverMessage!.Trim(); switch (success) { // Registered / global / dealer-bypass — full access. case 1: case 2: case 3: case 8: return new EcuStatusResult( EcuStatusOutcome.Pass, success == 8 ? "Dealer-bypassed ECU — access granted." : "ECU registered — access granted.", success, ServerReachable: true); // Activated but not warranty-registered — allowed, with a warning. case 6: case 7: return new EcuStatusResult( EcuStatusOutcome.Warn, "This ECU is activated but not registered for warranty.", success, ServerReachable: true); // Phone not registered. Legacy: FW < 150 → pass; else if activated → pass with a // "password change disabled" warning; else block. case 5: if (fwVersion < LegacyFwCeiling) return new EcuStatusResult(EcuStatusOutcome.Pass, "Legacy ECU — access granted.", success, ServerReachable: true); return isActivated ? new EcuStatusResult(EcuStatusOutcome.Warn, "This ECU is activated but not registered for warranty. " + "Password change is disabled.", success, ServerReachable: true) : NotActivated(success); // Unregistered. Legacy: FW < 150 → pass; else if activated → pass; else block. case 0: if (fwVersion < LegacyFwCeiling) return new EcuStatusResult(EcuStatusOutcome.Pass, "Legacy ECU — access granted.", success, ServerReachable: true); return isActivated ? new EcuStatusResult(EcuStatusOutcome.Pass, "ECU activated — access granted.", success, ServerReachable: true) : NotActivated(success); // Account removed — warranty void, but access is allowed. case 9: return new EcuStatusResult( EcuStatusOutcome.Warn, "The account for this ECU has been removed — warranty is void.", success, ServerReachable: true); // App out of date (server rejected the request shape). case 98: return new EcuStatusResult( EcuStatusOutcome.Block, "Missing parameter. Please update TuneBoss PC to the latest version.", success, ServerReachable: true); default: return new EcuStatusResult( EcuStatusOutcome.Block, $"Unrecognised ECU status from server (code {success})" + (msg.Length > 0 ? $": {msg}" : "."), success, ServerReachable: true); } } /// /// Outcome when the server can't be reached (offline, timeout, or a transport/parse error). /// Mirrors the legacy Android offline / server-error fallback exactly: FW < 150 passes, /// an activated ECU () passes, and only a genuinely /// unactivated modern unit is blocked. This is why an activated unit still works offline. /// public static EcuStatusResult Unreachable(byte fwVersion, bool isActivated, string? reason = null) { if (fwVersion < LegacyFwCeiling) return new EcuStatusResult(EcuStatusOutcome.Pass, "Legacy ECU — access granted (server not contacted).", SuccessCode: null, ServerReachable: false); if (isActivated) return new EcuStatusResult(EcuStatusOutcome.Pass, "ECU activated — access granted (server not contacted).", SuccessCode: null, ServerReachable: false); string why = string.IsNullOrWhiteSpace(reason) ? "" : $" ({reason})"; return new EcuStatusResult(EcuStatusOutcome.Block, "This ECU has not been activated, and the activation server could not be reached " + "to verify it. Please activate the ECU with your dealer." + why, SuccessCode: null, ServerReachable: false); } private static EcuStatusResult NotActivated(int success) => new( EcuStatusOutcome.Block, "This ECU has not been activated. Please activate it with your dealer.", success, ServerReachable: true); }