using System;
using adMoto.Payments.Core;
using adMoto.Payments.Core.Data;
using adMoto.Payments.Test.Fakes;
using adMoto.Payments.Web.Models;
using NUnit.Framework;

namespace adMoto.Payments.Test.Utils
{
    [TestFixture]
    // ReSharper disable InconsistentNaming
    public class MerchantHelperTests
    // ReSharper restore InconsistentNaming
    {
        private readonly TestDataHelper _testDataHelper = new TestDataHelper();

        [Test]
        [Category("Unit")]
        public void SetHash_Returns_ZlyHash_When_Passing_Invalid_Merchant()
        {
            //Arrange
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var payment = _testDataHelper.CreateNewPayment(123, true, DateTime.Now, 1);
            repPayment.Insert(payment);

            var ecarddata = new MerchantHelper(repPayment);

            var merchant = new Merchant
                               {
                                  Payment = payment,
                                  Id = "171485000",
                                  Password = "ashSeth2",
                               };

            //Act
            var result = ecarddata.SetHash(merchant);

            //Assert
            Assert.That(result, Is.EqualTo(Merchant.BAD_HASH));
        }

        [Test]
        [Category("Unit")]
        public void SetHash_Returns_PaymentErrorInfo_When_Passing_Invalid_Payment()
        {
            //Arrange
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var payment = _testDataHelper.CreateNewPayment(123, true, DateTime.Now, 1);

            repPayment.Insert(payment);

            var ecarddata = new MerchantHelper(repPayment);

            var merchant = new Merchant
                               {
                                   Payment = new PlatnosciEcard
                                                 {
                                                    ORDERDESCRIPTION = "dd",
                                                    IDFaktury = 21,
                                                    Data = DateTime.Now
                                                 },
                                   Id = "171485000",
                                   Password = "ashSeth2",
                               };

            //Act
            var result = ecarddata.SetHash(merchant);

            //Assert
            Assert.That(result, Is.EqualTo(Merchant.HASH_ERROR_INFO));
        }
        [Test]
        [Category("Unit")]
        public void SetHash_Returns_Correct_Hash_When_Passing_Valid_Data()
        {
            //Arrange
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var payment = _testDataHelper.CreateNewPayment(123, true, DateTime.Now, 1);
            payment.AMOUNT = 229;
            payment.CURRENCY = PaymentsUtils.PLN;

            repPayment.Insert(payment);

            var ecarddata = new MerchantHelper(repPayment);

            var merchant = new Merchant
                               {
                                   Payment = payment,
                                   Id = "171485000",
                                   Password = "ashSeth2",
                               };

            //Act
            var result = ecarddata.SetHash(merchant);

            //Assert
            Assert.That(result.Length, Is.GreaterThan(30));
        }

        [Test]
        [Category("Unit")]
        public void SetHash_Returns_Bad_Hash_When_Invoice_Amount_Is_Zero()
        {
            //Arrange
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var payment = _testDataHelper.CreateNewPayment(123, true, DateTime.Now, 1);
            payment.AMOUNT = 0;
            payment.CURRENCY = PaymentsUtils.PLN;

            repPayment.Insert(payment);

            var ecarddata = new MerchantHelper(repPayment);

            var merchant = new Merchant
                               {
                                   Payment = payment,
                                   Id = "171485000",
                                   Password = "ashSeth2",
                               };

            //Act
            var result = ecarddata.SetHash(merchant);

            //Assert
            Assert.That(result, Is.EqualTo(Merchant.BAD_HASH));
        }

        [Test]
        [Category("Unit")]
        public void SetHash_Returns_Bad_Hash_When_Password_Is_Wrong()
        {
            //Arrange
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var payment = _testDataHelper.CreateNewPayment(123, true, DateTime.Now, 1);
            payment.AMOUNT = 229;
            payment.CURRENCY = PaymentsUtils.PLN;

            repPayment.Insert(payment);

            var ecarddata = new MerchantHelper(repPayment);

            var merchant = new Merchant
                               {
                                   Payment = payment,
                                   Id = "TEST",
                                   Password = "test",
                               };

            //Act
            var result = ecarddata.SetHash(merchant);

            //Assert
            Assert.That(result, Is.EqualTo(Merchant.BAD_HASH));
        }

        [Test]
        [Category("Unit")]
        public void SetHash_Returns_Correct_Hash_When_Payment_Amount_Is_Less_Then_100()
        {
            //Arrange
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var payment = _testDataHelper.CreateNewPayment(123, true, DateTime.Now, 1);
            payment.AMOUNT = 11;
            payment.CURRENCY = PaymentsUtils.PLN;

            repPayment.Insert(payment);

            var ecarddata = new MerchantHelper(repPayment);

            var merchant = new Merchant
                               {
                                   Payment = payment,
                                   Id = "171485000",
                                   Password = "ashSeth2",
                               };

            //Act
            var result = ecarddata.SetHash(merchant);

            //Assert
            Assert.That(result.Length, Is.GreaterThan(30));
        }

        [Test]
        [Category("Unit")]
        public void Create_Merchant_Data_Returns_Invoice_Correct_Amount_When_Merchant_Is_Valid()
        {
            //Arrange
            const int idFaktury = 12345;
            const int amountInPln = 5000;
            const int amountInEur = 1300;

            var repVPayment = new Repository<Invoice>(new FakeDataContext());
            var invoice = _testDataHelper.CreateForeignInvoice(idFaktury, "nip1", "abc/2009", amountInPln, 0, amountInEur, "EUR");
            repVPayment.Insert(invoice);

            var payer = _testDataHelper.CreatePayer(idFaktury, "test", "test");
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var eCardData = new MerchantHelper(repPayment);

            //Act
            eCardData.CreateMerchantData(invoice, payer, "pl", "ahaah");
            var payment = repPayment.FindOne(i => i.IDFaktury == idFaktury);

            //Assert
            Assert.That(payment.AMOUNT, Is.EqualTo(amountInEur * 100));
        }

        [Test]
        [Category("Unit")]
        public void Create_Merchant_Data_Returns_Bad_Hash_When_Invoice_Amount_Is_Zero()
        {
            //Arrange
            var ecarddata = new MerchantHelper(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var invoice = _testDataHelper.CreateInvoice(123, "nip", "abc/2009", 0, 2);

            var payer = _testDataHelper.CreatePayer(123, "test", "test");

            //Act
            var result = ecarddata.CreateMerchantData(invoice, payer, "PL", "abcd");

            //Assert
            Assert.That(result.Error, Is.EqualTo("hash"));  //dla wartosci amount=0 eCard zwroci hash o wartości "zlyHash"
        }

        [Test]
        [Category("Unit")]
        public void Create_Merchant_Data_Returns_Error_When_Orderdescription_Is_Not_Valid()
        {
            //Arrange
            var ecarddata = new MerchantHelper(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var invoice = _testDataHelper.CreateInvoice(123, "nip", "", 200, 2);

            var payer = _testDataHelper.CreatePayer(123, "test", "test");

            //Act
            var result = ecarddata.CreateMerchantData(invoice, payer, "PL", "abcd");

            //Assert
            System.Diagnostics.Debug.WriteLine("Error: " + result.Error + " is not valid.");
            Assert.That(result.Error, Is.EqualTo("orderdescription"));
        }

        [Test]
        [Category("Unit")]
        public void Create_Merchant_Data_Returns_Error_When_Name_Is_Not_Valid()
        {
            //Arrange
            var ecarddata = new MerchantHelper(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var invoice = _testDataHelper.CreateInvoice(123, "nip", "abc/2009", 200, 2);

            var payer = _testDataHelper.CreatePayer(123, "", "test");

            //Act
            var result = ecarddata.CreateMerchantData(invoice, payer, "PL", "abcd");

            //Assert
            System.Diagnostics.Debug.WriteLine("Error: " + result.Error + " is not valid.");
            Assert.That(result.Error, Is.EqualTo("name"));
        }

        [Test]
        [Category("Unit")]
        public void Create_Merchant_Data_Returns_Error_When_Surname_Is_Not_Valid()
        {
            //Arrange
            var ecarddata = new MerchantHelper(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var invoice = _testDataHelper.CreateInvoice(123, "nip", "abc/2009", 200, 2);

            var payer = _testDataHelper.CreatePayer(123, "test", "");

            //Act
            var result = ecarddata.CreateMerchantData(invoice, payer, "PL", "abcd");

            //Assert
            System.Diagnostics.Debug.WriteLine("Error: " + result.Error + " is not valid.");
            Assert.That(result.Error, Is.EqualTo("surname"));
        }

        [Test]
        [Category("Unit")]
        public void Create_Merchant_Data_Returns_Valid_Merchnt_When_All_Data_Are_Correct()
        {
            //Arrange
            var ecarddata = new MerchantHelper(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var invoice = _testDataHelper.CreateInvoice(123, "nip", "abc/2009", 200, 2);

            var payer = _testDataHelper.CreatePayer(123, "test", "test");

            //Act
            var result2 = ecarddata.CreateMerchantData(invoice, payer, "PL", "abcd");

            //Assert
            System.Diagnostics.Debug.WriteLine("Wszystkie dane sa poprawne!!");
            Assert.That(result2.Error, Is.EqualTo(null));
        }

        [Test]
        [Category("Unit")]
        public void Get_Url_Returns_Correct_Ecard_Url()
        {
            //Arrange
            var payment = new PlatnosciEcard { ORDERDESCRIPTION = "abc/2009" };
            var merchant = new Merchant { Payment = payment, Id = "ABCD" };
            var eCardData = new MerchantHelper(new Repository<PlatnosciEcard>(new FakeDataContext()));

            //Act
            var result = eCardData.GetUrl(merchant);

            //Assert
            System.Diagnostics.Debug.WriteLine("eCard url:" + result + " " + merchant);
            Assert.That(result, Is.Not.EqualTo(""));
        }
    }
}