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;
using System.Collections.Generic;

using System.Configuration;
using System.Linq;

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"];
                if (String.IsNullOrEmpty(potwierdzenie.MERCHANTNUMBER))
                    throw new ArgumentNullException("MERCHANTNUMBER");

                string ordernumber = Request["ORDERNUMBER"] ?? Request.Form["ORDERNUMBER"];
                if (!String.IsNullOrEmpty(ordernumber))
                    potwierdzenie.ORDERNUMBER = Convert.ToInt32(ordernumber);
                else
                    throw new ArgumentNullException("ORDERNUMBER");

                potwierdzenie.COMMTYPE = Request["COMMTYPE"] ?? Request.Form["COMMTYPE"];
                if (String.IsNullOrEmpty(potwierdzenie.COMMTYPE))
                    throw new ArgumentNullException("COMMTYPE");

                potwierdzenie.CURRENTSTATE = Request["CURRENTSTATE"] ?? Request.Form["CURRENTSTATE"];
                if (String.IsNullOrEmpty(potwierdzenie.CURRENTSTATE))
                    throw new ArgumentNullException("CURRENTSTATE");

                potwierdzenie.PREVIOUSSTATE = Request["PREVIOUSSTATE"] ?? Request.Form["PREVIOUSSTATE"];
                if (String.IsNullOrEmpty(potwierdzenie.PREVIOUSSTATE))
                    throw new ArgumentNullException("PREVIOUSSTATE");

                string paymenttype = Request["PAYMENTTYPE"] ?? Request.Form["PAYMENTTYPE"];
                if (!String.IsNullOrEmpty(paymenttype))
                    potwierdzenie.PAYMENTTYPE = Convert.ToBoolean(paymenttype);
                else
                    throw new ArgumentNullException("PAYMENTTYPE");

                string eventtype = Request["EVENTTYPE"] ?? Request.Form["EVENTTYPE"];
                if (!String.IsNullOrEmpty(eventtype))
                    potwierdzenie.EVENTTYPE = Convert.ToBoolean(eventtype);
                else
                    throw new ArgumentNullException("EVENTTYPE");

                string paymentnumber = Request["PAYMENTNUMBER"] ?? Request.Form["PAYMENTNUMBER"];
                if (!String.IsNullOrEmpty(paymentnumber))
                    potwierdzenie.PAYMENTNUMBER = Convert.ToBoolean(paymentnumber);
                else
                    throw new ArgumentNullException("PAYMENTNUMBER");
               
                potwierdzenie.VALIDATIONCODE = Request["VALIDATIONCODE"] ?? Request.Form["VALIDATIONCODE"];
                if (String.IsNullOrEmpty(potwierdzenie.VALIDATIONCODE))
                    throw new ArgumentNullException("VALIDATIONCODE");

                //zapisywany do bazy tylko wtedy, gdy validationcode = 000
                potwierdzenie.APPROVALCODE = Request["APPROVALCODE"] ?? Request.Form["APPROVALCODE"];
                if (String.IsNullOrEmpty(potwierdzenie.APPROVALCODE) && potwierdzenie.VALIDATIONCODE == "000")
                    throw new ArgumentNullException("APPROVALCODE");

                potwierdzenie.BIN = Request["BIN"] ?? Request.Form["BIN"];
                if (String.IsNullOrEmpty(potwierdzenie.BIN))
                    throw new ArgumentNullException("BIN");
                    
                string authtime = Request["AUTHTIME"] ?? Request.Form["AUTHTIME"];
                if (!String.IsNullOrEmpty(authtime))
                    potwierdzenie.AUTHTIME = Convert.ToDateTime(authtime);
                else
                    throw new ArgumentNullException("AUTHTIME");
               
                potwierdzenie.TYPE = Request["TYPE"] ?? Request.Form["TYPE"];
                if (String.IsNullOrEmpty(potwierdzenie.TYPE))
                    throw new ArgumentNullException("TYPE");

                potwierdzenie.WITHCVC = Request["WITHCVC"] ?? Request.Form["WITHCVC"];
                if (String.IsNullOrEmpty(potwierdzenie.WITHCVC))
                    throw new ArgumentNullException("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;
        }       
    }
}