using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using NUnit.Framework;
using MvcContrib.TestHelper;
using Platnosci.Controllers;

using Platnosci.Core.Interface;
using Platnosci.Core.Linq;
using WatiN.Core;
using System.Threading;
using Platnosci.Models;
using Moq;

namespace Platnosci.Tests.Web
{
    [TestFixture]   
    class PlatnosciControllerTests
    {
        private string merchantNumber = "123";
        private int orderNumber = 9999;
        private string code_Ok = "payment_deposited";        //transakcja potwierdzona do rozliczenia
        private string code_Bad = "payment_declined";        //transakcja odrzucona 
        private DateTime data = DateTime.Now;
        
        private int test = 1; //oznacza, ze nie ustawiamy tlumaczen poprzez funkcje ustawTlumaczenia()
                              //w controllerze Platnosci, tylko poprzez funkcje setXXXXXXX()

        private Function _f = new Function();
               
        [Test]
        [Category("Unit")]
        public void Status_Saves_Correct_Transaction()
        {          
           var repository = new Repository<PlatnosciEcard>(new FakeDataContext());
           var platnosci = _f.createNewPayment(orderNumber, false, DateTime.Now, 12345); 
           repository.Insert(platnosci);
           repository.SubmitChanges();

           IRepository<PotwierdzeniaEcard> rep = new Repository<PotwierdzeniaEcard>(new FakeDataContext());
           
           var builder = new TestControllerBuilder();
           var controller = new PlatnoscController(null, repository, rep, test);
           builder.InitializeController(controller);
           builder.Form.Add("MERCHANTNUMBER", merchantNumber.ToString());
           builder.Form.Add("AUTHTIME", DateTime.Now.ToString());
           builder.Form.Add("DATATRANSMISJI", DateTime.Now.ToString());
           builder.Form.Add("ORDERNUMBER", orderNumber.ToString());
           builder.Form.Add("CURRENTSTATE", code_Ok);           
           
           var result = controller.Status();
           
           System.Diagnostics.Debug.WriteLine("rep:" + rep.Count() + " repPl: " + repository.Count() );

           var potwierdzeniaEcard = rep.Find(o => o.ORDERNUMBER == orderNumber).SingleOrDefault();
           Assert.That(potwierdzeniaEcard.MERCHANTNUMBER, Is.EqualTo(merchantNumber));
           PlatnosciEcard payment_after_update = repository.FindOne(orderNumber);
           System.Diagnostics.Debug.WriteLine("Status musi byc 'true'. Jest " + payment_after_update.Status);
           Assert.That(payment_after_update.Status, Is.EqualTo(true));            
        }
        [Test]
        [Category("Unit")]
        public void Status_Saves_Correct_Transaction_IncorrectUpdate()
        {
            var repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            var platnosci = _f.createNewPayment(orderNumber, false, DateTime.Now, 12345);
            repPayment.Insert(platnosci);
            repPayment.SubmitChanges();
            IRepository<PotwierdzeniaEcard> repConfirm = new Repository<PotwierdzeniaEcard>(new FakeDataContext());
          
            var builder = new TestControllerBuilder();
            var controller = new PlatnoscController(null, repPayment, repConfirm, test);
            builder.InitializeController(controller);
            builder.Form.Add("MERCHANTNUMBER", merchantNumber.ToString());
            builder.Form.Add("AUTHTIME", DateTime.Now.ToString());
            builder.Form.Add("DATATRANSMISJI", DateTime.Now.ToString());
            builder.Form.Add("ORDERNUMBER", orderNumber.ToString());
            builder.Form.Add("CURRENTSTATE", code_Bad);

            var result = controller.Status();           
           
            var potwierdzeniaEcard = repConfirm.Find(o => o.ORDERNUMBER == orderNumber).SingleOrDefault();
            Assert.That(potwierdzeniaEcard.MERCHANTNUMBER, Is.EqualTo(merchantNumber));
            
            PlatnosciEcard payment_after_update = repPayment.FindOne(orderNumber);
            System.Diagnostics.Debug.WriteLine("Status musi byc 'false'. Jest " + payment_after_update.Status);
            Assert.That(payment_after_update.Status, Is.EqualTo(false));
        }
        
        [Test]
        [Category("Unit")]
        public void StatusIsUpdated()
        {
            IRepository<PlatnosciEcard> repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());
            PlatnosciEcard platnosc = _f.createNewPayment(orderNumber, false, data, 1);
            repPayment.Insert(platnosc);

            var controller = new PlatnoscController(null, repPayment, null, test);
            controller.UpdateStatus(orderNumber, code_Ok);   
            
