using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Qusion.Cms.Messages.Domain;
using Qusion.Cms.Messages.Extensions;
using Qusion.Cms.Messages.Services;
using Qusion.WebApp.Areas.Qusion.Models.Messages;

namespace Qusion.WebApp.Areas.Qusion.Controllers.Api
{
    public class MessageController : ApiController
    {
        private readonly ConversationService _conversationService;
        private readonly MessageService _messageService;

        public MessageController(ConversationService conversationService,
                                 MessageService messageService)
        {
            _conversationService = conversationService;
            _messageService = messageService;
        }

        public HttpResponseMessage Get(Guid conversationId, Guid? referentId, bool includePrevious = false)
        {
            var conversation = _conversationService.GetById(conversationId);
            var member = conversation.GetMember(User.Identity.Name);
            var messages = _messageService.GetMemberMessages(conversation, member, false, referentId);

            var model = Mapper.Map<IList<Message>, IList<MessageModel>>(messages);
            return Request.CreateResponse(HttpStatusCode.OK, model);
        }
    }
}