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

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

        public DataContext()
        {
            _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) return;

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