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

namespace adMoto.Payments.Web.Controllers
{
    // ReSharper disable InconsistentNaming
    public class eCardController : Controller
    // ReSharper restore InconsistentNaming
    {
        private readonly IRepository<PotwierdzeniaEcard> _eCardRepository;

        public eCardController()
        {
            _eCardRepository = new Repository<PotwierdzeniaEcard>(new DataContext());
        }

        public eCardController(IRepository<PotwierdzeniaEcard> eCardRepository)
        {
            _eCardRepository = eCardRepository;
        }

        public ActionResult Status()
        {
            var content = new ContentResult();

            try
            {
                if (System.Web.HttpContext.Current != null)
                    ErrorSignal.FromCurrentContext().Raise(new ConfirmationLogger(), System.Web.HttpContext.Current);

                var potwierdzenie = new PotwierdzeniaEcard();
                potwierdzenie.MERCHANTNUMBER = Request["MERCHANTNUMBER"] ?? Request.Form["MERCHANTNUMBER"];
                potwierdzenie.ORDERNUMBER = Request["ORDERNUMBER"] != null ? Convert.ToInt32(Request["ORDERNUMBER"]) : Convert.ToInt32(Request.Form["ORDERNUMBER"]);
                potwierdzenie.COMMTYPE = Request["COMMTYPE"] ?? Request.Form["COMMTYPE"];
                potwierdzenie.CURRENTSTATE = Request["CURRENTSTATE"] ?? Request.Form["CURRENTSTATE"];
                potwierdzenie.PREVIOUSSTATE = Request["PREVIOUSSTATE"] ?? Request.Form["PREVIOUSSTATE"];
                potwierdzenie.PAYMENTTYPE = Request["PAYMENTTYPE"] != null ? Convert.ToBoolean(Convert.ToInt32(Request["PAYMENTTYPE"])) : Convert.ToBoolean(Convert.ToInt32(Request.Form["PAYMENTTYPE"]));
                potwierdzenie.EVENTTYPE = Request["EVENTTYPE"] != null ? Convert.ToBoolean(Convert.ToInt32(Request["EVENTTYPE"])) : Convert.ToBoolean(Convert.ToInt32(Request.Form["EVENTTYPE"]));
                potwierdzenie.PAYMENTNUMBER = Request["PAYMENTNUMBER"] != null ? Convert.ToBoolean(Convert.ToInt32(Request["PAYMENTNUMBER"])) : Convert.ToBoolean(Convert.ToInt32(Request.Form["PAYMENTNUMBER"]));
                potwierdzenie.APPROVALCODE = Request["APPROVALCODE"] ?? Request.Form["APPROVALCODE"];
                potwierdzenie.VALIDATIONCODE = Request["VALIDATIONCODE"] ?? Request.Form["VALIDATIONCODE"];
                potwierdzenie.BIN = Request["BIN"] ?? Request.Form["BIN"];
                potwierdzenie.AUTHTIME = Request["AUTHTIME"] != null ? Convert.ToDateTime(Request["AUTHTIME"]) : Convert.ToDateTime(Request.Form["AUTHTIME"]);
                potwierdzenie.TYPE = Request["TYPE"] ?? Request.Form["TYPE"];
                potwierdzenie.WITHCVC = Request["WITHCVC"] ?? Request.Form["WITHCVC"];
                potwierdzenie.DATATRANSMISJI = DateTime.Now;

                _eCardRepository.Insert(potwierdzenie);

                content.Content = "OK";
            }
            catch (Exception ex)
            {
                if (System.Web.HttpContext.Current != null)
                    ErrorSignal.FromCurrentContext().Raise(ex);
                content.Content = "FALSE " + ex.Message + " " + ex.GetType();
            }

            return content;
        }
    }
}