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

namespace Qusion.WebApp.Areas.Qusion.Controllers.Api
{
 [Authorize]
    public class KartaController : ApiController
    {
       
        private readonly KartaService _kartaService;
        private readonly LabelService _labelService;

        public KartaController(KartaService KartaService, LabelService labelService)
        {
            _kartaService = KartaService;
            _labelService = labelService;
        }



        [HttpPost]
        [ModelValidationFilter]
        [Authorize(Roles = "Administrator,Administrators,Radnik")]
        public HttpResponseMessage Create(KartaCreateModel karta)
        {

               Karta newKarta = Mapper.Map<KartaCreateModel, Karta>(karta);
               if (karta.Labels != null)
               {
                   foreach (string labelIds in karta.Labels)
                   {
                       Label addlabel = _labelService.GetById(new Guid(labelIds));
                       newKarta.Labels.Add(addlabel);
                   }
               }
               _kartaService.Save(newKarta);
               return Request.CreateResponse(HttpStatusCode.OK, Mapper.Map<Karta, KartaModel>(newKarta));
        }

        [HttpPost]
        [ModelValidationFilter]
        [Authorize(Roles = "Administrator,Administrators,Radnik")]
        public HttpResponseMessage Edit(KartaEditModel karta)
        {
            Karta newKarta = _kartaService.GetById(karta.Id);

            if (newKarta.Labels != null)
            {
                newKarta.Labels.Clear();
                foreach (string labelIds in karta.Labels)
                    newKarta.Labels.Add(_labelService.GetById(new Guid(labelIds)));
            }
            try
            {
                Mapper.Map(karta, newKarta);
                _kartaService.Save(newKarta);
            }
            catch (AliasException e)
            {
                var errors = new Dictionary<string, string[]>();
                return Request.CreateResponse(HttpStatusCode.OK, new {Errors = errors});
            }

            return Request.CreateResponse(HttpStatusCode.OK, Mapper.Map<Karta, KartaModel>(newKarta));
        }

        [HttpGet]
        public KartaModel Get(Guid id)
        {
           Karta Karta = _kartaService.GetById(id);
           return Mapper.Map<Karta, KartaModel>(Karta);
           
        }

        [HttpGet]
        public PageResult<KartaListModel> Query([FromUri] KartaQueryInput input)
        {
            KartaQuery query = Mapper.Map(input, KartaQuery.Default) ?? KartaQuery.Default;
               IEnumerable<Karta> karte = _kartaService.Search(query);
               var a = karte.ToList().Where(x => x.Title != null);
               IList<KartaListModel> model = Mapper.Map<IList<Karta>, IList<KartaListModel>>(a.ToList());

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

        [HttpGet]
        public HttpResponseMessage Delete(Guid id)
        {
            Karta karta = _kartaService.GetById(id);
            _kartaService.Delete(karta);
            return Request.CreateResponse(HttpStatusCode.OK);
        }
  
    }
}