using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Platnosci.Core.Interface;
using System.Data.Linq;

namespace Platnosci.Core.Linq
{
    public class DataContext1 : IDataContext
	{
	        private readonly PlatnosciDataContext _dataContext;
            private List<object> listaPlatnosci = new List<object>();
            public DataContext1()
	        {
	            _dataContext = new PlatnosciDataContext();
	        }            
	        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)
                {
                    _dataContext.GetTable<T>().DeleteOnSubmit(item);
                    _dataContext.SubmitChanges();
                }
            }
            public void SubmitChanges()
            {
                _dataContext.SubmitChanges();
            }
            public IQueryable<vPlatnosciEcard> FindInvoiceByNipNumber(string nip, string numer)
            {
                var query = from i in _dataContext.vPlatnosciEcards
                            where (i.nip == nip && i.Faktura_Numer == numer)
                            select i;
                return query;
            }
            public IQueryable<vPlatnosciEcard> FindInvoiceById(int id)
            {
                var query = from i in _dataContext.vPlatnosciEcards
                            where i.ID_faktury == id
                            select i;
                return query;
            }
            public List<PotwierdzeniaEcard> FindItemsByIdFaktury(int idFaktury)
            {
                var query = from vp in _dataContext.PlatnosciEcards
                            where vp.IDFaktury == idFaktury && vp.Status == true
                            orderby vp.IDFaktury descending
                            select vp;

                query.ToList();
                var tablica = new List<PotwierdzeniaEcard>();

                foreach (var pt in query)
                {
                    var query2 = from ps in _dataContext.PotwierdzeniaEcards
                                 where ps.ORDERNUMBER == pt.ORDERNUMBER && ps.CURRENTSTATE == "payment_deposited"
                                 orderby ps.id
                                 select ps;

                    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;
            }

    }
}
