using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
using LogType = EscapeRoomEngine.Engine.Runtime.Utilities.LogType;

namespace EscapeRoomEngine.Engine.Runtime.Requirements
{
    /// <summary>
    /// The main class any precondition requirement inherits from. To place a module, each precondition requirement must return the true-set.
    /// </summary>
    public abstract class PreconditionRequirement : Requirement<bool>
    {
        private static readonly List<bool> TrueSet = new(new[] { true });

        protected abstract override List<bool> FilterCandidates(List<bool> candidates, Module module, Space space);

        public static bool CheckPreconditions(Module module, Space space)
        {
            if (module.description.preconditionRequirements.Count == 0)
            {
                // don't evaluate requirements if there are none
                return true;
            }
            
            var preconditionsMet = Candidates(
                    TrueSet,
                    module.description.preconditionRequirements,
                    module, space)
                .Contains(true);

            Logger.Log(
                preconditionsMet
                    ? $"Preconditions for {module} satisfied"
                    : $"Could not satisfy preconditions for {module}",
                LogType.RequirementResolution);

            return preconditionsMet;
        }
    }
}