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.GenericContent;
using Qusion.Cms.GenericContent.Core;
using Qusion.Cms.Web.Http;
using Qusion.WebApp.Areas.Qusion.Filters;
using Qusion.WebApp.Areas.Qusion.Models.GenericContents;
using Qusion.Cms.Site.Core;
using Qusion.Cms.Site;
using Qusion.Framework.Configuration;
using Qusion.WebApp.Areas.Qusion.Helpers;

namespace Qusion.WebApp.Areas.Qusion.Controllers.Api
{
   [Authorize]
    public class GenericContentController : ApiController
    {
        private readonly GenericContentService _genericContentService;
        private readonly PageService _pageService;
        public GenericContentController(GenericContentService genericContentService, PageService pageService)
        {
            _genericContentService = genericContentService;
            _pageService = pageService;
        }

        [HttpGet]
        public PageResult<ContentListModel> Query([FromUri] ContentQueryInput input)
        {
           GenericContentQuery query = Mapper.Map(input, GenericContentQuery.Default) ?? GenericContentQuery.Default;
                IEnumerable<GenericContent> contents = _genericContentService.Search(query);
                var a = contents.ToList().Where(x => x.Title != null);
                IList<ContentListModel> model = Mapper.Map<IList<GenericContent>, IList<ContentListModel>>(a.ToList());

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

        [HttpGet]
        public ContentModel Get(Guid id)
        {
           GenericContent content = _genericContentService.GetById(id);
                return Mapper.Map<GenericContent, ContentModel>(content);

        }


        [HttpGet]
        public ContentModel GetEdit(Guid id)
        {
            GenericContent content = _genericContentService.GetByIdForEditing(id);
            return Mapper.Map<GenericContent, ContentModel>(content);
        }

        [HttpGet]
        public ContentModel GetEditPage(Guid pageid)
        {
            GenericContent content = _genericContentService.GetByPageID(pageid);
            return Mapper.Map<GenericContent, ContentModel>(content);
        }
 

        [HttpPost]
        [ModelValidationFilter]
        [Authorize(Roles = "Administrator,Administrators,Radnik")]
        public HttpResponseMessage Create(ContentCreateInput newContent)
        {
                Guid idpage = newContent.PageID;
                Object obje = new Object();
                obje = idpage;
                Page page = _pageService.GetById(obje);
                GenericContent content = Mapper.Map<ContentCreateInput, GenericContent>(newContent);
                content.Page = page;
                _genericContentService.Save(content);
                return Request.CreateResponse(HttpStatusCode.OK, Mapper.Map<GenericContent, ContentModel>(content));
        }

        [HttpPost]
        [ModelValidationFilter]
        [Authorize(Roles = "Administrator,Administrators,Radnik")]
        public ContentModel Edit(ContentEditInput newContent)
        {
            GenericContent content = _genericContentService.GetById(newContent.ID);
            Mapper.Map(newContent, content);

            _genericContentService.Save(content);

            return Mapper.Map<GenericContent, ContentModel>(content);
        }

        [HttpGet]
        public HttpResponseMessage Delete(Guid id)
        {
            GenericContent content = _genericContentService.GetById(id);
            _genericContentService.Delete(content);
            return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
}