using System;
using NUnit.Framework;
using Platnosci.Models;
using Platnosci.Core.Linq;

namespace Platnosci.Tests.Web
{
    [TestFixture]
    class eCardDataTests
    { 
        private readonly Function _function = new Function();

        [Test]
        [Category("Unit")]
        public void SetHash_Returns_zlyHash_When_Passing_Invalid_Merchant()
        {
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var platnosc = _function.CreateNewPayment(123, true, DateTime.Now, 1);
            repPayment.Insert(platnosc);
           
            var ecarddata = new eCardData(repPayment);
           
            var merchant = new Merchant(){
                Payment = platnosc,
                Id = "171485000",
                Password = "ashSeth2",
            };
            var result = ecarddata.SetHash(merchant);
            Assert.That(result, Is.EqualTo(Merchant.BAD_HASH));
        }
        
        [Test]
        [Category("Unit")]
        public void SetHash_Returns_PaymentErrorInfo_When_Passing_Invalid_Payment()
        {
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var platnosc = _function.CreateNewPayment(123, true, DateTime.Now, 1);
            
            repPayment.Insert(platnosc);

            var ecarddata = new eCardData(repPayment);

            var merchant = new Merchant()
            {
                Payment = new PlatnosciEcard(){
                    ORDERDESCRIPTION = "dd",
                    IDFaktury = 21,
                    Data = DateTime.Now
                },
                Id = "171485000",
                Password = "ashSeth2",
            };
            var result = ecarddata.SetHash(merchant);
            Assert.That(result, Is.EqualTo(Merchant.HASH_ERROR_INFO));
        }
        [Test]
        [Category("Unit")]
        public void SetHash_Returns_Correct_Hash_When_Passing_Valid_Data()
        {
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var platnosc = _function.CreateNewPayment(123, true, DateTime.Now, 1);
            platnosc.AMOUNT = 229;
            platnosc.CURRENCY = FunkcjePlatnosci.PLN;

            repPayment.Insert(platnosc);

            var ecarddata = new eCardData(repPayment);

            var merchant = new Merchant()
            {
                Payment = platnosc,
                Id = "171485000",
                Password = "ashSeth2",
            };
            var result = ecarddata.SetHash(merchant);
            Assert.That(result.Length, Is.EqualTo(48) );
        }

        [Test]
        [Category("Unit")]
        public void SetHash_Returns_BAD_HASH_When_Invoice_Amount_Is_Zero()
        {
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var platnosc = _function.CreateNewPayment(123, true, DateTime.Now, 1);
            platnosc.AMOUNT = 0;
            platnosc.CURRENCY = FunkcjePlatnosci.PLN;

            repPayment.Insert(platnosc);

            var ecarddata = new eCardData(repPayment);

            var merchant = new Merchant()
            {
                Payment = platnosc,
                Id = "171485000",
                Password = "ashSeth2",
            };
            var result = ecarddata.SetHash(merchant);
            Assert.That(result, Is.EqualTo(Merchant.BAD_HASH));
        }

        [Test]
        [Category("Unit")]
        public void SetHash_Returns_BAD_HASH_When_Password_Is_Wrong()
        {
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var platnosc = _function.CreateNewPayment(123, true, DateTime.Now, 1);
            platnosc.AMOUNT = 229;
            platnosc.CURRENCY = FunkcjePlatnosci.PLN;

            repPayment.Insert(platnosc);

            var ecarddata = new eCardData(repPayment);

            var merchant = new Merchant()
            {
                Payment = platnosc,
                Id = "TEST",
                Password = "test",
            };
            var result = ecarddata.SetHash(merchant);
            Assert.That(result, Is.EqualTo(Merchant.BAD_HASH));
        }
    }
}
