using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
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);
        }
    }
}
