using System;
using NUnit.Framework;
using Platnosci.Core.Linq;
using Platnosci.Core.Interface;
using Platnosci.Controllers;
using Platnosci.Models;
using System.Web.Mvc;

namespace Platnosci.Tests.Web
{
    [TestFixture] 
    class MerchantControllerTests
    {
        private readonly Function _function = new Function();
        private readonly ITranslateManager _translateManager = new FakeTranslation();

        [Test]
        [Category("Unit")]
        public void IncorrectUserIdentity_ReturnErrorView()
        {
            IRepository<vPlatnosciEcard> repVPayment = new Repository<vPlatnosciEcard>(new FakeDataContext());
            vPlatnosciEcard platnosc = _function.CreateInvoice(123, "nip1", "", 0, 0);
            repVPayment.Insert(platnosc);
            
            Payer payer = _function.CreatePayer(123, "test", "test");
            
            var controller = new MerchantController(repVPayment, null, _translateManager);
            controller.ControllerContext = _function.CreateControllerContext("nip2");

            var result = controller.Merchant(payer, "pl") as ViewResult;
            var error = (ErrorViewData)result.ViewData.Model;

            Assert.That(error.Error, Is.EqualTo("weryfikacja"));
            Assert.That(result.ViewName, Is.EqualTo("Error1"));
        }
        [Test]
        [Category("Unit")]
        public void PaymentNotFound_ReturnErrorView()
        {
            IRepository<vPlatnosciEcard> repVPayment = new Repository<vPlatnosciEcard>(new FakeDataContext());
            vPlatnosciEcard platnosc = _function.CreateInvoice(12, "nip1", "", 0, 0);
            repVPayment.Insert(platnosc);

            Payer payer = _function.CreatePayer(123, "test", "test");

            var controller = new MerchantController(repVPayment, null, _translateManager);
            controller.ControllerContext = _function.CreateControllerContext("nip2");

            var result = controller.Merchant(payer, "pl") as ViewResult;
            var error = (ErrorViewData)result.ViewData.Model;

            Assert.That(error.Error, Is.EqualTo("brakdanych"));
            Assert.That(result.ViewName, Is.EqualTo("Error1"));
        }
        [Test]
        [Category("Unit")]
        public void CheckCorrectAmount()
        {
            var idFaktury = 12345;
            var amountPL = 5000;
            var amountEUR = 1300;

            var repVPayment = new Repository<vPlatnosciEcard>(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 eCardData(repPayment);
            var result = eCardData.CreateMerchantData(invoice, payer, "pl", "ahaah");
            var payment = repPayment.FindOne(i => i.IDFaktury == idFaktury);

            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 CheckeCardUrl()
        {
            var m = new Merchant();
            var pl = new PlatnosciEcard();
            pl.ORDERDESCRIPTION = "abc/2009";
            m.Payment = pl;
            m.Id = "ABCD";
            
            var eCardData = new eCardData(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var result = eCardData.GetUrl(m);
            
            System.Diagnostics.Debug.WriteLine("eCard url:" + result + " " + m);
            Assert.That(result, Is.Not.EqualTo(""));
        }
        [Test]
        [Category("Unit")]
        public void AmountIsNotValid()
        {
            var ecarddata = new eCardData(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var invoice = _function.CreateInvoice(123, "nip", "abc/2009", 0, 2);
            
            var payer = _function.CreatePayer(123, "test", "test");
            var result = ecarddata.CreateMerchantData(invoice, payer, "PL", "abcd");

            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 OrderdescriptionIsNotValid()
        {
            var ecarddata = new eCardData(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var invoice = _function.CreateInvoice(123, "nip", "", 200, 2);

            var payer = _function.CreatePayer(123, "test", "test");
            var result = ecarddata.CreateMerchantData(invoice, payer, "PL", "abcd");

            System.Diagnostics.Debug.WriteLine("Error: " + result.Error + " is not valid.");
            Assert.That(result.Error, Is.EqualTo("orderdescription"));
        }
        [Test]
        [Category("Unit")]
        public void CurrencyIsNotValid()
        {
            var m = new Merchant();
            var p = new PlatnosciEcard();
            m.Payment = p;
            m.Payment.AMOUNT = 100;
            m.Payment.AUTODEPOSIT = true;
            m.Payment.ORDERDESCRIPTION = "abc/2009";
            m.IsValid();
            var result = m.Error;

            System.Diagnostics.Debug.WriteLine("Error: " + result + " is not valid.");
            Assert.That(result, Is.EqualTo("currency"));
        }
        [Test]
        [Category("Unit")]
        public void NameIsNotValid()
        {

            var ecarddata = new eCardData(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var invoice = _function.CreateInvoice(123, "nip", "abc/2009", 200, 2);

            var payer = _function.CreatePayer(123, "", "test");
            var result = ecarddata.CreateMerchantData(invoice, payer, "PL", "abcd");

            System.Diagnostics.Debug.WriteLine("Error: " + result.Error + " is not valid.");
            Assert.That(result.Error, Is.EqualTo("name"));
        }
        [Test]
        [Category("Unit")]
        public void SurnameIsNotValid()
        {
            var ecarddata = new eCardData(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var invoice = _function.CreateInvoice(123, "nip", "abc/2009", 200, 2);

            var payer = _function.CreatePayer(123, "test", "");
            var result = ecarddata.CreateMerchantData(invoice, payer, "PL", "abcd");

            System.Diagnostics.Debug.WriteLine("Error: " + result.Error + " is not valid.");
            Assert.That(result.Error, Is.EqualTo("surname"));
        }
        [Test]
        [Category("Unit")]
        public void LanguageIsNotValid()
        {
            var m = new Merchant();
            var p = new PlatnosciEcard();
            m.Payment = p;
            m.Payment.AMOUNT = 100;
            m.Payment.AUTODEPOSIT = true;
            m.Payment.ORDERDESCRIPTION = "abc/2009";
            m.Payment.CURRENCY = "test";
            m.Payment.NAME = "test";
            m.Payment.SURNAME = "test";

            m.IsValid();
            var result = m.Error;

            System.Diagnostics.Debug.WriteLine("Error: " + result + " is not valid.");
            Assert.That(result, Is.EqualTo("language"));
        }
        
        [Test]
        [Category("Unit")]
        public void MerchantDataIsValid()
        {
            var m = new Merchant();
            var p = new PlatnosciEcard();
            m.Payment = p;
            m.Payment.AMOUNT = 100;
            m.Payment.AUTODEPOSIT = true;
            m.Payment.ORDERDESCRIPTION = "abc/2009";
            m.Payment.CURRENCY = "test";
            m.Payment.NAME = "test";
            m.Payment.SURNAME = "test";
            m.Payment.LANGUAGE = "test";
            m.Payment.ORDERNUMBER = 100;
            m.Id = "aaaa";
            m.Password = "aa";
            m.Hash = "22";
            m.LinkFail = "a";
            m.LinkOk = "a";

            m.IsValid();
            var result = m.Error;
            Assert.That(result,Is.EqualTo(null));

            var ecarddata = new eCardData(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var invoice = _function.CreateInvoice(123, "nip", "abc/2009", 200, 2);

            var payer = _function.CreatePayer(123, "test", "test");
            var result2 = ecarddata.CreateMerchantData(invoice, payer, "PL", "abcd");

            System.Diagnostics.Debug.WriteLine("Wszystkie dane sa poprawne!!");
            Assert.That(result2.Error, Is.EqualTo(null));
        }
        [Test]
        [Category("Unit")]
        public void IncorrectValueAmount()
        {
            var ecarddata = new eCardData(new Repository<PlatnosciEcard>(new FakeDataContext()));
            var brutto = Convert.ToDecimal(0.11);
            var invoice = _function.CreateInvoice(123, "nip", "abc/2009", brutto, 2);

            var payer = _function.CreatePayer(123, "test", "test");
            var result = ecarddata.CreateMerchantData(invoice, payer, "PL", "abcd");

            Assert.That(result.Error, Is.EqualTo(null));
        }        
    }
}
