using System;
using System.Linq;
using System.Web.Mvc;
using adMoto.Payments.Core;
using adMoto.Payments.Core.Data;
using adMoto.Payments.Core.Interfaces;
using adMoto.Payments.Web.Models;
using Elmah;

namespace adMoto.Payments.Web.Controllers
{
    public class PlatnoscController : Controller
    {
        public const string ISPAID = "payment_deposited";       //transakcja potwierdzona do rozliczenia
        private readonly IRepository<Invoice> _repVPayment;
        private readonly IRepository<PlatnosciEcard> _repPayment;
        private readonly IRepository<PotwierdzeniaEcard> _repConfirm;
        private readonly ITranslateManager _translateManager;
        private readonly PaymentsUtils _paymentsUtils;

        public PlatnoscController()
        {
            _repVPayment = new Repository<Invoice>(new DataContext());
            _repPayment = new Repository<PlatnosciEcard>(new DataContext());
            _repConfirm = new Repository<PotwierdzeniaEcard>(new DataContext());
            _paymentsUtils = new PaymentsUtils(_repPayment);
            _translateManager = new Translation();
        }
        public PlatnoscController(IRepository<Invoice> repVPayment, IRepository<PlatnosciEcard> repPayment, IRepository<PotwierdzeniaEcard> repConfirm, ITranslateManager translate)
        {
            _repVPayment = repVPayment;
            _repPayment = repPayment;
            _repConfirm = repConfirm;
            _translateManager = translate;
            _paymentsUtils = new PaymentsUtils(_repPayment, _translateManager);

        }

        [Authorize]
        public ActionResult Show(string id, string language)
        {
            _paymentsUtils.SetLanguage(language);
            var id1 = ConvertId(id);

            var invoice = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();

            var errorViewData = _paymentsUtils.IsError(invoice, HttpContext.User.Identity.Name);
            if (!String.IsNullOrEmpty(errorViewData.Error))
                return View("Error1", errorViewData);

            var invoiceDeatailsViewData = InitInvoiceDetailsViewData(invoice);

            var tablicaPotwierdzenia = _repConfirm.FindItemsByIdFaktury(id1); //szukamy uregulowanej faktury
            if (tablicaPotwierdzenia.Count > 0) //platnosc za fakture zostala uregulowana
            {
                var dataZaplaty = String.Format("{0:dd-MM-yyyy}", tablicaPotwierdzenia[0].AUTHTIME);
                invoiceDeatailsViewData.Info = String.Format(_translateManager.Translate("tlumaczenia", "zaplacono"), invoice.Faktura_Numer, dataZaplaty);
                invoiceDeatailsViewData.Termin = dataZaplaty;
                return View("Paid", invoiceDeatailsViewData);
            }
            return View(invoiceDeatailsViewData);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Show(Payer payer, string language)
        {
            _paymentsUtils.SetLanguage(language);

            if (String.IsNullOrEmpty(payer.FirstName))
                ModelState.AddModelError("Payer.FirstName", _translateManager.Translate("tlumaczenia", "err_imieWK"));
            else if (payer.FirstName.Length > 25)
                ModelState.AddModelError("Payer.FirstName", String.Format(_translateManager.Translate("tlumaczenia", "ToLongValue"), "25"));

            if (String.IsNullOrEmpty(payer.LastName))
                ModelState.AddModelError("Payer.LastName", _translateManager.Translate("tlumaczenia", "err_nazwiskoWK"));
            else if (payer.LastName.Length > 30)
                ModelState.AddModelError("Payer.LastName", String.Format(_translateManager.Translate("tlumaczenia", "ToLongValue"), "30"));

            if (ModelState.IsValid == false)
            {
                var invoice = _repVPayment.Find(p => p.ID_faktury == payer.Id_faktury).SingleOrDefault();
                var errorViewData = _paymentsUtils.IsError(invoice, HttpContext.User.Identity.Name);

                if (!String.IsNullOrEmpty(errorViewData.Error))
                    return View("Error1", errorViewData);

                return View("Show", InitInvoiceDetailsViewData(invoice));
            }
            return RedirectToAction("Merchant", "Merchant", payer);
        }

        public ActionResult Ok(string id, string order, string language)
        {
            var orderId = ConvertId(order);
            _paymentsUtils.SetLanguage(language);
            var id1 = ConvertId(id);
            var invoice = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();

            if (invoice == null)
                return View("Error1", _paymentsUtils.InitErrorViewData(_translateManager.Translate("tlumaczenia", "brakdanych"), 0));

            var invoiceDeatailsViewData = InitInvoiceDetailsViewData(invoice);

            //sprawdzamy czy dla kombinacji ordernumber i idfaktury istnieje platnosc, 
            //jesli tak, to sprawdzamy czy przyszlo potwierdzenie z eCardu.             

            if (CheckConfirm(id1, orderId) == 0)   //nie ma potwierdzenia z eCardu
                invoiceDeatailsViewData.Info = String.Format(_translateManager.Translate("tlumaczenia", "blad1"), invoiceDeatailsViewData.Invoice.Faktura_Numer);

            else if (CheckConfirm(id1, orderId) == 2)
                invoiceDeatailsViewData.Info = _translateManager.Translate("tlumaczenia", "weryfikacja");

            return View(invoiceDeatailsViewData);
        }

        public ActionResult Fail(string id, string language)
        {
            _paymentsUtils.SetLanguage(language);
            var id1 = ConvertId(id);
            var invoice = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();

            if (invoice == null)
                return View("Error1",
                            _paymentsUtils.InitErrorViewData(
                                _translateManager.Translate("tlumaczenia", "brakdanych"), 0));

            var invoiceDeatailsViewData = InitInvoiceDetailsViewData(invoice);
            return View(invoiceDeatailsViewData);
        }

        public ActionResult Form()
        {
            return View();
        }

        private static Payer InitPayer(int idFaktury)
        {
            var payer = new Payer { Id_faktury = idFaktury };
            return payer;
        }

        private InvoiceDetailsViewData InitInvoiceDetailsViewData(Invoice invoice)
        {
            var invoiceDeatailsViewData = new InvoiceDetailsViewData();
            invoiceDeatailsViewData.Invoice = invoice;
            invoiceDeatailsViewData.Payer = InitPayer(invoice.ID_faktury);
            invoiceDeatailsViewData.Brutto = _paymentsUtils.BruttoToString(invoice.Brutto, invoice.waluta_brutto, invoice.waluta_miano);
            return invoiceDeatailsViewData;
        }

        public int ConvertId(string id)
        {
            int id1;
            return Int32.TryParse(id, out id1) ? id1 : 0;
        }

        public void UpdateStatus(int ordernumber, string currentstate)
        {
            var payment = _repPayment.Find(p => p.ORDERNUMBER == ordernumber).SingleOrDefault();

            if (payment != null && currentstate == ISPAID)
            {
                payment.Status = true;
                payment.Status_data = DateTime.Now;
                _repPayment.SubmitChanges();
            }
        }

        public int CheckConfirm(int idfaktury, int order)
        {
            var payment = _repPayment.Find(p => p.ORDERNUMBER == order && p.IDFaktury == idfaktury).SingleOrDefault();

            if (payment != null)
            {
                var confirm = _repConfirm.Find(p => p.ORDERNUMBER == order).FirstOrDefault();
                if (confirm == null) return 0; //potwierdzenie nie przyszlo z eCardu
            }
            else
            {
                return 2; //nie ma platnosci o takim idfaktury i ordernumber
            }

            return 1;    //potwierdzenie przyszlo z eCardu
        }
    }
}