﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using EventManager.Server.Data;
using EventManager.Server.Data.Model;

namespace EventManager.Server.WepApi.Controllers.DataControllers
{
    /*
    The WebApiConfig class may require additional changes to add a route for this controller. Merge these statements into the Register method of the WebApiConfig class as applicable. Note that OData URLs are case sensitive.

    using System.Web.Http.OData.Builder;
    using System.Web.Http.OData.Extensions;
    using EventManager.Server.Data.Model;
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<Hotel>("Hotels");
    builder.EntitySet<Photo>("Photos"); 
    builder.EntitySet<Room>("Rooms"); 
    config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
    */
    public class HotelsController : ODataController
    {
        private EventManagerDataContext db = new EventManagerDataContext();

        // GET: odata/Hotels
        [EnableQuery]
        public IQueryable<Hotel> GetHotels()
        {
            return db.Hotels;
        }

        // GET: odata/Hotels(5)
        [EnableQuery]
        public SingleResult<Hotel> GetHotel([FromODataUri] int key)
        {
            return SingleResult.Create(db.Hotels.Where(hotel => hotel.Id == key));
        }

        // PUT: odata/Hotels(5)
        public async Task<IHttpActionResult> Put([FromODataUri] int key, Delta<Hotel> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Hotel hotel = await db.Hotels.FindAsync(key);
            if (hotel == null)
            {
                return NotFound();
            }

            patch.Put(hotel);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!HotelExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(hotel);
        }

        // POST: odata/Hotels
        public async Task<IHttpActionResult> Post(Hotel hotel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Hotels.Add(hotel);
            await db.SaveChangesAsync();

            return Created(hotel);
        }

        // PATCH: odata/Hotels(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<Hotel> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Hotel hotel = await db.Hotels.FindAsync(key);
            if (hotel == null)
            {
                return NotFound();
            }

            patch.Patch(hotel);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!HotelExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(hotel);
        }

        // DELETE: odata/Hotels(5)
        public async Task<IHttpActionResult> Delete([FromODataUri] int key)
        {
            Hotel hotel = await db.Hotels.FindAsync(key);
            if (hotel == null)
            {
                return NotFound();
            }

            db.Hotels.Remove(hotel);
            await db.SaveChangesAsync();

            return StatusCode(HttpStatusCode.NoContent);
        }

        // GET: odata/Hotels(5)/Photos
        [EnableQuery]
        public IQueryable<Photo> GetPhotos([FromODataUri] int key)
        {
            return db.Hotels.Where(m => m.Id == key).SelectMany(m => m.Photos);
        }

        // GET: odata/Hotels(5)/Rooms
        [EnableQuery]
        public IQueryable<Room> GetRooms([FromODataUri] int key)
        {
            return db.Hotels.Where(m => m.Id == key).SelectMany(m => m.Rooms);
        }

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

        private bool HotelExists(int key)
        {
            return db.Hotels.Count(e => e.Id == key) > 0;
        }
    }
}
