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.Framework;
using Qusion.WebApp.Areas.cms.Models.Libraries;
using Qusion.WebApp.Areas.cms.Filters;
using Qusion.WebApp.Areas.cms.Models.GLibraries;
using Qusion.Cms.Modules.Media.Core;
using Qusion.Cms.Web.Http;
using Qusion.Cms.Core;
using Qusion.Cms.Core.Domain;
using Qusion.Cms.Core.Exceptions;
using Qusion.Cms.Core.Security;
using Qusion.Cms.Users.Core;
using Qusion.Cms.Users.Domain;
using Qusion.Cms.Web.Http;
using Qusion.WebApp.Areas.cms.Filters;
using Qusion.Framework.Configuration;
using Qusion.WebApp.Areas.cms.Helpers;



namespace Qusion.WebApp.Areas.cms.Controllers
{
    public class LibraryLaktasiController : ApiController
    {
        private readonly LibraryService _libraryService;
        private readonly LibraryTypeService _librarytypeService;
        private readonly UserService _userService;
   

        public LibraryLaktasiController(LibraryService libraryService, LibraryTypeService librarytypeService, UserService userService)
        {
            _libraryService = libraryService;
            _librarytypeService = librarytypeService;
            _userService = userService;

        }

        [HttpGet]
      [Compress]
        public IQueryable<LibraryListModel> All()
        {
          
                var list = _libraryService.GetAll();
                var a = list.ToList().Where(x => x.Title != null);
                var model = Mapper.Map<IEnumerable<Library>, IList<LibraryListModel>>(a);
                return model.AsQueryable();
            
        }

        [HttpGet]
      [Compress]
        public LibraryModel Get(Guid id)
        {
           
           var library = _libraryService.GetById(id);
           return Mapper.Map<Library, LibraryModel>(library);
            
        }

        /*VRACA DATEOTEKE PO TIPU NPR SLUZBENI GLASNIK - NIJE PAGIRANO*/
        [HttpGet]
      [Compress]
        public IQueryable<LibraryListModel> GetByLibraryType(Guid librarytype)
        {
           var list = _libraryService.GetByLibraryType(librarytype);
                var model = Mapper.Map<IEnumerable<Library>, IList<LibraryListModel>>(list);
                return model.AsQueryable();
            
        }

        /*VRACA DATEOTEKE PO TIPU NPR SLUZBENI GLASNIK + PAGING*/
        [HttpGet]
      [Compress]
        public PageResult<LibraryListModel> GetByLibraryTypePage(Guid librarytype, int page)
        {
           var list  = _libraryService.GetByLibraryType(librarytype);
                var pagelist = list.ToList().Where(x => x.Title != null).Skip((page - 1) * 10).Take(10).ToList();
                IList<LibraryListModel> model = Mapper.Map<IList<Library>, IList<LibraryListModel>>(pagelist.ToList());
  
                return new PageResult<LibraryListModel>
                {
                    Items = model,
                    Count = list.Count()
                };
            
        }
        /*VRACA LIBRARIE PO ALIASU ISTE*/
        [HttpGet]
      [Compress]
        public LibraryTypeModel GetByAlias(string alias)
        {
           
            LibraryType libraryType = _librarytypeService.GetByAlias(alias);
            return Mapper.Map<LibraryType, LibraryTypeModel>(libraryType);
            
        }

        /*OVDJE IDU PREGLEDI PO TIPU AKTIVNI NE AKTIVNI*/

        [HttpGet]
      [Compress]
     
        public IQueryable<LibraryListModel> GetByLibraryTypePageAktivan(Guid librarytype, string aktivan)
        {
           var list = _libraryService.GetByLibraryType(librarytype);
                var tolist = list.ToList().Where(x => x.Status.DisplayName == aktivan);
                var model = Mapper.Map<IEnumerable<Library>, IList<LibraryListModel>>(tolist);
                return model.AsQueryable();
            
        }
        

       
      

        /*VRACA LIBRARIES ODREDJENOG TIPA NPR IMAGES,DOCUMENTS,VIDEO...*/
        [HttpGet]
      [Compress]
        public IQueryable<LibraryListModel> GetByType(string type)
        {
           var mediaType = Enumeration.FromDisplayName<MediaType>(type);
                var list = _libraryService.GetByType(mediaType);
                if (mediaType == MediaType.Image || mediaType == MediaType.Video)
                {
                    var imagelist = list.ToList().Where(x => x.Status == WorkflowStatus.Aktivan);
                    var model = Mapper.Map<IEnumerable<Library>, IList<LibraryListModel>>(imagelist);
                    return model.AsQueryable();
                }
                else
                {
                    var model = Mapper.Map<IEnumerable<Library>, IList<LibraryListModel>>(list);
                    return model.AsQueryable();
                }
            
        }

        /*VRACA LIBRARIES TIPA DOCUMENTS PAGIRANO*/
        [HttpGet]
      [Compress]
        public PageResult<LibraryListModel> GetByTypeDocuments(string type, int page)
        {
          var mediaType = Enumeration.FromDisplayName<MediaType>(type);
                var list = _libraryService.GetByTypePaging(mediaType);
                var pagelist = list.ToList().Where(x => x.Title != null).Skip((page - 1) * 10).Take(10).ToList();
                IList<LibraryListModel> model = Mapper.Map<IList<Library>, IList<LibraryListModel>>(pagelist.ToList());

                return new PageResult<LibraryListModel>
                {
                    Items = model,
                    Count = list.Count()
                };
                
        }

        [HttpGet]
      [Compress]
        public LibraryTypeModel GetType(Guid id)
        {
           LibraryType librarytype = _librarytypeService.GetById(id);
           return Mapper.Map<LibraryType, LibraryTypeModel>(librarytype);
            
        }

        [HttpGet]
      [Compress]
        public PageResult<LibraryTypeListModel> QueryType([FromUri] LibraryTypeQueryInput input)
        {
           LibraryTypeQuery query = Mapper.Map(input, LibraryTypeQuery.Default) ?? LibraryTypeQuery.Default;
                query.Paging.PageSize = 50;
                IEnumerable<LibraryType> librarytype = _librarytypeService.Search(query);
                IList<LibraryTypeListModel> model = Mapper.Map<IList<LibraryType>, IList<LibraryTypeListModel>>(librarytype.ToList());

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

           }  
 
       
    }
}