            PlatnosciEcard payment_after_update = new PlatnosciEcard();
            payment_after_update = repPayment.FindOne(orderNumber);
            System.Diagnostics.Debug.WriteLine("Status musi byc 'true'. Jest " + payment_after_update.Status);
            Assert.That(payment_after_update.Status, Is.EqualTo(true));
        }
        [Test]
        [Category("Unit")]
        public void StatusIsNotUpdated()
        {
            IRepository<PlatnosciEcard> repPayment = new Repository<PlatnosciEcard>(new FakeDataContext());

            PlatnosciEcard platnosc = _f.createNewPayment(orderNumber, false, data, 1);
            repPayment.Insert(platnosc);

            var controller = new PlatnoscController(null, repPayment, null, test);
            controller.UpdateStatus(orderNumber, code_Bad);   

            PlatnosciEcard payment_after_update = new PlatnosciEcard();
            payment_after_update = repPayment.FindOne(i => i.ORDERNUMBER == orderNumber);
            System.Diagnostics.Debug.WriteLine("Status musi byc 'false'. Jest " + payment_after_update.Status);
            Assert.That(payment_after_update.Status, Is.EqualTo(false));
        }        
        [Test]
        [Category("Unit")]
        public void ActionShowPayment_IncorrectUserIdentity_ReturnErrorView()
        {
            IRepository<vPlatnosciEcard> repVPayment = new Repository<vPlatnosciEcard>(new FakeDataContext());
            vPlatnosciEcard platnosc = _f.createInvoice(123, "nip1", "", 0, 0);
            repVPayment.Insert(platnosc);

            var controller = new PlatnoscController(repVPayment, null, null, test);
            controller.ControllerContext = _f.createControllerContext("nip2");
            controller.setWeryfikacja("Niepoprawny nip");            
           
            var result = controller.Show("123","pl") as ViewResult;
            var error = (ErrorViewData)result.ViewData.Model;

            Assert.That(error.error, Is.EqualTo("Niepoprawny nip"));
            Assert.That(result.ViewName, Is.EqualTo("Error1"));
        }
        [Test]
        [Category("Unit")]
        public void ActionShowPayment_PaymentNotFound_ReturnErrorView()
        {
            IRepository<vPlatnosciEcard> repVPayment = new Repository<vPlatnosciEcard>(new FakeDataContext());
            vPlatnosciEcard platnosc = _f.createInvoice(123, "", "", 0, 0);
            repVPayment.Insert(platnosc);

            var controller = new PlatnoscController(repVPayment, null, null, test);
            controller.setBrakDanych("Platnosc o takim id nie istnieje");

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

            Assert.That(error.error, Is.EqualTo("Platnosc o takim id nie istnieje"));
            Assert.That(result.ViewName, Is.EqualTo("Error1"));
        }
        [Test]
        [Category("Unit")]
        public void ActionShowPayment_PaymentIsPaid()
        {
            //Tworzymy takie dane aby platnosc o danym id byla juz zaplacona 
            var repVPayment = new Repository<vPlatnosciEcard>(new FakeDataContext());
            var invoice = _f.createInvoice(123, "nip1", "aaa", 0, 0);
            repVPayment.Insert(invoice);
            repVPayment.SubmitChanges();

            var repConfirm = new Repository<PotwierdzeniaEcard>(new FakeDataContext());
            var confirm = _f.createConfirm(code_Ok, 123456);
            repConfirm.Insert(confirm);
            repConfirm.SubmitChanges();

            var controller = new PlatnoscController(repVPayment, null, repConfirm, test);
            controller.ControllerContext = _f.createControllerContext("nip1");
            controller.setZaplacono("Platnosc zostala uregulowana");

            var result = controller.Show("123", "pl") as ViewResult;
            var view = (InvoiceDetailsViewData)result.ViewData.Model;

            Assert.That(result.ViewName, Is.EqualTo("Paid"));
            Assert.That(view.info, Is.EqualTo("Platnosc zostala uregulowana"));
        }
        [Test]
        [Category("Unit")]
        public void ActionShowPayment_CorrectData_ReturnViewForPayment()
        {
            IRepository<vPlatnosciEcard> repVPayment = new Repository<vPlatnosciEcard>(new FakeDataContext());
            vPlatnosciEcard platnosc = _f.createInvoice(123, "nip1", "numer", 200, 0);
            repVPayment.Insert(platnosc);

            var repConfirm = new Repository<PotwierdzeniaEcard>(new FakeDataContext());
            var confirm = _f.createConfirm(code_Ok, 1);
            repConfirm.Insert(confirm);
            repConfirm.SubmitChanges();

            var controller = new PlatnoscController(repVPayment, null, repConfirm, test);
            controller.ControllerContext = _f.createControllerContext("nip1");

            var result = controller.Show("123", "pl") as ViewResult;
                      
            var view = (InvoiceDetailsViewData)result.ViewData.Model;
            System.Diagnostics.Debug.WriteLine("Brutto 200: " + view.vPlatnosciEcard.Brutto);
            System.Diagnostics.Debug.WriteLine("Nr Faktury musi byc 'numer'. Jest " + view.vPlatnosciEcard.Faktura_Numer);
            Assert.That(view.vPlatnosciEcard.Brutto, Is.EqualTo(200));
            Assert.That(view.vPlatnosciEcard.Faktura_Numer, Is.EqualTo("numer"));            
        }
        [Test]
        [Category("Unit")]
        public void AfterPay_PaymentIsNotValid()
        {
            IRepository<vPlatnosciEcard> repVPayment = new Repository<vPlatnosciEcard>(new FakeDataContext());
            vPlatnosciEcard platnosc = _f.createInvoice(123, "nip1", "numer", 200, 0);
            repVPayment.Insert(platnosc);

            var controller = new PlatnoscController(repVPayment, null, null, test);
            controller.ControllerContext = _f.createControllerContext("nip1");
            
            Payer payer = _f.createPayer(123, "", "test");  //Brak imienia
            controller.Show(payer, "pl");
            Assert.That(controller.ModelState.IsValid, Is.False);
        }
        [Test]
        [Category("Unit")]
        public void AfterPay_PaymentIsValid()
        {
            IRepository<vPlatnosciEcard> repVPayment = new Repository<vPlatnosciEcard>(new FakeDataContext());
            vPlatnosciEcard platnosc = _f.createInvoice(123, "nip1", "numer", 200, 0);
            repVPayment.Insert(platnosc);

            var controller = new PlatnoscController(repVPayment, null, null, test);
            controller.ControllerContext = _f.createControllerContext("nip1");

            Payer payer = _f.createPayer(123, "test", "test");  
            var result = controller.Show(payer, "pl") as ViewResult;

            System.Diagnostics.Debug.WriteLine("Model powinien byc 'true'. Jest " + controller.ModelState.IsValid);
            Assert.That(controller.ModelState.IsValid, Is.True);
        }
    }
}

