using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NT8.Core.Execution;
using NT8.Core.OMS;
using NT8.Core.Tests.Mocks;
using ExecutionTrailingStopConfig = NT8.Core.Execution.TrailingStopConfig;

namespace NT8.Core.Tests.Execution
{
    [TestClass]
    public class TrailingStopManagerFixedTests
    {
        private TrailingStopManager _manager;

        [TestInitialize]
        public void TestInitialize()
        {
            _manager = new TrailingStopManager(new MockLogger<TrailingStopManager>());
        }

        [TestMethod]
        public void CalculateNewStopPrice_FixedTrailing_LongAt5100With8Ticks_Returns5098()
        {
            var position = CreatePosition(OrderSide.Buy, 5000m);
            var config = new ExecutionTrailingStopConfig(StopType.FixedTrailing, 8, 2m, true);

            var stop = _manager.CalculateNewStopPrice(StopType.FixedTrailing, position, 5100m, config);

            Assert.AreEqual(5098.0m, stop);
        }

        [TestMethod]
        public void CalculateNewStopPrice_FixedTrailing_ShortAt5100With8Ticks_Returns5102()
        {
            var position = CreatePosition(OrderSide.Sell, 5000m);
            var config = new ExecutionTrailingStopConfig(StopType.FixedTrailing, 8, 2m, true);

            var stop = _manager.CalculateNewStopPrice(StopType.FixedTrailing, position, 5100m, config);

            Assert.AreEqual(5102.0m, stop);
        }

        private static OrderStatus CreatePosition(OrderSide side, decimal averageFillPrice)
        {
            var position = new OrderStatus();
            position.OrderId = Guid.NewGuid().ToString();
            position.Symbol = "ES";
            position.Side = side;
            position.Quantity = 1;
            position.AverageFillPrice = averageFillPrice;
            position.State = OrderState.Working;
            position.FilledQuantity = 1;
            position.CreatedTime = DateTime.UtcNow;
            return position;
        }
    }
}
