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

namespace Qusion.WebApp.Areas.cms.Controllers
{
  //  [Authorize]
    public class PageLaktasiController : ApiController
    {
        private readonly PageService _pageService;
        private readonly SiteService _siteService;

        public PageLaktasiController(PageService pageService, SiteService siteService)
        {
            _pageService = pageService;
            _siteService = siteService;
        }

        /// <summary>
        ///   Gets list of all pages.
        /// </summary>
        /// <returns> </returns>
        [HttpGet]
      [Compress]
        public PageResult<PageListModel> Query([FromUri] PageQueryInput input)
        {
            PageQuery query = Mapper.Map(input, PageQuery.Default) ?? PageQuery.Default;
                IEnumerable<Page> pages = _pageService.Search(query);
                var a = pages.ToList().Where(x => x.Title != null);
                IList<PageListModel> model = Mapper.Map<IList<Page>, IList<PageListModel>>(a.ToList());

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

  


        /// <summary>
        /// Gets list of top pages.
        /// </summary>
        /// <returns> </returns>
        [HttpGet]
        [Compress]
        public PageResult<PageListModel> Top(int tip,Guid? parentID = null)
        {
          IList<Page> pages;
                if (parentID.HasValue)
                {
                    Page page = _pageService.GetById(parentID.Value);
                    pages = page != null ? page.Children : new List<Page>();
                }
                else
                {
                    IEnumerable<Page> tree = _pageService.GetTopPages().Where(x => x.PageType.Value ==tip);
                    pages = _pageService.SortByOrdinal(tree).ToList();
                }
                var a = pages.ToList().Where(x => x.Title != null);
                IList<PageListModel> model = Mapper.Map<IList<Page>, IList<PageListModel>>(a.ToList());

                return new PageResult<PageListModel>
                {
                    Items = model,
                    Count = pages.Count
                };

        }
        /// <summary>
        ///   Get page for edit
        /// </summary>
        /// <param name="id"> </param>
        /// <returns> </returns>
        [HttpGet]
      [Compress]
        public HttpResponseMessage GetForEdit(Guid id)
        {
          Page page = _pageService.GetById(id);
                PageModel model = Mapper.Map<Page, PageModel>(page);
                if (page.InternalRedirect != null)
                    model.InternalRedirectID = page.InternalRedirect.GuidID;
                return Request.CreateResponse(HttpStatusCode.OK, model);
        }

        [HttpPost]
      [Compress]
        public HttpResponseMessage Root(Guid id)
        {
            Page page = _pageService.GetById(id);

            if (page == null)
                return Request.CreateResponse(HttpStatusCode.NotFound);

            _siteService.SetRoot(page);

            PageListModel model = Mapper.Map<Page, PageListModel>(page);
            return Request.CreateResponse(HttpStatusCode.OK, model);
        }

        [HttpPost]
        public HttpResponseMessage Reorder(PageReorderListModel model)
        {
            ReorderPage(model.Pages, null);
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        private void ReorderPage(IEnumerable<PageReorderModel> pages, Page parent)
        {
            int index = 1;
            foreach (PageReorderModel page in pages)
            {
                Page current = _pageService.GetById(new Guid(page.ID));
                current.Parent = null;
                current.Ordinal = index++;
                _pageService.Save(current);
                if (parent != null)
                {
                    current.Parent = parent;
                    _pageService.Save(parent);
                }
                if (page.Children.Count > 0)
                {
                    ReorderPage(page.Children, current);
                }
            }
        }

    }
}