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.Qusion.Models.Documents;
using Qusion.WebApp.Areas.Qusion.Models.Libraries;
using Qusion.Framework.Configuration;
using Qusion.WebApp.Areas.Qusion.Helpers;

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

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

        [HttpGet]
        public PageResult<DocumentListModel> Query([FromUri] DocumentQueryInput input)
        {
          
              DocumentQuery query = Mapper.Map(input, DocumentQuery.Default) ?? DocumentQuery.Default;
              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
              };
          

        }

        [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]
        public HttpResponseMessage RemoveLibrary(Guid id)
        {
            Library library = _libraryService.GetById(id);
            IList<DocumentItem> documents = _documentService.GetDocumentsByLibrary(id);
            var errors = new Dictionary<string, string[]>();

            if (errors.Count == 0)
            {
                try
                {
                    foreach (DocumentItem documentItem in documents)
                    {
                        _documentService.DeleteDocument(_documentService.GetById(documentItem.GuidID));
                    }
                    _libraryService.DeleteDirectory(library);
                    _libraryService.Delete(library);

                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                catch (Exception)
                {

                    errors.Add("removeError", new[] { "documents.not_possible_delete_gallery" });

                }
            }
            return Request.CreateResponse(HttpStatusCode.OK, new { Errors = errors });
        }

        [HttpGet]
        public HttpResponseMessage Remove(Guid id, bool permanently = false)
        {
            var errors = new Dictionary<string, string[]>();
            if (errors.Count == 0)
            {
                try
                {
                    if (permanently)
                        _documentService.DeleteDocument(_documentService.GetById(id));
                    else
                        _documentService.Delete(_documentService.GetById(id));

                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                catch (Exception e)
                {
                    errors.Add("removeDocError", new[] { "documents.not_possible_delete_doc" });
                }
            }
            return Request.CreateResponse(HttpStatusCode.OK, new { Errors = errors });
        }

        [HttpGet]
        public HttpResponseMessage RemoveSettings()
        {
            _documentService.Settings.Delete();
            return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
}