using System;
namespace DevilLink2.Gameplay
{
/// Scores successful matches only. The real-time combo clock keeps advancing while paused.
public sealed class SessionScore
{
public const double ComboWindowSeconds = 4;
private readonly IMonotonicClock clock;
private double lastRead, lastMatch;
private bool finished;
private int comboCount;
public long TotalScore { get; private set; }
public int ComboCount { get { Update(); return comboCount; } }
public int CurrentMultiplier => Math.Min(ComboCount, 5);
public bool IsInCombo => ComboCount > 0;
public double ComboRemainingTime
{
get { Update(); return comboCount == 0 ? 0 : Math.Max(0, ComboWindowSeconds - (lastRead - lastMatch)); }
}
public int ClearBonus { get; private set; }
public event Action MatchScored;
public event Action ClearBonusAwarded;
public event Action ComboChanged;
public SessionScore(IMonotonicClock clock)
{
this.clock = clock ?? throw new ArgumentNullException(nameof(clock));
GameplayTime.Read(clock, ref lastRead);
}
public void Update()
{
double now = GameplayTime.Read(clock, ref lastRead);
if (comboCount > 0 && now - lastMatch > ComboWindowSeconds)
{ comboCount = 0; ComboChanged?.Invoke(); }
}
public int RegisterMatch()
{
if (finished) return 0;
Update();
comboCount++; lastMatch = lastRead;
int points = 100 * Math.Min(comboCount, 5);
TotalScore += points;
ComboChanged?.Invoke(); MatchScored?.Invoke(points);
return points;
}
public int CompleteClear(bool hasTimer, double remainingTime)
{
GameplayTime.Validate(remainingTime, nameof(remainingTime));
if (finished) return 0;
int bonus = checked(1000 + (hasTimer ? checked((int)Math.Ceiling(remainingTime) * 10) : 0));
finished = true; ClearBonus = bonus; TotalScore += bonus;
ClearBonusAwarded?.Invoke(bonus);
return bonus;
}
public void EndWithoutBonus() { finished = true; }
}
}