using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Platnosci.Core.Interface;


namespace Platnosci.Core.Linq
{
    /// <summary>
    /// An implementation of the Repository which uses LINQ to SQL Server to persist its data
    /// </summary>
    /// <typeparam name="T">The generic type representing the entity we are dealing with</typeparam>
    /// <remarks>We use a 1 to 1 mapping of entity -> SQL table</remarks>
    public class Repository<T> : IRepository<T> where T : class, IIdentifiable
    {
        protected readonly IDataContext DataContext;

        public Repository(IDataContext dataContext)
        {
            DataContext = dataContext;
        }  
        public int Count()
        {
            return DataContext.GetTable<T>().Count();
        }
        public int Count(Expression<Func<T, bool>> expression)
        {
            return DataContext.GetTable<T>().Count(expression);
        }
        public T FindOne(int id)
        {
            return FindOne(t => t.Id == id);            
        }        
        public T FindOne(Expression<Func<T, bool>> expression)
        {
            return DataContext.GetTable<T>().Where(expression).SingleOrDefault();
        }

        public bool TryFindOne(Expression<Func<T, bool>> expression, out T entity)
        {
            entity = FindOne(expression);

            return (entity != null);
        }
        public bool Exists(Expression<Func<T, bool>> expression)
        {
            var entity = FindOne(expression);

            return (entity != null);
        }
        public IList<T> FindAll()
        {
            return DataContext.GetTable<T>().ToList();
        }
        public IList<T> FindAll(Expression<Func<T, bool>> expression)
        {
            return DataContext.GetTable<T>().Where(expression).ToList();
        }
        public void Insert(T entity)
	    {
	        DataContext.Insert(entity);            
	    }
        public void Delete(T entity)
        {
            DataContext.Delete(entity);
        }
        public void SubmitChanges()
        {
            DataContext.SubmitChanges();
        }
        public IQueryable<T> Find(int id)
        {
            return DataContext.GetTable<T>().Where(t => t.Id == id);
        }
        public IQueryable<T> Find(Expression<Func<T, bool>> expression)
        {
            return DataContext.GetTable<T>().Where(expression);        
        }
        public IQueryable<T> Find()
        {
            return DataContext.GetTable<T>();
        }
        public IQueryable<vPlatnosciEcard> FindInvoiceByNipNumber(string nip, string numer)
        {
            return DataContext.FindInvoiceByNipNumber(nip, numer);
        }
        public List<PotwierdzeniaEcard> FindItemsByIdFaktury(int idFaktury)
        {
            return DataContext.FindItemsByIdFaktury(idFaktury);
        }
        public int GetOrdernumber(string description, int? idfaktury, DateTime? data)
        {
            return DataContext.GetOrdernumber(description, idfaktury, data);
        }
    }
}
