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

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

        // GET api/RegistrarApi
        public IQueryable<Registrar> GetRegistrars()
        {
            return _uow.Registrars.List();
        }

        // GET api/RegistrarApi/5
        [ResponseType(typeof(Registrar))]
        public IHttpActionResult GetRegistrar(int id)
        {
            Registrar registrar = _uow.Registrars.GetById(id);
            if (registrar == null)
            {
                return NotFound();
            }

            return Ok(registrar);
        }

        // PUT api/RegistrarApi/5
        public IHttpActionResult PutRegistrar(int id, Registrar registrar)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            _uow.Registrars.Update(registrar);

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

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/RegistrarApi
        [ResponseType(typeof(Registrar))]
        public IHttpActionResult PostRegistrar(Registrar registrar)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _uow.Registrars.Add(registrar);
            _uow.SaveChanges();

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

        // DELETE api/RegistrarApi/5
        [ResponseType(typeof(Registrar))]
        public IHttpActionResult DeleteRegistrar(int id)
        {
            Registrar registrar = _uow.Registrars.GetById(id);
            if (registrar == null)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Der Registrar wurde nicht gefunden."));
            }
            if (_uow.Registrars.IsInUse(registrar.Id))
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, "Der Registrar wird noch verwendet.")); 
            }
            _uow.Registrars.Delete(registrar);
            _uow.SaveChanges();
            return Ok(registrar);
        }

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

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

        private readonly IUnitOfWork _uow;
    }
}