using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Linq.Expressions;
using Wierszowki.Core.Interfaces;

namespace Wierszowki.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 LinqRepository<T> : IRepository<T>
      where T : class, IIdentifiable
    {
        /// <summary>
        /// Gets the active LINQ DataContext which manages access to the persistance store
        /// </summary>
        private readonly WierszowkiDataContext _dataContext = new WierszowkiDataContext();

        /// <summary>
        /// Returns a count of the items in the repository
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            return _dataContext.GetTable<T>().Count();
        }

        /// <summary>
        /// Returns a count of the items in the repository which match the supplied lambda
        /// </summary>
        /// <param name="expression">A lambda expression which will be evaluated against the repository</param>
        /// <returns></returns>
        public int Count(Expression<Func<T, bool>> expression)
        {
            return _dataContext.GetTable<T>().Count(expression);
        }

        /// <summary>
        /// Finds a single instance of an entity matching a unique identifier.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T FindOne(int id)
        {
            return FindOne(t => t.Id == id);
        }

        /// <summary>
        /// Finds a single instance of an entity matching a lambda expression
        /// </summary>
        /// <param name="expression"></param>
        /// <returns>
        /// The first entity which matches the expession
        /// </returns>
        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);
        }

        /// <summary>
        /// Checks if an entity exists matching a lambda expression
        /// </summary>
        /// <param name="expression"></param>
        /// <returns>
        /// True if an entity exists, otherwise False
        /// </returns>
        public bool Exists(Expression<Func<T, bool>> expression)
        {
            var entity = FindOne(expression);

            return (entity != null);
        }


        /// <summary>
        /// Finds all entities in the repository
        /// </summary>
        /// <returns></returns>
        public IList<T> FindAll()
        {
            return _dataContext.GetTable<T>().ToList();
        }

        /// <summary>
        /// Finds all entities matching a lambda expression
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public IList<T> FindAll(Expression<Func<T, bool>> expression)
        {
            return _dataContext.GetTable<T>().Where(expression).ToList();
        }

        /// <summary>
        /// Adds a new entity in to the repository
        /// </summary>
        /// <param name="entity"></param>
        public void Insert(T entity)
        {
            //DataContext.GetTable<T>().InsertOnSubmit(entity);
            //DataContext.SubmitChanges();

            //using (DataContext dc = new WierszowkiDataContext())
            //{
                _dataContext.GetTable<T>().InsertOnSubmit(entity);
                _dataContext.SubmitChanges();
            //}
        }

        /// <summary>
        /// Removes the specified entity from the repository
        /// </summary>
        /// <param name="entity"></param>
        public void Delete(T entity)
        {
            using (var dc = new WierszowkiDataContext())
            {
                dc.GetTable<T>().Attach(entity);
                dc.GetTable<T>().DeleteOnSubmit(entity);
                dc.SubmitChanges();
            }
        }

        /// <summary>
        /// Saves the specified entity back to the repository
        /// </summary>
        /// <param name="entity"></param>
        public void Update(T entity)
        {
            _dataContext.SubmitChanges();
        }

        /// <summary>
        /// Returns a requeryable set of all entities in the repository matching a unique identifier
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<T> Find(int id)
        {
            return _dataContext.GetTable<T>().Where(t => t.Id == id);
        }

        /// <summary>
        /// Returns a requeryable set of all entities in the repository matching a lambda expression
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public IQueryable<T> Find(Expression<Func<T, bool>> expression)
        {
            return _dataContext.GetTable<T>().Where(expression);        }

        /// <summary>
        /// Returns a requeryable set of all entities in the repository
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> Find()
        {
            return _dataContext.GetTable<T>();
        }
    }
}