﻿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>();

        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.VALIDATIONCODE = 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 i)
        {
            if (i == 1)
            {
                listaPlatnosci.Add(DodajPlatnosc(1000, "abcd", "12345"));
                listaPl.Add(createNewPayment(9999, true, DateTime.Now, 1000));
                listaPotwierdzenia.Add(DodajPotwierdzenie("000", 9999));
            }
            else if (i == 2)
            {
                listaPl.Add(createNewPayment(9999, true, DateTime.Now, 1000));
            }
        }
        public IQueryable<T> GetTable<T>() where T : class
        {
            var query = from objects in lista	
	            where typeof(T).IsAssignableFrom(objects.GetType())	
	            select objects;
	        return query.Select(o => (T)o).AsQueryable();
        }
        public void Insert<T>(T item) where T : class
        {
            lista.Add(item);
        }
        public void Delete<T>(T item) where T : class
        {
            lista.Remove(item);
        }
        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].VALIDATIONCODE == "000") 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();
        }
        public void SubmitChanges()
        {
            
        }
        
    }
}
