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.Qusion.Filters;
using Qusion.WebApp.Areas.Qusion.Models.Newsletters;
using Qusion.Framework.Configuration;
using Qusion.WebApp.Areas.Qusion.Helpers;

namespace Qusion.WebApp.Areas.Qusion.Controllers.Api
{
  [Authorize]
    public class NewsletterController : ApiController
    {
       
        private readonly NewsletterService _newsletterService;

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



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

        [HttpGet]
        public PageResult<NewsletterListModel> Query([FromUri] NewsletterQueryInput input)
        {
           NewsletterQuery newsletter = Mapper.Map(input, NewsletterQuery.Default) ?? NewsletterQuery.Default;
                  IEnumerable<Newsletter> newsletters = _newsletterService.Search(newsletter);
                  var a = newsletters.ToList().Where(x => x.Ime != null);
                  IList<NewsletterListModel> model = Mapper.Map<IList<Newsletter>, IList<NewsletterListModel>>(a.ToList());

                  return new PageResult<NewsletterListModel>
                  {
                      Items = model,
                      Count = newsletter.Paging.TotalCount
                  };
              
        }

      
        [HttpPost]
        [ModelValidationFilter]
        [Authorize(Roles = "Administrator,Administrators,Radnik")]
        public HttpResponseMessage Create(NewsletterCreateModel newsletter)
        {           
                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));
        }


        [HttpGet]
        public HttpResponseMessage Edit(Guid id)
        {
          Newsletter newsletter = _newsletterService.GetById(id);
             return Request.CreateResponse(HttpStatusCode.OK, Mapper.Map<Newsletter, NewsletterModel>(newsletter));
         
        }

        [HttpPost]
        [ModelValidationFilter]
        [Authorize(Roles = "Administrator,Administrators,Radnik")]
        public HttpResponseMessage Update(NewsletterEditModel newsletter)
        {
            Newsletter newNewsletter = _newsletterService.GetById(newsletter.ID);
            Mapper.Map(newsletter, newNewsletter);
            _newsletterService.Save(newNewsletter);
            return Request.CreateResponse(HttpStatusCode.OK, Mapper.Map<Newsletter, NewsletterEditModel>(newNewsletter));
        }

        [HttpGet]
        public HttpResponseMessage Delete(Guid id)
        {
            Newsletter newsletter = _newsletterService.GetById(id);
            _newsletterService.Delete(newsletter);
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        /// <summary>
        ///   Set newsletter aktivan i neaktivan
        /// </summary>
        /// <param name="id"> User id. </param>
        /// <returns> Http status code 200 (Ok) or 404 (NotFound) </returns>
        [HttpGet]
        public HttpResponseMessage Status(Guid id)
        {
            Newsletter newsletter = _newsletterService.GetById(id);
            if (newsletter.Status == WorkflowStatus.Aktivan)
            {
                newsletter.Status = WorkflowStatus.NeAktivan;
                _newsletterService.Save(newsletter);
                return Request.CreateResponse(HttpStatusCode.OK, new { newsletter.Status });
            }
            else
            {
                newsletter.Status = WorkflowStatus.Aktivan;
                _newsletterService.Save(newsletter);
                return Request.CreateResponse(HttpStatusCode.OK, new { newsletter.Status });
            }
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }


    }
}