using System.Collections.Generic;
using System.Linq;
using DatabaseEditor.BL.Filters.Conditions;

namespace DatabaseEditor.BL.Filters
{
    public class Filter<TListModel>
    {
        public Filter()
        {
            Conditions = new List<PropertyCondition<TListModel>>();
        }

        public Filter(List<PropertyCondition<TListModel>> conditions)
        {
            Conditions = conditions.ToList();
        }

        public List<PropertyCondition<TListModel>> Conditions { get; set; }

        public string WherePredicate
        {
            get
            {
                var first = Conditions.FirstOrDefault();
                if (first == null) return "true";

                string predicate = first.Predicate;
                for (int i = 1; i < Conditions.Count; i++)
                {
                    var cond = Conditions[i];
                    predicate = $"({predicate} {cond.LogicOperator} {cond.Predicate})";
                }
                return predicate;
            }
        }
    }
}