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

namespace Qusion.WebApp.Areas.Qusion.Controllers.Api
{
   [Authorize]
    public class ClassificationController : ApiController
    {
        private readonly ClassificationService _classificationService;

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

        [HttpGet]
        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]
        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);
            
        }

        [HttpPost]
        [ModelValidationFilter]
        [Authorize(Roles = "Administrator,Administrators,Radnik")]
        public ClassificationModel Create(ClassificationCreateInput classification)
        {

                var newClassification = new Classification
                                         {
                                             ClassificationType =
                                                 Enumeration.FromValue<ClassificationType>(classification.ClassificationType)
                                         };
                Mapper.Map(classification, newClassification);

                _classificationService.Save(newClassification);

                return Mapper.Map<Classification, ClassificationModel>(newClassification);

        }

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

        [HttpPost]
        [ModelValidationFilter]
        [Authorize(Roles = "Administrator,Administrators,Radnik")]
        public ClassificationModel Edit(ClassificationEditInput classification)
        {
           var newClassification = _classificationService.GetById(classification.Id);
           Mapper.Map(classification, newClassification);
           _classificationService.Save(newClassification);
           return Mapper.Map<Classification, ClassificationModel>(newClassification);
        }

        [HttpGet]
        public HttpResponseMessage Delete(Guid id)
        {
            var errors = new Dictionary<string, string[]>();
            
            var classification = _classificationService.GetById(id);
            if (classification == null)
                return Request.CreateResponse(HttpStatusCode.NotFound);

            if (classification.Labels.Count > 0)
                errors.Add("removeError", new[] { "classifications.labels_first" });
            

            if (errors.Count == 0)
            {
                _classificationService.Delete(classification);
                return Request.CreateResponse(HttpStatusCode.OK);
            }
            return Request.CreateResponse(HttpStatusCode.OK, new { Errors = errors });
        }
    }
}