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;
using AMESCoreStudio.CommonTools.Result;

namespace AMESCoreStudio.WebApi.Controllers.AMES
{
    /// <summary>
    /// PLM轉入料號說明
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class PlmMeterialInfoeController : ControllerBase
    {
        private readonly AMESContext _context;

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

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        // GET: api/PlmMeterialInfoe
        [HttpGet]
        public async Task<ActionResult<IEnumerable<PlmMeterialInfo>>> GetPlmMeterialInfos()
        {
            return await _context.PlmMeterialInfos.ToListAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">PLM料號</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<IEnumerable<PlmMeterialInfo>>> GetPlmMeterialInfo(string id)
        {
            IQueryable<PlmMeterialInfo> q = _context.PlmMeterialInfos;
            q = q.Where(p => p.MeterialNo.Equals(id));

            var plmMeterialInfo = await q.ToListAsync();

            //if (plmMeterialInfo == null)
            //{
            //    return NotFound();
            //}

            return plmMeterialInfo;
        }

        /// <summary>
        /// 關鍵字查詢
        /// </summary>
        /// <param name="id">PLM料號</param>
        /// <returns></returns>
        [HttpGet("AutoComplete/{id}")]
        public async Task<ActionResult<IEnumerable<PlmMeterialInfo>>> GetPlmMeterialInfoAutoComplete(string id)
        {
            var plmMeterialInfo = await _context.PlmMeterialInfos.Where(w => w.MeterialNo.ToUpper().Contains(id.ToUpper())).Take(20).ToListAsync();
            return plmMeterialInfo;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="plmMeterialInfo"></param>
        /// <returns></returns>
        // PUT: api/PlmMeterialInfoe/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> PutPlmMeterialInfo(string id, PlmMeterialInfo plmMeterialInfo)
        {
            if (id != plmMeterialInfo.MeterialNo)
            {
                return BadRequest();
            }

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

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

            return NoContent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="plmMeterialInfo"></param>
        /// <returns></returns>
        // POST: api/PlmMeterialInfoe
        // 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<PlmMeterialInfo>> PostPlmMeterialInfo(PlmMeterialInfo plmMeterialInfo)
        {
            _context.PlmMeterialInfos.Add(plmMeterialInfo);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (PlmMeterialInfoExists(plmMeterialInfo.MeterialNo))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtAction("GetPlmMeterialInfo", new { id = plmMeterialInfo.MeterialNo }, plmMeterialInfo);
        }

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

            _context.PlmMeterialInfos.Remove(plmMeterialInfo);
            await _context.SaveChangesAsync();

            return plmMeterialInfo;
        }

        private bool PlmMeterialInfoExists(string id)
        {
            return _context.PlmMeterialInfos.Any(e => e.MeterialNo == id);
        }
    }
}