﻿using System.Collections.Generic;
using System.Linq;

namespace ForexRunner.Strategies
{
    public class OrExit : IExitStrategy
    {
        public readonly IEnumerable<IExitStrategy> Strategies;
        public OrExit(IEnumerable<IExitStrategy> strategies)
        {
            Strategies = strategies;
        }

        public OrExit(params IExitStrategy[] strategies) : this((IEnumerable<IExitStrategy>)strategies) { }

        public ExitRequest ShouldExit(Position position, Tick currentTick)
        {
            return Strategies
                .Select(s => s.ShouldExit(position, currentTick))
                .Where(e => e != null)
                .FirstOrDefault();
        }

        public static OrExit<T1, T2> Create<T1, T2>(T1 strategy1, T2 strategy2)
            where T1 : IExitStrategy
            where T2 : IExitStrategy
        {
            return new OrExit<T1, T2>(strategy1, strategy2);
        }
    }

    public class OrExit<T1, T2> : IExitStrategy
        where T1 : IExitStrategy
        where T2 : IExitStrategy
    {
        private readonly T1 _strategy1;
        private readonly T2 _strategy2;

        public OrExit(T1 strategy1, T2 strategy2)
        {
            _strategy1 = strategy1;
            _strategy2 = strategy2;
        }

        public ExitRequest ShouldExit(Position position, Tick currentTick)
        {
            return _strategy1.ShouldExit(position, currentTick) ?? _strategy2.ShouldExit(position, currentTick);
        }
    }
}
