using System.Linq;

namespace Sevomin.Models.Repositories
{
    public class ParameterRepository: IParameterRepository
    {
        
        #region Singleton
        private static ParameterRepository member;
        private static object locker = new object();

        private ParameterRepository()
        {
        }

        static ParameterRepository()
        {
              lock (locker)
              {
                  ParameterRepository.member = new ParameterRepository();
              }
        }
        public static ParameterRepository Current
        {
            get
            {
                return ParameterRepository.member;
            }

        }
        #endregion

        #region JobRepository

        public IQueryable<Parameter> ListAll()
        {
            return SevominDbContext.Current.Parameters;
        }

        public Parameter Find(long key)
        {
            return ListAll().SingleOrDefault(d => d.Id == key);
        }

        public void Delete(Parameter d)
        {
            SevominDbContext.Current.Parameters.Remove(d);
            Save();
        }

        public void Add(Parameter d)
        {
            SevominDbContext.Current.Parameters.Add(d);
            Save();
        }

        public void Save()
        {
            SevominDbContext.Current.SaveChanges();
        }

        public void AddParametersToDovomin(Dovomin dovomin)
        {
            foreach (var param in ListAll().ToList())
            {
                if (dovomin.DovominParameters.Count(x => x.ParameterId == param.Id) == 0)
                {
                    dovomin.DovominParameters.Add(new DovominParameter(dovomin, param));
                }
            }
        }
        #endregion

    }
}
