﻿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 sealed class Repository<T> : IRepository<T>
      where T : class, IIdentifiable
    {
        private readonly PlatnosciDataContext _dataContext = new PlatnosciDataContext();

        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_faktury == 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.GetTable<T>().InsertOnSubmit(entity);
            //DataContext.SubmitChanges();

            //using (DataContext dc = new WierszowkiDataContext())
            //{
                _dataContext.GetTable<T>().InsertOnSubmit(entity);
                _dataContext.SubmitChanges();
            //}
        }
        public void Delete(T entity)
        {
            if (entity != null)
            {
                _dataContext.GetTable<T>().DeleteOnSubmit(entity);
                _dataContext.SubmitChanges();
            }
        }
        public void Update(T entity)
        {
            _dataContext.SubmitChanges();
        }
        public IQueryable<T> Find(int id)
        {
            return _dataContext.GetTable<T>().Where(t => t.ID_faktury == id);
        }
        public IQueryable<T> Find(Expression<Func<T, bool>> expression)
        {
            return _dataContext.GetTable<T>().Where(expression);        
        }
        public IQueryable<T> Find()
        {
            return _dataContext.GetTable<T>();
        }       
    }
}
