using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Http.Description;
using Rainbow.Data;
using Rainbow.Models;
using Rainbow.ViewModels;

namespace Rainbow.ApiControllers
{
    public class TicketResponseApiController : ApiController
    {
        public TicketResponseApiController(IUnitOfWork uow)
        {
            _uow = uow;
        }

        // GET api/TicketResponseApi
        public IQueryable<TicketResponseViewModel> GetTicketResponses(int? ticketId)
        {
            var responses = _uow.TicketResponses.List();
            if (ticketId.HasValue)
            {
                responses = responses.Where(t => t.TicketId == ticketId);
            }
            return responses.Select(t => new TicketResponseViewModel
                {
                    Id = t.Id,
                    Description = t.Description,
                    Created = t.Created,
                    CreatedBy = t.CreatedBy,
                    LastModified = t.LastModified,
                    LastModifiedBy = t.LastModifiedBy
                });
        }

        // GET api/TicketResponseApi/5
        [ResponseType(typeof(TicketResponse))]
        public IHttpActionResult GetTicketResponse(int id)
        {
            TicketResponse ticketresponse = _uow.TicketResponses.GetById(id);
            if (ticketresponse == null)
            {
                return NotFound();
            }

            return Ok(ticketresponse);
        }

        // PUT api/TicketResponseApi/5
        public IHttpActionResult PutTicketResponse(int id, TicketResponse ticketresponse)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != ticketresponse.Id)
            {
                return BadRequest();
            }

            _uow.TicketResponses.Update(ticketresponse);

            try
            {
                _uow.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TicketResponseExists(id))
                {
                    return NotFound();
                }
                throw;
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/TicketResponseApi
        [ResponseType(typeof(TicketResponse))]
        public IHttpActionResult PostTicketResponse(TicketResponse ticketresponse)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _uow.TicketResponses.Add(ticketresponse);
            _uow.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = ticketresponse.Id }, ticketresponse);
        }

        // DELETE api/TicketResponseApi/5
        [ResponseType(typeof(TicketResponse))]
        public IHttpActionResult DeleteTicketResponse(int id)
        {
            TicketResponse ticketresponse = _uow.TicketResponses.GetById(id);
            if (ticketresponse == null)
            {
                return NotFound();
            }

            _uow.TicketResponses.Delete(ticketresponse);
            _uow.SaveChanges();

            return Ok(ticketresponse);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _uow.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool TicketResponseExists(int id)
        {
            return _uow.TicketResponses.List().Count(e => e.Id == id) > 0;
        }

        private readonly IUnitOfWork _uow;
    }
}