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.BAS;

namespace AMESCoreStudio.WebApi.Controllers.BAS
{
    /// <summary>
    /// BAS002生產製程單位維護相關
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class LineInfoesController : ControllerBase
    {
        private readonly AMESContext _context;

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

        /// <summary>
        /// 获取全部單位资料
        /// </summary>
        /// <returns></returns>
        // GET: api/LineInfoes
        [HttpGet]
        public async Task<ActionResult<IEnumerable<LineInfo>>> GetLineInfo()
        {
            IQueryable<LineInfo> q = _context.LineInfoes;
            q = q.OrderBy(p => p.LineID);

            var LineInfo = await q.ToListAsync();


            foreach (var data in LineInfo)
            {
                data.Dept = _context.DeptInfoes.Find(data.DeptID);
            }

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

            return LineInfo;
        }

        /// <summary>
        /// 用ID获取该單位资料
        /// </summary>
        /// <returns></returns>
        // GET: api/LineInfoes/5
        [HttpGet("{id}")]
        public async Task<ActionResult<IEnumerable<LineInfo>>> GetLineInfo(int id)
        {
            IQueryable<LineInfo> q = _context.LineInfoes;
            q = q.Where(p => p.LineID.Equals(id));
            var lineInfo = await q.ToListAsync();

            //foreach (var data in LineInfo)
            //{
            //   data.Dept = _context.DeptInfoes.Where(p1 => p1.DeptID.Equals(data.DeptID)).FirstOrDefault();

            //}

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

            return lineInfo ;
        }

        /// <summary>
        /// 更改單位资料
        /// </summary>
        /// <returns></returns>
        // PUT: api/LineInfoes/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<LineInfo>> PutLineInfo(int id, LineInfo lineInfo)
        {
            if (id != lineInfo.LineID)
            {
                return BadRequest();
            }

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

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

            return lineInfo;
        }

        /// <summary>
        /// 新增單位资料
        /// </summary>
        /// <returns></returns>
        // POST: api/LineInfoes
        // 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<LineInfo>> PostLineInfo(LineInfo lineInfo)
        {
            Helper helper = new Helper(_context);
            lineInfo.LineID = helper.GetIDKey("LINE_ID").Result; //SHANI?
            lineInfo.CreateDate = DateTime.Now;
            lineInfo.CreateUserId = 0;
            lineInfo.UpdateDate = DateTime.Now;
            lineInfo.WipNo = -1;

            _context.LineInfoes.Add(lineInfo);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetLineInfo", new { id = lineInfo.LineID }, lineInfo);
        }

        /// <summary>
        /// 刪除單位资料
        /// </summary>
        /// <returns></returns>
        // DELETE: api/LineInfoes/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<LineInfo>> DeleteLineInfo(int id)
        {
            var lineInfo = await _context.LineInfoes.FindAsync(id);
            if (lineInfo == null)
            {
                return NotFound();
            }

            _context.LineInfoes.Remove(lineInfo);
            await _context.SaveChangesAsync();

            return lineInfo;
        }

        private bool LineInfoExists(int id)
        {
            return _context.LineInfoes.Any(e => e.LineID == id);
        }
    }
}