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.Classifications;
using Qusion.Cms.Classifications.Core;
using Qusion.Cms.Core.Domain;
using Qusion.Cms.Core.Exceptions;
using Qusion.Cms.Media.Core;
using Qusion.Cms.Modules.Articles;
using Qusion.Cms.Modules.Articles.Core;
using Qusion.Cms.Web.Http;
using Qusion.WebApp.Areas.cms.Filters;
using Qusion.WebApp.Areas.cms.Models.Articles;
using Qusion.WebApp.Areas.cms.Models.Classifications;
using Qusion.Framework.Queries;
using System.Threading;
using Qusion.Framework.Configuration;
using System.Web;
using System.Globalization;
using Qusion.WebApp.Areas.cms.Helpers;



namespace Qusion.WebApp.Areas.cms.Controllers
{
    //[Authorize]
    public class ArticleLaktasiController : ApiController
    {
        private readonly ArticleService _articleService;
        private readonly ClassificationService _classificationService;
        private readonly ImageService _imageService;
        private readonly DocumentService _documentService;
        private readonly LabelService _labelService;
        private readonly LibraryService _libraryService;

        public ArticleLaktasiController(ArticleService articleService, ImageService imageService,
                                 ClassificationService classificationService, LabelService labelService, LibraryService libraryService, DocumentService documentService)
        {
            _articleService = articleService;
            _imageService = imageService;
            _documentService = documentService;
            _classificationService = classificationService;
            _labelService = labelService;
            _libraryService = libraryService;
        }

        /// <summary>
        ///   Queries all articles.
        /// </summary>
        /// <param name="input"> </param>
        /// <returns> </returns>
        [HttpGet]
        [Compress]
        public PageResult<ArticleListModel> Query([FromUri] ArticleQueryInput input)
        {
            
                ArticleQuery query = Mapper.Map(input, ArticleQuery.Default) ?? ArticleQuery.Default;
                query.Paging.PageSize = 10;
                if (query.Title != null)
                    query.Title = input.Title;
                IEnumerable<Article> articles = _articleService.Search(query);
                var a = articles.ToList().Where(x => x.Title != null);
                a.ToList().ForEach(e =>
                {
                    if (e.Summary.Length <= 35)
                    {
                        e.Summary = Convert.ToString(e.Summary + "...");
                    }

                    if (e.Summary.Length < 230)
                    {
                        e.Summary = Convert.ToString(e.Summary + "...");
                    }

                    if (e.Summary.Length > 230)
                    {
                        e.Summary = Convert.ToString(e.Summary.Substring(0, 230) + "...");
                    }
                   

                });
                
                IList<ArticleListModel> model = Mapper.Map<IList<Article>, IList<ArticleListModel>>(a.ToList());

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

        }

        /// <summary>
        ///   Queries all articles.
        /// </summary>
        /// <param name="input"> </param>
        /// <returns> </returns>
        [HttpGet]
        [Compress]
        public PageResult<ArticleListModel> QueryFour([FromUri] ArticleQueryInput input)
        {
           ArticleQuery query = Mapper.Map(input, ArticleQuery.Default) ?? ArticleQuery.Default;
                IEnumerable<Article> articles = _articleService.Search(query);
                var a = articles.ToList().Where(x => x.Title != null).Take(4);
                a.ToList().ForEach(e =>
                {
                    if (e.Summary.Length <= 35)
                    {
                        e.Summary = Convert.ToString(e.Summary + "...");
                    }

                    if (e.Summary.Length < 230)
                    {
                        e.Summary = Convert.ToString(e.Summary  + "...");
                    }

                    if (e.Summary.Length > 230)
                    {
                        e.Summary = Convert.ToString(e.Summary.Substring(0, 230) + "...");
                    }

                });
               
                IList<ArticleListModel> model = Mapper.Map<IList<Article>, IList<ArticleListModel>>(a.ToList());
                
                return new PageResult<ArticleListModel>
                {
                    Items = model,
                    Count = query.Paging.TotalCount
                };

        }

        [HttpGet]
        [Compress]
        public ArticleModel Get(Guid id)
        {
           
                Article article = _articleService.GetById(id);
                ArticleModel mapped = Mapper.Map<Article, ArticleModel>(article);
                if (article.Thumb != null)
                {
                    mapped.Thumb = new[] { article.Thumb.GuidID.ToString() };
                }

                if (article.Library != null)
                {
                    mapped.Library = new[] { article.Library.GuidID.ToString() };
                }

                if (article.Document != null)
                {
                    mapped.Document = new[] { article.Document.GuidID.ToString() };
                }

                if (article.LibraryDocument != null)
                {
                    mapped.LibraryDocument = new[] { article.LibraryDocument.GuidID.ToString() };
                }

                if (article.Karta != null)
                {
                    mapped.Karta = new[] { article.Karta.GuidID.ToString() };
                }
                return mapped;

            }

    }
}