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.Web.Http;
using Qusion.Framework;
using Qusion.WebApp.Areas.cms.Filters;
using Qusion.WebApp.Areas.cms.Models.Classifications;
using Qusion.WebApp.Areas.cms.Models.Validation;
using Qusion.Framework.Configuration;
using Qusion.WebApp.Areas.cms.Helpers;

namespace Qusion.WebApp.Areas.cms.Controllers
{
   // [Authorize]
    public class ClassificationLaktasiController : ApiController
    {
        private readonly ClassificationService _classificationService;

        public ClassificationLaktasiController(ClassificationService classificationService)
        {
            _classificationService = classificationService;
        }

        [HttpGet]
      [Compress]
        public PageResult<ClassificationListModel> Query([FromUri] ClassificationQueryInput input)
        {
         
                var query = Mapper.Map(input, ClassificationQuery.Default) ?? ClassificationQuery.Default;
                var classifications = _classificationService.Search(query);
                var a = classifications.ToList().Where(x => x.Name != null);
                var model = Mapper.Map<IList<Classification>, IList<ClassificationListModel>>(a.ToList());

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

        [HttpGet]
      [Compress]
        public HttpResponseMessage GetByAlias(string alias)
        {
         
                var classification = _classificationService.GetByAlias(alias);
                var model = new ClassificationModel { Type = classification.ClassificationType.Value };
                Mapper.Map(classification, model);

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

        [HttpGet]
      [Compress]
        public ClassificationModel GetEdit(Guid id)
        {
           var classification = _classificationService.GetByIdForEditing(id);
                return Mapper.Map<Classification, ClassificationModel>(classification);
            
        }
    }
}