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

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

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

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

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET: api/ProgramInfoes/5
        [HttpGet("{id}")]
        public async Task<ActionResult<IEnumerable<ProgramInfo>>> GetProgramInfo(int id)
        {
            //var programInfo = await _context.ProgramInfoes.FindAsync(id);

            IQueryable<ProgramInfo> q = _context.ProgramInfoes;

            q = q.Where(p => p.ProgramID.Equals(id));

            var programInfo = await q.ToListAsync();

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

            return programInfo;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="programInfo"></param>
        /// <returns></returns>
        // PUT: api/ProgramInfoes/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<ActionResult<ProgramInfo>> PutProgramInfo(int id, [FromBody] ProgramInfo programInfo)
        {
            if (id != programInfo.ProgramID)
            {
                return BadRequest();
            }

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

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

            return programInfo;
            //return NoContent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="programInfo"></param>
        /// <returns></returns>
        // POST: api/ProgramInfoes
        // 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<ProgramInfo>> PostProgramInfo([FromBody] ProgramInfo programInfo)
        {
            Helper helper = new Helper(_context);
            programInfo.ProgramID = helper.GetIDKey("PROGRAM_ID").Result;

            programInfo.CreateDateTime = DateTime.Now;
            programInfo.CreateUserId = 0;

            _context.ProgramInfoes.Add(programInfo);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetProgramInfo", new { id = programInfo.ProgramID }, programInfo);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // DELETE: api/ProgramInfoes/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<ProgramInfo>> DeleteProgramInfo(int id)
        {
            //var programInfo = await _context.ProgramInfoes.FindAsync(id);
            var programInfo = await _context.ProgramInfoes.Where(m => m.ProgramID == id).FirstOrDefaultAsync();

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

            _context.ProgramInfoes.Remove(programInfo);
            await _context.SaveChangesAsync();

            return programInfo;
        }

        private bool ProgramInfoExists(int id)
        {
            return _context.ProgramInfoes.Any(e => e.ProgramID == id);
        }
    }
}