using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Qusion.Cms.Classifications.Core;
using Qusion.Cms.Media;
using Qusion.Cms.Media.Core;
using Qusion.Cms.Media.Images;
using Qusion.WebApp.Areas.cms.Filters;
using Qusion.WebApp.Areas.cms.Models.Images;
using Qusion.WebApp.Areas.cms.Models.Libraries;
using Ranslant.JSON.Linq;
using System.Security.Policy;
using System.Web;
using System.Web.Helpers;
using Qusion.Cms.Core.Domain;



namespace Qusion.WebApp.Areas.cms.Controllers
{
    public class ImageLaktasiController : ApiController 
    {
        private readonly ImageService _imageService;
        private readonly LabelService _labelService;
        private readonly LibraryService _libraryService;


        public ImageLaktasiController(ImageService imageService, LibraryService libraryService, LabelService labelService)
        {
            _imageService = imageService;
            _libraryService = libraryService;
            _labelService = labelService;
        }



        [HttpGet]
      [Compress]
        public HttpResponseMessage Query([FromUri] ImageQueryInput input)
        {
            ImageQuery query = Mapper.Map<ImageQueryInput, ImageQuery>(input);

            query.Paging.PageSize = 32;
            Library library = input.LibraryID.HasValue ? _libraryService.GetById(input.LibraryID) : null;
          
            string some = _imageService.Settings.CachePath;
            IList<ImageItem> images = _imageService.Search(query).ToList();
            var a = images.ToList().Where(x => x.Library.Type == MediaType.Image).Where(x => x.Library.Status == WorkflowStatus.Aktivan);
            var model = new ImageListWrapper
                            {
                                Library = Mapper.Map<Library, LibraryListModel>(library),
                                Images = Mapper.Map<IEnumerable<ImageItem>, IList<ImageListModel>>(a.ToList()),
                                Paging = query.Paging
                            };

            return Request.CreateResponse(HttpStatusCode.OK, model);
        }

        [HttpGet]
        [Compress]
        public IQueryable<ImageListModel> QueryAll()
        {
            string some = _imageService.Settings.CachePath;
            var images = _imageService.GetAll().ToList();
            var a = images.ToList().Where(x => x.Library.Type == MediaType.Image).Where(x => x.Library.Status == WorkflowStatus.Aktivan);
            var model = Mapper.Map<IEnumerable<ImageItem>, IList<ImageListModel>>(a.ToList());
            return model.AsQueryable();
        }

        [HttpGet]
      [Compress]
        public HttpResponseMessage QueryGallery([FromUri] ImageQueryInput input)
        {
            ImageQuery query = Mapper.Map<ImageQueryInput, ImageQuery>(input);

            query.Paging.PageSize = 32;
            Library library = input.LibraryID.HasValue ? _libraryService.GetById(input.LibraryID) : null;

            string some = _imageService.Settings.CachePath;
            IList<ImageItem> images = _imageService.Search(query).ToList();
            var a = images.ToList().Where(x => x.Library.Type == MediaType.Image).Where(x => x.Library.Status == WorkflowStatus.Draft);
            var model = new ImageListWrapper
            {
                Library = Mapper.Map<Library, LibraryListModel>(library),
                Images = Mapper.Map<IEnumerable<ImageItem>, IList<ImageListModel>>(a.ToList()),
                Paging = query.Paging
            };

            return Request.CreateResponse(HttpStatusCode.OK, model);
        }

        /// <summary>
        /// Reordering images in library.
        /// </summary>
        /// <param name="input">Input properties.</param>
        /// <returns>Http status ok.</returns>
        [HttpPost]
        public HttpResponseMessage Organize(OrganizeInput input)
        {
            _imageService.Organize(input.Members);
            return Request.CreateResponse(HttpStatusCode.OK);
        }


    }
}