using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using adMoto.Payments.Core.Data;
using adMoto.Payments.Test.Fakes;
using adMoto.Payments.Web.Models;
using adMoto.Payments.Core;
using adMoto.Payments.Core.Interfaces;
using WatiN.Core;

namespace adMoto.Payments.Test.UI
{
    public class UIHelper
    {
        private readonly TestDataHelper _testDataHelper = new TestDataHelper();
        private IRepository<FAKTURY> _repInvoices;
        private IRepository<FAKTURA_DETAIL> _repInvoiceDetails;
        private IRepository<PlatnosciEcard> _repPayment;
        private UIData uiData;
        private string nip = "test";    //testowy klient o id 76131 
        private string numer_faktury = "1/SLJ/1";        
        public static string LoginSite = "http://localhost:3646/pl/Account/LogOn";
        public static string ConfirmForm = "http://localhost:3646/pl/Platnosc/Form"; 

        public UIData CreateAndAddTestRecordToRepository(int amount)
        {    
            _repInvoices = new Repository<FAKTURY>(new DataContext());
            var invoices = new FAKTURY
                               {
                                   NUMER = 1,
                                   NUMER_ROZ = "SLJ",
                                   NUMER_ROK = 1,                                        
                                   NABYWCA_NIP = nip,
                                   ID_SPRZEDAWCY = 2,
                                   ID_NABYWCY = 76131,  //testowy klient 
                                   waluta_brutto = Convert.ToDouble(amount),
                                   waluta_miano = "EUR"
                               };
            _repInvoices.Insert(invoices);
            var invoice = _repInvoices.Find(p => p.NUMER == 1 && p.NUMER_ROK == 1 && p.NUMER_ROZ == "SLJ" && p.ID_NABYWCY == 76131).FirstOrDefault();

            if (invoice != null)
            {
                _repInvoiceDetails = new Repository<FAKTURA_DETAIL>(new DataContext());
                var invoiceDetails = new FAKTURA_DETAIL
                                        {
                                            ID_FAKTURY = invoice.ID_FAKTURY,
                                            SYMBOL_SWW = "TEST"
                                        };
                _repInvoiceDetails.Insert(invoiceDetails);

                var invoiceDetails2 = _repInvoiceDetails.Find(p => p.ID_FAKTURY == invoiceDetails.ID_FAKTURY && p.SYMBOL_SWW == "TEST").FirstOrDefault();

                if (invoiceDetails2 != null)
                {
                    uiData = new UIData
                                {
                                    FAKTURA_ID = Convert.ToInt32(invoiceDetails2.ID_FAKTURY),
                                    FAKTURA_DETAIL_ID = invoiceDetails2.ID_FAKTURA_DETAILS,
                                    Test_nip = nip,
                                    Test_numer_faktury = numer_faktury
                                };
                }
            }
            return uiData;
        }
        
        public void DeleteTestRecordsFromRepository(UIData uiData)
        {
            var invoice = _repInvoices.Find(p => p.ID_FAKTURY == uiData.FAKTURA_ID).FirstOrDefault();
            if (invoice != null)
                _repInvoices.Delete(invoice);

            var invoiceDetails = _repInvoiceDetails.Find(p => p.ID_FAKTURA_DETAILS == uiData.FAKTURA_DETAIL_ID).FirstOrDefault();
            if (invoiceDetails != null)
                _repInvoiceDetails.Delete(invoiceDetails);
        }

        public void DeleteTestPaymentFromRepository(UIData uiData)
        {
            _repPayment = new Repository<PlatnosciEcard>(new DataContext());
            var payment = _repPayment.Find(p => p.IDFaktury == uiData.FAKTURA_ID &&
                                                   p.NAME == uiData.Test_firstname &&
                                                   p.SURNAME == uiData.Test_surname &&
                                                   p.ORDERDESCRIPTION == uiData.Test_numer_faktury &&
                                                   p.nip == uiData.Test_nip).FirstOrDefault();

            if (payment != null)
                _repPayment.Delete(payment);
        }
        
        public void CloseWebBrowser(IE ie)
        {
            ie.ForceClose();
            ie.Close();
            ie.Dispose();
        }       
    }
}
