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

namespace Qusion.WebApp.Areas.cms.Controllers
{
    public class DocumentLaktasiController : ApiController
    {
        private readonly DocumentService _documentService;
        private readonly LibraryService _libraryService;

        public DocumentLaktasiController(DocumentService documentService, LibraryService libraryService)
        {
            _documentService = documentService;
            _libraryService = libraryService;
        }

        [HttpGet]
       [Compress]
        public PageResult<DocumentListModel> Query([FromUri] DocumentQueryInput input)
        {
        
                if (input.LibraryType != null)
                {
                    var type = input.LibraryType;
                    DocumentQuery query = Mapper.Map(input, DocumentQuery.Default) ?? DocumentQuery.Default;
                    if (query.Title != null)
                        query.Title = input.Title;
                    IEnumerable<DocumentItem> documents = _documentService.Search(query);
                    var a = documents.ToList().Where(x => x.Title != null && x.Library.LibraryType.Alias == type);
                    //a.ToList().Where(x => x.Title != null && x.Library.LibraryType.Alias == type);
                    IList<DocumentListModel> model = Mapper.Map<IList<DocumentItem>, IList<DocumentListModel>>(a.ToList());

                    return new PageResult<DocumentListModel>
                    {
                        Items = model,
                        Count =  query.Paging.TotalCount
                    };
                }
                else
                {
                    DocumentQuery query = Mapper.Map(input, DocumentQuery.Default) ?? DocumentQuery.Default;
                    if (query.Title != null)
                        query.Title = input.Title;
                    IEnumerable<DocumentItem> documents = _documentService.Search(query);
                    var a = documents.ToList().Where(x => x.Title != null);
                    IList<DocumentListModel> model = Mapper.Map<IList<DocumentItem>, IList<DocumentListModel>>(a.ToList());

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

        }
        /*DOKUMENTI NEKOG TIPA NPR SLUZBANI GLASNICI*/
        [HttpGet]
       [Compress]
        public PageResult<DocumentListModel> GetDocumentsByLibraryType(string type,int page)
        {
         
                var list = _documentService.GetDocumentsByLibraryType(type);
                var pagelist = list.ToList().Where(x => x.Title != null).Skip((page - 1) * 10).Take(10).ToList();
                IList<DocumentListModel> model = Mapper.Map<IList<DocumentItem>, IList<DocumentListModel>>(pagelist.ToList());
            
              
                return new PageResult<DocumentListModel>
                {
                    Items = model,
                    Count = list.Count()
                };

        }

        [HttpPost]
        public HttpResponseMessage Organize(OrganizeInput input)
        {
            _documentService.Organize(input.Members);
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        [HttpGet]
        public HttpResponseMessage Settings()
        {
            DocumentsSettings settings = _documentService.Settings;
            DocumentSettingsModel model = Mapper.Map<DocumentsSettings, DocumentSettingsModel>(settings);
            return Request.CreateResponse(HttpStatusCode.OK, model);
        }

        [HttpPost]
        public HttpResponseMessage Settings(DocumentSettingsInput model)
        {
            DocumentsSettings settings = _documentService.Settings;
            settings.General.PageSize = model.GeneralPageSize;
            Mapper.Map(model, settings);
            _documentService.Settings.Save();
            DocumentSettingsModel response = Mapper.Map<DocumentsSettings, DocumentSettingsModel>(settings);
            return Request.CreateResponse(HttpStatusCode.OK, response);
        }

        [HttpGet]
       [Compress]
        public DocumentModel Get(Guid id)
        {
          
                DocumentItem document = _documentService.GetById(id);
                DocumentModel mapped = Mapper.Map<DocumentItem, DocumentModel>(document);
                return mapped;
        }


    }
}