using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using adMoto.Payments.Core.Interfaces;

namespace adMoto.Payments.Core.Data
{
    public class DataContext : IDataContext
    {
        private readonly PlatnosciDataContext _dataContext;
        private readonly List<object> _listaPlatnosci = new List<object>();

        public DataContext()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["BazaReklamConnectionString"];
            if (connectionString == null)
                throw new ArgumentNullException("BazaReklamConnectionString");
            _dataContext = new PlatnosciDataContext(connectionString.ConnectionString);
        }

        public IQueryable<T> GetTable<T>() where T : class
        {
            return _dataContext.GetTable<T>();
        }

        public void Insert<T>(T item) where T : class
        {
            _dataContext.GetTable<T>().InsertOnSubmit(item);
            _dataContext.SubmitChanges();
        }

        public void Delete<T>(T item) where T : class
        {
            if (item == null) return;

            _dataContext.GetTable<T>().DeleteOnSubmit(item);
            _dataContext.SubmitChanges();
        }

        public void SubmitChanges()
        {
            _dataContext.SubmitChanges();
        }

        public IQueryable<Invoice> FindInvoiceByNipNumber(string nip, string numer)
        {
            var query = from i in _dataContext.Invoices
                        where (i.nip == nip && i.Faktura_Numer == numer)
                        select i;
            return query;
        }

        public IQueryable<Invoice> FindInvoiceById(int id)
        {
            var query = from i in _dataContext.Invoices
                        where i.ID_faktury == id
                        select i;
            return query;
        }

        public List<PotwierdzeniaEcard> FindItemsByIdFaktury(int idFaktury)
        {
            var query = _dataContext.PlatnosciEcards
                .Where(vp => vp.IDFaktury == idFaktury && vp.Status == true)
                .OrderByDescending(vp => vp.IDFaktury);

            query.ToList();

            var tablica = new List<PotwierdzeniaEcard>();

            foreach (var pt in query)
            {
                var ecard = pt;
                var query2 = _dataContext.PotwierdzeniaEcards
                    .Where(ps => ps.ORDERNUMBER == ecard.ORDERNUMBER && ps.CURRENTSTATE == "payment_deposited")
                    .OrderBy(ps => ps.id);

                for (var i = 0; i < query2.ToList().Count; i++)
                {
                    tablica.Add(query2.ToList()[i]);
                }
            }

            return tablica;
        }

        public List<object> Getlista()
        {
            return _listaPlatnosci;
        }

        public IQueryable<PlatnosciEcard> FindPaymentByOrdernumber(int ordernumber)
        {
            var query = from l in _dataContext.PlatnosciEcards
                        where l.ORDERNUMBER == ordernumber
                        select l;

            return query;
        }

        public int GetOrdernumber(string description, int? idfaktury, DateTime? data)
        {
            var ordernumber = 0;

            var query = from l in _dataContext.PlatnosciEcards
                        where l.ORDERDESCRIPTION == description && l.IDFaktury == idfaktury && l.Data == data
                        select l;

            var pl = query.ToList();
            
            if (pl.Count > 0 && pl[0].ORDERNUMBER > 0) ordernumber = pl[0].ORDERNUMBER;
            
            return ordernumber;
        }
    }
}