using StandardScene.Fass2Simulator;
using Xunit;

namespace StandardScene.Magnetic.Tests.Tasking
{
    public class Fass2SimControlStopTests
    {
        [Fact]
        public void ControlStop_StaysUntilReleasePatch()
        {
            var config = new Fass2SimConfig
            {
                InitialNode = 1,
                DefaultSegmentDistance = 200,
                DefaultSpeed = 1000,
                AutoContinueOnPass = true,
                AutoCompleteStationActions = false,
                ActionDelayMs = 0
            };
            var (vehicle, motion, actions) = Fass2SimBootstrap.CreateTestStack(config);
            vehicle.State = 1;

            motion.OnNodesCommand(3001, new[]
            {
                new Fass2SimNodeMessage { Node = 1, StartStop = 1, Distance = 200 },
                new Fass2SimNodeMessage { Node = 2, StartStop = 12 }
            });

            for (var i = 0; i < 20 && vehicle.Node != 2; i++)
            {
                motion.Tick(50);
                actions.Tick(50);
            }

            Assert.Equal((ushort)2, vehicle.Node);
            Assert.Equal((byte)2, vehicle.State);
            Assert.Equal((byte)12, vehicle.StartStop);
            Assert.False(actions.CanLeaveStation());

            motion.OnNodesCommand(3001, new[]
            {
                new Fass2SimNodeMessage { Node = 2, StartStop = 11, Distance = 200 },
                new Fass2SimNodeMessage { Node = 3, StartStop = 1 }
            });
            actions.OnActionCommand(9, new Fass2SimNodeMessage
            {
                Node = 2,
                StartStop = 11
            });
            actions.Tick(10);
            motion.OnStationActionsCompleted();

            for (var i = 0; i < 30 && vehicle.Node != 3; i++)
            {
                motion.Tick(50);
                actions.Tick(50);
            }

            Assert.Equal((ushort)3, vehicle.Node);
        }
    }
}
