using System;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Security;
using System.Collections.Generic;
using Platnosci.Core.Linq;
using Platnosci.Models;
using System.Globalization;
using System.Threading;
using Platnosci.Core.Interface;
using System.Resources;
using Platnosci.Core;

namespace Platnosci.Controllers
{  

    [HandleError]
    public class AccountController : Controller
    {

        private readonly IRepository<vPlatnosciEcard> _repository;
        private readonly PlatnosciDataContext _context;
        private FunkcjePlatnosci _func;
                
        public IFormsAuthentication FormsAuth
        {
            get;
            private set;
        }

        public AccountController()
        {
            _repository = new Repository<vPlatnosciEcard>(new DataContext1());
            FormsAuth = new FormsAuthenticationService();
            _context = new PlatnosciDataContext();
            _func = new FunkcjePlatnosci();
        }

        public AccountController(IFormsAuthentication formsAuth, IRepository<vPlatnosciEcard> repository, PlatnosciDataContext context, FunkcjePlatnosci func)
        {
            _repository = repository;
            FormsAuth = formsAuth ?? new FormsAuthenticationService();
            _context = context;
            _func = func;
        }

        public ActionResult LogOn(string language)
        {
            language = _func.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 numer_faktury, string returnUrl, string language)
        {

            language = _func.setLanguage(language);
            vPlatnosciEcard platnosc = new vPlatnosciEcard();    
            if (!ValidateLogOn(nip, numer_faktury))
            {
                return View();
            }
            else
            {
                var Login = _repository.FindInvoiceByNipNumber(nip, numer_faktury).SingleOrDefault();
                platnosc = Login;
                if (platnosc == null) return View();
            }
            _func.SetUserLogger(nip, numer_faktury);

            FormsAuth.SignIn(nip, false);

            if (!String.IsNullOrEmpty(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else return RedirectToAction("Show", "Platnosc", new { id = platnosc.ID_faktury });         
        }
        public ActionResult LogOff()
        {
            FormsAuth.SignOut();
            return RedirectToAction("LogOn", "Account");
        }

        private bool ValidateLogOn(string nip, string numer_faktury)
        {
            if (String.IsNullOrEmpty(nip))
            {
                string err_nip = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_nip").ToString();
                ModelState.AddModelError("nip", err_nip);
            }
            if (String.IsNullOrEmpty(numer_faktury))
            {
                string err_faktura = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_faktura").ToString();
                ModelState.AddModelError("numer_faktury", err_faktura);
            }
            if (ModelState.IsValid)
            {
                bool DaneOk = _repository.Exists(u => u.Faktura_Numer == numer_faktury && u.nip == nip);
                if (DaneOk == false)
                {
                    string err_logowanie = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_logowanie").ToString();
                    ModelState.AddModelError("_FORM", err_logowanie);
                }               
            }
            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();
        }
    }
}
