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>
    /// 料號站別工項資料檔
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class MaterialStationsItemController : ControllerBase
    {
        private readonly AMESContext _context;

        /// <summary>
        /// 建構式
        /// </summary>
        /// <param name="context"></param>
        public MaterialStationsItemController(AMESContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 查詢料號站別工項資料檔
        /// </summary>
        /// <returns></returns>
        // GET: api/MaterialStationsItems
        [HttpGet]
        public async Task<ActionResult<IEnumerable<MaterialStationsItem>>> GetMaterialStationsItems()
        {
            IQueryable<MaterialStationsItem> q = _context.MaterialStationsItems;
            q = q.OrderBy(p => p.MsiID);
            var MaterialStationsItem = await q.ToListAsync();
            return MaterialStationsItem;

        }


        /// <summary>
        /// 料號站別工項檔 by ID
        /// </summary>
        /// <param name="id">MaterialKpID</param>
        /// <returns></returns>
        // GET: api/MaterialStationsItems/5
        [HttpGet("{id}")]
        public async Task<IEnumerable<MaterialStationsItem>> GetMaterialStationsItem(int id)
        {
            IQueryable<MaterialStationsItem> q = _context.MaterialStationsItems;
            var result = await q.Where(p => p.MsiID == id).ToListAsync();
            return result;
        }

        /// <summary>
        /// 料號站別工項資料檔 by ItemID
        /// </summary>
        /// <param name="id">ItemID</param>
        /// <returns></returns>
        [HttpGet("ByItemID/{id}")]
        public async Task<IEnumerable<MaterialStationsItem>> GetMaterialStationsItemByItemID(int id)
        {
            IQueryable<MaterialStationsItem> q = _context.MaterialStationsItems;
            var result = await q.Where(p => p.ItemID == id).ToListAsync();
            return result;
        }

        /// <summary>
        /// 修改料號站別工項資料檔
        /// </summary>
        /// <returns></returns>
        // PUT: api/MaterialStationsItems/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<ResultModel<MaterialStationsItem>> PutMaterialStationsItem(int id, MaterialStationsItem MaterialStationsItem)
        {
            ResultModel<MaterialStationsItem> result = new ResultModel<MaterialStationsItem>();
            if (id != MaterialStationsItem.MsiID)
            {
                result.Success = false;
                result.Msg = "序號錯誤";
                return result;
            }

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

            try
            {
                await _context.SaveChangesAsync();
                result.Success = true;
                result.Msg = "OK";
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.InnerException.Message;
            }
            return result;
        }

        /// <summary>
        /// 新增料號站別工項資料檔
        /// </summary>
        /// <param name="MaterialStationsItem"></param>
        // POST: api/MaterialStationsItems
        // 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<ResultModel<MaterialStationsItem>> PostMaterialStationsItem(MaterialStationsItem MaterialStationsItem)
        {
            ResultModel<MaterialStationsItem> result = new ResultModel<MaterialStationsItem>();
            Helper helper = new Helper(_context);
            MaterialStationsItem.MsiID = helper.GetIDKey("MSI_ID").Result;

            _context.MaterialStationsItems.Add(MaterialStationsItem);
            try
            {
                await _context.SaveChangesAsync();
                result.Success = true;
                result.Msg = "OK";
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.InnerException.Message;
            }
            return result;
        }

        // DELETE: api/MaterialStationsItems/5
        [HttpDelete("{id}")]
        public async Task<ResultModel<MaterialStationsItem>> DeleteMaterialStationsItem(int id)
        {
            ResultModel<MaterialStationsItem> result = new ResultModel<MaterialStationsItem>();
            var MaterialStationsItem = await _context.MaterialStationsItems.Where(m => m.MsiID == id).FirstOrDefaultAsync();
            if (MaterialStationsItem == null)
            {
                result.Success = false;
                result.Msg = "序號不存在";
                return result;
            }

            _context.MaterialStationsItems.Remove(MaterialStationsItem);
            await _context.SaveChangesAsync();

            result.Success = true;
            result.Msg = "OK";
            return result;
        }

        private bool MaterialStationsItemExists(int id)
        {
            return _context.MaterialStationsItems.Any(e => e.MsiID == id);
        }
    }
}