﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using Platnosci.Core.Linq;
using Platnosci.Models;
using Platnosci.Core.Interface;

namespace Platnosci.Controllers
{
    [HandleError]
    public class AccountController : Controller
    {
        private readonly IRepository<vPlatnosciEcard> _repository;
        private readonly FunkcjePlatnosci _funkcjePlatnosci;

        public IFormsAuthentication FormsAuth
        {
            get;
            private set;
        }

        public AccountController()
        {
            _repository = new Repository<vPlatnosciEcard>(new DataContext1());
            FormsAuth = new FormsAuthenticationService();
            _funkcjePlatnosci = new FunkcjePlatnosci();
        }

        public AccountController(IFormsAuthentication formsAuth, IRepository<vPlatnosciEcard> repository, FunkcjePlatnosci func)
        {
            _repository = repository;
            FormsAuth = formsAuth ?? new FormsAuthenticationService();
            _funkcjePlatnosci = func;
        }

        public ActionResult LogOn(string language)
        {
            _funkcjePlatnosci.setLanguage(language);
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
            Justification = "Needs to take same parameter type as Controller.Redirect()")]
        public ActionResult LogOn(string nip, string numerFaktury, string returnUrl, string language)
        {
            _funkcjePlatnosci.setLanguage(language);
            var platnosc = new vPlatnosciEcard();
            if (!ValidateLogOn(nip, numerFaktury))
            {
                return View();
            }
            
            var login = _repository.FindInvoiceByNipNumber(nip, numerFaktury).SingleOrDefault();
            platnosc = login;
            if (platnosc == null) return View();
            _funkcjePlatnosci.SetUserLogger(nip, numerFaktury);

            FormsAuth.SignIn(nip, false);

            if (!String.IsNullOrEmpty(returnUrl))
            {
                return Redirect(returnUrl);
            }
            
            return RedirectToAction("Show", "Platnosc", new { id = platnosc.ID_faktury });
        }

        public ActionResult LogOff()
        {
            FormsAuth.SignOut();
            return RedirectToAction("LogOn", "Account");
        }

        private bool ValidateLogOn(string nip, string numerFaktury)
        {
            if (String.IsNullOrEmpty(nip))
            {
                var errNip = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_nip").ToString();
                ModelState.AddModelError("nip", errNip);
            }
            if (String.IsNullOrEmpty(numerFaktury))
            {
                var errFaktura = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_faktura").ToString();
                ModelState.AddModelError("numerFaktury", errFaktura);
            }
            if (ModelState.IsValid)
            {
                var daneOk = _repository.Exists(u => u.Faktura_Numer == numerFaktury && u.nip == nip);
                if (daneOk == false)
                {
                    var errLogowanie = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_logowanie").ToString();
                    ModelState.AddModelError("_FORM", errLogowanie);
                }
            }
            return ModelState.IsValid;
        }
    }
    public interface IFormsAuthentication
    {
        void SignIn(string nip, bool createPersistentCookie);
        void SignOut();
    }

    public class FormsAuthenticationService : IFormsAuthentication
    {
        public void SignIn(string nip, bool createPersistentCookie)
        {
            FormsAuthentication.SetAuthCookie(nip, createPersistentCookie);
        }
        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
    }
}