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 PlmBomsController : ControllerBase
    {
        private readonly AMESContext _context;

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

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

        /// <summary>
        /// 根據零件位置查詢料號
        /// </summary>
        /// <param name="itemNo">料號</param>
        /// <param name="locationNo">零件位置</param>
        /// <returns></returns>
        [HttpGet("GetPlmBom4REP001")]
        public async Task<ActionResult<IEnumerable<PlmBom>>> GetPlmBom4REP001(string itemNo,string locationNo)
        {
            IQueryable<PlmBom> q = _context.PlmBoms;
            q = q.Where(p => p.MatnrP.Equals(itemNo) && (p.Ebort1.Contains(locationNo) || p.Ebort2.Contains(locationNo)));

            var plmBom = await q.ToListAsync();

            return plmBom;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET: api/PlmBoms/5
        [HttpGet("{id}")]
        public async Task<ActionResult<PlmBom>> GetPlmBom(string id)
        {
            var plmBom = await _context.PlmBoms.FindAsync(id);

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

            return plmBom;
        }


        /// <summary>
        /// 根據母階料號查詢子件料號
        /// </summary>
        /// <param name="id">母階料號</param>
        /// <returns></returns>
        [HttpGet("byMaterP/{id}")]
        public async Task<ActionResult<IEnumerable<PlmBom>>> GetPlmBombyMATERP(string id)
        {
            var plmBom = await _context.PlmBoms.Where(w => w.MatnrP == id).ToListAsync();

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

            return plmBom;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="plmBom"></param>
        /// <returns></returns>
        // PUT: api/PlmBoms/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> PutPlmBom(string id, PlmBom plmBom)
        {
            if (id != plmBom.BomID)
            {
                return BadRequest();
            }

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

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

            return NoContent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="plmBom"></param>
        /// <returns></returns>
        // POST: api/PlmBoms
        // 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<PlmBom>> PostPlmBom(PlmBom plmBom)
        {
            _context.PlmBoms.Add(plmBom);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (PlmBomExists(plmBom.BomID))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtAction("GetPlmBom", new { id = plmBom.BomID }, plmBom);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // DELETE: api/PlmBoms/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<PlmBom>> DeletePlmBom(string id)
        {
            var plmBom = await _context.PlmBoms.FindAsync(id);
            if (plmBom == null)
            {
                return NotFound();
            }

            _context.PlmBoms.Remove(plmBom);
            await _context.SaveChangesAsync();

            return plmBom;
        }

        private bool PlmBomExists(string id)
        {
            return _context.PlmBoms.Any(e => e.BomID == id);
        }
    }
}