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.Polls.Core;
using Qusion.Cms.Modules.Polls.Domain;
using Qusion.Cms.Users.Core;
using Qusion.Cms.Users.Domain;
using Qusion.Cms.Web.Http;
using Qusion.WebApp.Areas.cms.Filters;
using Qusion.WebApp.Areas.cms.Models.Polls;
using Qusion.Cms.Modules.Polls.Queries;
using Qusion.Framework.Configuration;
using Qusion.WebApp.Areas.Qusion.Helpers;



namespace Qusion.WebApp.Areas.cms.Controllers.Api
{

    public class PollLaktasiController : ApiController
    {
       
        private readonly PollService _pollService;
        private readonly LabelService _labelService;
        private readonly UserService _userService;

        public PollLaktasiController(PollService pollService, UserService userService, PollService questionService,
                              LabelService labelService)
        {
            _pollService = pollService;
            _userService = userService;
            _labelService = labelService;
        }

 

        [HttpGet]
        public PageResult<PollListModel> Query([FromUri] PollQueryInput input)
        {
            PollQuery query = Mapper.Map(input, PollQuery.Default) ?? PollQuery.Default;
                IEnumerable<Poll> polls = _pollService.Search(query);
                var a = polls.ToList().Where(x => x.Status == WorkflowStatus.Aktivan);
                IList<PollListModel> model = Mapper.Map<IList<Poll>, IList<PollListModel>>(a.ToList());

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

        }

    

        [HttpGet]
        public HttpResponseMessage Delete(Guid id)
        {
            Poll poll = _pollService.GetById(id);
            _pollService.Delete(poll);
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        /********************************************************************
         *******                 Questions manipulation                 *********
         *******************************************************************/

        [HttpGet]
        public IQueryable<PollQuestionListModel> GetQuestioniByPollId(Guid pollId)
        {
            var list = _pollService.GetQustionByPollId(pollId);
                var model = Mapper.Map<IEnumerable<PollQuestion>, IList<PollQuestionListModel>>(list.OrderBy(x => x.CreatedOn));
                return model.AsQueryable();

        }


        [HttpGet]
        public HttpResponseMessage QuestionForEdit(Guid id)
        {
            PollQuestion question = _pollService.GetQuestionById(id);

            return Request.CreateResponse(HttpStatusCode.OK, Mapper.Map<PollQuestion, PollQuestionModel>(question));
        }



        /********************************************************************
      *******                 Answers manipulation                 *********
      *******************************************************************/

        [HttpGet]
        public IQueryable<PollAnswerListModel> GetAnswerByQuestionId(Guid questionId)
        {
            var list = _pollService.GetAnswerByQuesionId(questionId);
                var model = Mapper.Map<IEnumerable<PollAnswer>, IList<PollAnswerListModel>>(list.OrderBy(x => x.CreatedOn));
                return model.AsQueryable();
        }

        [HttpGet]
        public HttpResponseMessage AnswerForEdit(Guid id)
        {
            PollAnswer answer = _pollService.GetAnswerById(id);
            return Request.CreateResponse(HttpStatusCode.OK, Mapper.Map<PollAnswer, PollAnswerModel>(answer));
        }



        /********************************************************************
      *******                 Votes manipulation                 *********
      *******************************************************************/

       /// <summary>
       /// Glasaj
       /// </summary>
       /// <param name="answer"></param>
       /// <param name="ip"></param>
       /// <param name="user"></param>
       /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage ExecuteVote(string answer, string ip, string user)
        {
            var errors = new Dictionary<string, string[]>();
            errors.Add("model.IP", new[] { "polls.IpExist" });
            Guid a = new Guid(answer);
            PollAnswer ans = _pollService.GetAnswerById(a);
            if (_pollService.CanVote(ans.Question, ip))
            { 
            _pollService.ExecuteVote(ans,ip,user);
            return Request.CreateResponse(HttpStatusCode.OK);
            }
            return Request.CreateResponse(HttpStatusCode.OK, new { Error = true, Errors = errors });
        }

    }
}