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

namespace adMoto.Payments.Test.Web
{
    [TestFixture]
    public class eCardDataTests
    { 
        private readonly Function _function = new Function();

        [Test]
        [Category("Unit")]
        public void SetHash_Returns_zlyHash_When_Passing_Invalid_Merchant()
        {
            //Arrange
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var platnosc = _function.CreateNewPayment(123, true, DateTime.Now, 1);
            repPayment.Insert(platnosc);
           
            var ecarddata = new MerchantHelper(repPayment);
           
            var merchant = new Merchant(){
                                             Payment = platnosc,
                                             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 platnosc = _function.CreateNewPayment(123, true, DateTime.Now, 1);
            
            repPayment.Insert(platnosc);

            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 platnosc = _function.CreateNewPayment(123, true, DateTime.Now, 1);
            platnosc.AMOUNT = 229;
            platnosc.CURRENCY = PaymentsUtils.PLN;

            repPayment.Insert(platnosc);

            var ecarddata = new MerchantHelper(repPayment);

            var merchant = new Merchant()
                               {
                                   Payment = platnosc,
                                   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 platnosc = _function.CreateNewPayment(123, true, DateTime.Now, 1);
            platnosc.AMOUNT = 0;
            platnosc.CURRENCY = PaymentsUtils.PLN;

            repPayment.Insert(platnosc);

            var ecarddata = new MerchantHelper(repPayment);

            var merchant = new Merchant()
                               {
                                   Payment = platnosc,
                                   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 platnosc = _function.CreateNewPayment(123, true, DateTime.Now, 1);
            platnosc.AMOUNT = 229;
            platnosc.CURRENCY = PaymentsUtils.PLN;

            repPayment.Insert(platnosc);

            var ecarddata = new MerchantHelper(repPayment);

            var merchant = new Merchant()
                               {
                                   Payment = platnosc,
                                   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 platnosc = _function.CreateNewPayment(123, true, DateTime.Now, 1);
            platnosc.AMOUNT = 11;
            platnosc.CURRENCY = PaymentsUtils.PLN;

            repPayment.Insert(platnosc);

            var ecarddata = new MerchantHelper(repPayment);

            var merchant = new Merchant()
                               {
                                   Payment = platnosc,
                                   Id = "171485000",
                                   Password = "ashSeth2",
                               };
            
            //Act
            var result = ecarddata.SetHash(merchant);

            //Assert
            System.Diagnostics.Debug.WriteLine(result.Length);
            Assert.That(result.Length, Is.GreaterThan(30));
        }

        [Test]
        [Category("Unit")]
        public void Create_Merchant_Data_Returns_Invoice_Correct_Amount_When_Merchant_Is_Valid()
        {
            //Arrange
            var idFaktury = 12345;
            var amountPL = 5000;
            var amountEUR = 1300;

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

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

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

            //Assert
            System.Diagnostics.Debug.WriteLine("Faktura jest w EUR.");
            System.Diagnostics.Debug.WriteLine("PLN:" + amountPL + "," + " EUR:" + amountEUR);
            Assert.That(payment.AMOUNT, Is.EqualTo(amountEUR * 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 = _function.CreateInvoice(123, "nip", "abc/2009", 0, 2);

            var payer = _function.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("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 = _function.CreateInvoice(123, "nip", "", 200, 2);

            var payer = _function.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 = _function.CreateInvoice(123, "nip", "abc/2009", 200, 2);

            var payer = _function.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 = _function.CreateInvoice(123, "nip", "abc/2009", 200, 2);

            var payer = _function.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 = _function.CreateInvoice(123, "nip", "abc/2009", 200, 2);

            var payer = _function.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 pl = new PlatnosciEcard() {ORDERDESCRIPTION = "abc/2009"};
            var m = new Merchant(){Payment = pl, Id = "ABCD" };
            var eCardData = new MerchantHelper(new Repository<PlatnosciEcard>(new FakeDataContext()));
            
            //Act
            var result = eCardData.GetUrl(m);

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