using Theriapolis.Core;
using Theriapolis.Core.Data;
using Theriapolis.Core.Entities;
using Theriapolis.Core.Rules.Character;
using Theriapolis.Core.Rules.Combat;
using Theriapolis.Core.Util;
using Xunit;

namespace Theriapolis.Tests.Combat;

public sealed class EncounterTriggerTests
{
    private readonly ContentResolver _content = new(new ContentLoader(TestHelpers.DataDirectory));

    [Fact]
    public void FindHostileTrigger_ReturnsNullWhenNoHostiles()
    {
        var mgr = new ActorManager();
        mgr.SpawnPlayer(new Vec2(100, 100));
        Assert.Null(EncounterTrigger.FindHostileTrigger(mgr));
    }

    [Fact]
    public void FindHostileTrigger_ReturnsNearbyHostile()
    {
        var mgr = new ActorManager();
        mgr.SpawnPlayer(new Vec2(100, 100));
        var wolf = mgr.SpawnNpc(_content.Npcs.Templates.First(t => t.Id == "wolf"),
            new Vec2(105, 100));
        var hit = EncounterTrigger.FindHostileTrigger(mgr);
        Assert.Same(wolf, hit);
    }

    [Fact]
    public void FindHostileTrigger_IgnoresHostilesPastTriggerRadius()
    {
        var mgr = new ActorManager();
        mgr.SpawnPlayer(new Vec2(100, 100));
        mgr.SpawnNpc(_content.Npcs.Templates.First(t => t.Id == "wolf"),
            new Vec2(100 + C.ENCOUNTER_TRIGGER_TILES + 5, 100));
        Assert.Null(EncounterTrigger.FindHostileTrigger(mgr));
    }

    [Fact]
    public void FindHostileTrigger_IgnoresFriendlyAndNeutral()
    {
        var mgr = new ActorManager();
        mgr.SpawnPlayer(new Vec2(100, 100));
        var merchant = _content.Npcs.Templates.First(t => t.Id == "merchant_traveler");
        mgr.SpawnNpc(merchant, new Vec2(102, 100));
        Assert.Null(EncounterTrigger.FindHostileTrigger(mgr));
    }

    [Fact]
    public void FindInteractCandidate_FindsFriendlyOrNeutralInRange()
    {
        var mgr = new ActorManager();
        mgr.SpawnPlayer(new Vec2(100, 100));
        var merchant = _content.Npcs.Templates.First(t => t.Id == "merchant_traveler");
        var npc = mgr.SpawnNpc(merchant, new Vec2(101, 100));
        var hit = EncounterTrigger.FindInteractCandidate(mgr);
        Assert.Same(npc, hit);
    }

    [Fact]
    public void FindInteractCandidate_IgnoresHostiles()
    {
        var mgr = new ActorManager();
        mgr.SpawnPlayer(new Vec2(100, 100));
        mgr.SpawnNpc(_content.Npcs.Templates.First(t => t.Id == "wolf"), new Vec2(101, 100));
        Assert.Null(EncounterTrigger.FindInteractCandidate(mgr));
    }
}
