using NUnit.Framework;
using WatiN.Core;

namespace adMoto.Payments.Test.UI
{
    [TestFixture]
    public class MerchantTests
    {
        private readonly UIHelper _uiHelper = new UIHelper();
        private static string adres = UIHelper.LoginSite;

        [Test]
        [Category("UI")]
        public void Redirects_To_Ecard_When_All_Details_Are_Correct()
        {
            //dodanie testowego rekordu do tabel: FAKTURY, FAKTURA_DETAILS
            var uiData = _uiHelper.CreateAndAddTestRecordToRepository(2);
            
            const string test = "test";
            uiData.Test_surname = test;
            uiData.Test_firstname = test;

            var ie = new IE(adres);
            ie.TextField(Find.ByName("numerFaktury")).TypeText(uiData.Test_numer_faktury);
            ie.TextField(Find.ByName("nip")).TypeText(uiData.Test_nip);
            ie.Button(Find.ById("loguj")).Click();

            ie.TextField(Find.ByName("Payer.FirstName")).TypeText(test);
            ie.TextField(Find.ByName("Payer.LastName")).TypeText(test);
            ie.Button(Find.ById("place")).Click();
            Assert.IsTrue(ie.ContainsText("Imię i nazwisko:" + test + " " + test));
            Assert.IsTrue(ie.Url.Contains("https://pay.ecard.pl/"));
            _uiHelper.CloseWebBrowser(ie);

            //usuniecie rekordu z tabel: FAKTURY, FAKTURA_DETAILS
            _uiHelper.DeleteTestRecordsFromRepository(uiData);

            //usuniecie rekordu z tabeli PatnosciEcard
            _uiHelper.DeleteTestPaymentFromRepository(uiData);
        }

        [Test]
        [Category("UI")]
        public void Returns_ZlyHash_When_Invoice_Amount_Is_Zero()
        {
            // wartosc testowej faktury 1/SLJ/1 jest 0 (zero)
            // wowczas eCard powinien zwrocic zlyHash - stala informujaca, ze cos jest nie tak...
            var uiData = _uiHelper.CreateAndAddTestRecordToRepository(0);

            const string test = "test";
            uiData.Test_surname = test;
            uiData.Test_firstname = test;

            var ie = new IE(adres);
            ie.TextField(Find.ByName("numerFaktury")).TypeText(uiData.Test_numer_faktury);
            ie.TextField(Find.ByName("nip")).TypeText(uiData.Test_nip);
            ie.Button(Find.ById("loguj")).Click();

            ie.TextField(Find.ByName("Payer.FirstName")).TypeText(test);
            ie.TextField(Find.ByName("Payer.LastName")).TypeText(test);
            ie.Button(Find.ById("place")).Click();
            Assert.IsTrue(ie.ContainsText("Wystąpił nieoczekiwany błąd"));
            _uiHelper.CloseWebBrowser(ie);

            //usuniecie rekordu z tabel: FAKTURY, FAKTURA_DETAILS
            _uiHelper.DeleteTestRecordsFromRepository(uiData);            
        }
    }
}