using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Qusion.Cms.Classifications.Core;
using Qusion.Cms.Core;
using Qusion.Cms.Core.Domain;
using Qusion.Cms.Core.Exceptions;
using Qusion.Cms.Core.Security;
using Qusion.Cms.Modules.Newsletters.Core;
using Qusion.Cms.Modules.Newsletters.Domain;
using Qusion.Cms.Users.Core;
using Qusion.Cms.Users.Domain;
using Qusion.Cms.Web.Http;
using Qusion.WebApp.Areas.cms.Filters;
using Qusion.WebApp.Areas.cms.Models.Newsletters;
using Qusion.Framework.Configuration;
using Qusion.WebApp.Areas.cms.Helpers;
using Qusion.WebApp.Areas.cms.Mailers.News;
using Qusion.Cms.Messages.Domain;

namespace Qusion.WebApp.Areas.cms.Controllers.Api
{
   
    public class NewsletterLaktasiController : ApiController
    {
       
        private readonly NewsletterService _newsletterService;

        public NewsletterLaktasiController(NewsletterService newsletterService)
        {
            _newsletterService = newsletterService;
        }



        /********************************************************************
         *******                 Newsletter manipulation                 *********
         *******************************************************************/
      

        [HttpPost]
        [ModelValidationFilter]
        public HttpResponseMessage Create(NewsletterCreateModel newsletter)
        {
            var errors = new Dictionary<string, string[]>();
            errors.Add("model.Email", new[] { "users.EmailExist" });
            var email = newsletter.Email.ToString();
            NewsletterQueryInput input = new NewsletterQueryInput();
            input.Email = email;
            NewsletterQuery newsletterprovjera = Mapper.Map(input, NewsletterQuery.Default) ?? NewsletterQuery.Default;
            IEnumerable<Newsletter> rezultat = _newsletterService.GetNewsletterByEmail(email);
            var a = rezultat.ToList();
            if (a.Count == 0)
            {
                Newsletter newNewsletter = Mapper.Map<NewsletterCreateModel, Newsletter>(newsletter);
                newNewsletter.Status = WorkflowStatus.Aktivan;
                _newsletterService.Save(newNewsletter);
                Mapper.Map(newsletter, newNewsletter);
                _newsletterService.Save(newNewsletter);
                return Request.CreateResponse(HttpStatusCode.OK, Mapper.Map<Newsletter, NewsletterListModel>(newNewsletter));
            }
            return Request.CreateResponse(HttpStatusCode.OK, new { Error = true, Errors = errors });

        }

        [HttpPost]
        public HttpResponseMessage SendMessage(NewsletterMessageModel formp)
        {
                var mail = new CreateNewMessageNotification
                {
                    Email = "master@nenadjesic.com",
                    From = formp.Email.ToString(),
                    Subject = formp.Title,
                    ViewName = formp.Ime,
                    Body = formp.Content + Environment.NewLine + formp.Ime + Environment.NewLine + formp.Email
                };
                new NewsMailer().CreateNotification(mail).Send();
                return Request.CreateResponse(HttpStatusCode.OK);
        }

     
    }
}