using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using AMESCoreStudio.WebApi;
using AMESCoreStudio.WebApi.Models.AMES;

namespace AMESCoreStudio.WebApi.Controllers.AMES
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CalendarTablesController : ControllerBase
    {
        private readonly AMESContext _context;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public CalendarTablesController(AMESContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        // GET: api/CalendarTables
        [HttpGet]
        public async Task<ActionResult<IEnumerable<CalendarTable>>> GetCalendarTable()
        {
            return await _context.CalendarTables.ToListAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET: api/CalendarTables/5
        [HttpGet("{id}")]
        public async Task<ActionResult<CalendarTable>> GetCalendarTable(DateTime id)
        {
            var calendarTable = await _context.CalendarTables.FindAsync(id);

            if (calendarTable == null)
            {
                return NotFound();
            }

            return calendarTable;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="calendarTable"></param>
        /// <returns></returns>
        // PUT: api/CalendarTables/5
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
        [HttpPut("{id}")]
        public async Task<IActionResult> PutCalendarTable(DateTime id, CalendarTable calendarTable)
        {
            if (id != calendarTable.TimeID)
            {
                return BadRequest();
            }

            _context.Entry(calendarTable).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CalendarTableExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="calendarTable"></param>
        /// <returns></returns>
        // POST: api/CalendarTables
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
        [HttpPost]
        public async Task<ActionResult<CalendarTable>> PostCalendarTable(CalendarTable calendarTable)
        {
            _context.CalendarTables.Add(calendarTable);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (CalendarTableExists(calendarTable.TimeID))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtAction("GetCalendarTable", new { id = calendarTable.TimeID }, calendarTable);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // DELETE: api/CalendarTables/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<CalendarTable>> DeleteCalendarTable(DateTime id)
        {
            var calendarTable = await _context.CalendarTables.FindAsync(id);
            if (calendarTable == null)
            {
                return NotFound();
            }

            _context.CalendarTables.Remove(calendarTable);
            await _context.SaveChangesAsync();

            return calendarTable;
        }

        private bool CalendarTableExists(DateTime id)
        {
            return _context.CalendarTables.Any(e => e.TimeID == id);
        }
    }
}