using SoulstormReplayReader.Core.Domain.Action;
using SoulstormReplayReader.Core.Enums;
using SoulstormReplayReader.Core.Utils;

namespace SoulstormReplayReader.Core.Domain.BugCheckers;

public sealed class FastT2Checker(int playerId, RaceEnum playerRace) : BugCheckerBase(playerId, playerRace)
{
    public override BugType BugType => BugType.FastT2;

    private byte _gensBuilt = 0;
    private bool _isAspectPortalBuilt = false;
    private bool _isSoulShrineBuilt = false;
    private bool _isListenPostBuilt = false;
    private bool _isMachineryBuilt = false;

    public override void Check(IGameAction action)
    {
        if (IsCompleted)
            return;

        if (PlayerId != action.PlayerId)
            return;

        if (action.Cmd != 117)
            return;

        switch (action.Arg)
        {
            case 24:
                _gensBuilt++;
                break;
            case 14:
                _isListenPostBuilt = true;
                break;
            case 7:
                _isAspectPortalBuilt = true;
                break;
            case 16:
                _isSoulShrineBuilt = true;
                break;
            case 22:
                _isMachineryBuilt = true;
                break;
        }

        if (_isMachineryBuilt)
        {
            if (action.Tick <= (2 * 60 + 30) * 8)
            {
                IsCompleted = true;
                Accusation = "FastT2: machinery build started in under 2:30 minutes";
            }
            else if (_gensBuilt >= 3 && _isSoulShrineBuilt && !_isAspectPortalBuilt)
            {
                IsCompleted = true;
                var time = TicksFormatter.Format(action.Tick, TicksFormatter.Pattern.Hms);
                Accusation = $"FastT2: machinery build started in {time} but there is no aspect portal";
                if (!_isListenPostBuilt)
                    Accusation += " and listening posts";
            }
        }
    }
}