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

namespace Platnosci.Core.Linq
{
    public class FakeDataContext : IDataContext
    {
        private List<vPlatnosciEcard> listaPlatnosci = new List<vPlatnosciEcard>();
        private List<PotwierdzeniaEcard> listaPotwierdzenia = new List<PotwierdzeniaEcard>();
        private List<PlatnosciEcard> listaPl = new List<PlatnosciEcard>();
        private List<object> lista = new List<object>();
        private string code_Ok = "payment_deposited";        //transakcja potwierdzona do rozliczenia
        private string code_Bad = "payment_declined";        //transakcja odrzucona 

        public vPlatnosciEcard DodajPlatnosc(int id, string numer, string nip)
        {
            vPlatnosciEcard p = new vPlatnosciEcard();
            p.ID_faktury = id;
            p.Faktura_Numer = numer;
            p.nip = nip;
            
            return p;
        }
        public PotwierdzeniaEcard DodajPotwierdzenie(string code, int ordernumber)
        {
            PotwierdzeniaEcard potwierdzenie = new PotwierdzeniaEcard();
            potwierdzenie.CURRENTSTATE = code;
            potwierdzenie.ORDERNUMBER = ordernumber;

            return potwierdzenie;
        }
        public PlatnosciEcard createNewPayment(int ordernumber, bool status, DateTime data, int id_faktury)
        {
            PlatnosciEcard platnosc = new PlatnosciEcard();
            platnosc.ORDERNUMBER = ordernumber;
            platnosc.Status = status;
            platnosc.Status_data = data;
            platnosc.IDFaktury = id_faktury;

            return platnosc;
        }
        public FakeDataContext()
        {
            listaPlatnosci.Add(DodajPlatnosc(1,"1","123"));
            listaPlatnosci.Add(DodajPlatnosc(2, "2", "aaa"));
        }
        //public FakeDataContext(int fake)
        //{
        //    if (fake == 1)
        //    {
        //        listaPlatnosci.Add(DodajPlatnosc(1000, "abcd", "12345"));
        //        listaPl.Add(createNewPayment(9999, true, DateTime.Now, 1000));
        //        listaPotwierdzenia.Add(DodajPotwierdzenie(code_Ok, 9999));
        //    }
        //    else if (fake == 2)
        //    {
        //        listaPl.Add(createNewPayment(9999, false, DateTime.Now, 1000));
        //    }
        //}


        private readonly List<object> _dataStore = new List<object>();

        public IQueryable<T> GetTable<T>() where T : class
        {
            var query = _dataStore.Where(objects => typeof(T).IsAssignableFrom(objects.GetType()));
            return query.Select(o => (T)o).AsQueryable();
        }

        public void Insert<T>(T item) where T : class
        {
            _dataStore.Add(item);
        }

        public void Delete<T>(T item) where T : class
        {
            _dataStore.Remove(item);
        }

        public void SubmitChanges()
        {
            InvokeCompleted(EventArgs.Empty);
        }

        public event EventHandler Completed;

        private void InvokeCompleted(EventArgs e)
        {
            var completedHandler = Completed;
            if (completedHandler != null) completedHandler(this, e);
        }

        public void Dispose()
        {
        }

        public IQueryable<vPlatnosciEcard> FindInvoiceById(int id)
	    {
            List<object> lp = lista;
            for (int i = 0; i < lp.Count(); i++)
            {
                if (lp[i].GetType() == typeof(vPlatnosciEcard))
                {
                    listaPlatnosci.Add((vPlatnosciEcard)lp[i]);
                }
            }
            lista.Clear();

            var query = from l in listaPlatnosci
                        where l.ID_faktury == id 
	                    select l;
                
            return query.AsQueryable();
	    }
        public IQueryable<vPlatnosciEcard> FindInvoiceByNipNumber(string nip, string numer)
        {
            List<object> lp = lista;
            for (int i = 0; i < lp.Count(); i++)
            {
                if (lp[i].GetType() == typeof(vPlatnosciEcard))
                {
                    listaPlatnosci.Add((vPlatnosciEcard)lp[i]);
                }
            }
            lista.Clear();

            var query = from l in listaPlatnosci
                        where l.nip == nip && l.Faktura_Numer == numer
                        select l;

            return query.AsQueryable();
        }
        public List<PotwierdzeniaEcard> FindItemsByIdFaktury(int idFaktury)
        {
            List<PotwierdzeniaEcard> listazaplaconych = new List<PotwierdzeniaEcard>();
            for (int j = 0; j < listaPl.Count(); j++)
            {
                for (int i = 0; i < listaPotwierdzenia.Count(); i++)
                {
                    if (listaPl[j].IDFaktury == idFaktury && listaPl[j].ORDERNUMBER == listaPotwierdzenia[i].ORDERNUMBER && listaPotwierdzenia[i].CURRENTSTATE == code_Ok) listazaplaconych.Add(listaPotwierdzenia[i]);
                }
            }
            return listazaplaconych;
        }
        public IQueryable<PlatnosciEcard> FindPaymentByOrdernumber(int ordernumber)
        {
            var query = from l in listaPl
                        where l.ORDERNUMBER == ordernumber
                        select l;

            return query.AsQueryable();
        }
    }
}
