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

namespace adMoto.Payments.Test.Fakes
{
    public class FakeDataContext : IDataContext
    {
        private const string CODE_OK = "payment_deposited";        //transakcja potwierdzona do rozliczenia
       
        private readonly List<object> _lista = new List<object>();
        private readonly TestDataHelper _testDataHelper = new TestDataHelper();
 

        public IQueryable<T> GetTable<T>() where T : class
        {
            var query = _lista.Where(objects => typeof(T).IsAssignableFrom(objects.GetType()));
            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 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<Invoice> FindInvoiceByNipNumber(string nip, string numer)
        {
            throw new NotImplementedException();
        }
        public List<PotwierdzeniaEcard> FindItemsByIdFaktury(int idFaktury)
        {
            
            var platnosc = _testDataHelper.CreateNewPayment(123456, true, DateTime.Now, 123);
            var payment = new List<PlatnosciEcard>();
            var listaConfirm = new List<PotwierdzeniaEcard> {(PotwierdzeniaEcard) _lista[0]};
            payment.Add(platnosc);

            var newConfirm = new List<PotwierdzeniaEcard>();

            if (platnosc.IDFaktury == idFaktury) 
            {
                if (listaConfirm[0].ORDERNUMBER == platnosc.ORDERNUMBER && listaConfirm[0].CURRENTSTATE == CODE_OK)
                    newConfirm.Add(listaConfirm[0]);
            }
            System.Diagnostics.Debug.WriteLine("lista" + newConfirm.Count());  
            return newConfirm;
        }
        public int GetOrdernumber(string desc, int? id, DateTime? data)
        {
            return 1111;
        }
    }
}