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.Blogs.Core;
using Qusion.Cms.Modules.Blogs.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.Blogs;

namespace Qusion.WebApp.Areas.cms.Controllers.Api
{
  
    public class BlogLaktasiController : ApiController
    {
       
        private readonly BlogService _blogService;
        private readonly LabelService _labelService;
        private readonly BlogPostService _postService;
        private readonly UserService _userService;

        public BlogLaktasiController(BlogService blogService, UserService userService, BlogPostService postService,
                              LabelService labelService)
        {
            _blogService = blogService;
            _userService = userService;
            _postService = postService;
            _labelService = labelService;
        }



        [HttpGet]
        public BlogModel Get(Guid id)
        {
            Blog blog = _blogService.GetById(id);
            return Mapper.Map<Blog, BlogModel>(blog);
        }

        [HttpGet]
        public PageResult<BlogListModel> Query([FromUri] BlogQueryInput input)
        {
            BlogQuery query = Mapper.Map(input, BlogQuery.Default) ?? BlogQuery.Default;
            IEnumerable<Blog> blogs = _blogService.Search(query);
            IList<BlogListModel> model = Mapper.Map<IList<Blog>, IList<BlogListModel>>(blogs.ToList());

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

     

        /********************************************************************
         *******                 Posts manipulation                 *********
         *******************************************************************/

        [HttpGet]
        public PageResult<BlogPostListModel> QueryPosts([FromUri] BlogPostQueryInput input)
        {
            BlogPostQuery post = Mapper.Map(input, BlogPostQuery.Default) ?? BlogPostQuery.Default;
            IEnumerable<BlogPost> posts = _postService.Search(post);
            IList<BlogPostListModel> model = Mapper.Map<IList<BlogPost>, IList<BlogPostListModel>>(posts.ToList());

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

    }
}