using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using AMESCoreStudio.WebApi.Models.AMES;
using AMESCoreStudio.CommonTools.Result;
using AMESCoreStudio.WebApi.DTO.AMES;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace AMESCoreStudio.WebApi.Controllers.AMES
{
    [Route("api/[controller]")]
    [ApiController]
    public class WipSystemController : ControllerBase
    {
        private readonly AMESContext _context;

        /// <summary>
        /// 工單資訊-系統組裝工程資訊 Controller
        /// </summary>
        /// <param name="context"></param>
        public WipSystemController(AMESContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 工單資訊-系統組裝工程資訊 to WipNo
        /// </summary>
        /// <param name="id">WipNo</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<WipSystem>> GetWipSystem(string id)
        {
            IQueryable<WipSystem> q = _context.WipSystems;
            var wipSystem = await q.Where(p => p.WipNo == id).FirstOrDefaultAsync();

            //if (result.Data.Count() == 0)
            //{
            //    return NotFound();
            //}

            return wipSystem;
        }

        /// <summary>
        /// 查詢燒機時間
        /// </summary>
        /// <param name="id">WipNo</param>
        /// <returns>0:沒資料 -1:欄位格式有誤</returns>
        /// <remarks>0:沒資料 -1:欄位格式有誤</remarks> 
        [HttpGet("BITime/{id}")]
        public async Task<ActionResult<double>> GetWipSystemToBITime(string id)
        {
            double Time = 0;
            IQueryable<WipSystem> q = _context.WipSystems;
            var wipSystem = await q.Where(p => p.WipNo == id).FirstOrDefaultAsync();

            if (wipSystem != null)
            {
                try
                {
                    if (!string.IsNullOrWhiteSpace(wipSystem.BiTime))
                        Time = double.Parse(wipSystem.BiTime);

                }
                catch
                {
                    Time = -1;
                }

            }

            return Time;
        }

        /// <summary>
        /// 新增工單資訊-系統組裝工程資訊
        /// </summary>
        /// <param name="wipSystem"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<WipSystem>> PostWipSystem([FromBody] WipSystem wipSystem)
        {
            ResultModel<WipSystem> result = new ResultModel<WipSystem>();
            _context.WipSystems.Add(wipSystem);
            try
            {
                await _context.SaveChangesAsync();
                result.Success = true;
                result.Msg = "OK";
            }
            catch (DbUpdateConcurrencyException ex)
            {
                result.Success = false;
                result.Msg = ex.InnerException.Message;
            }
            return result;
        }

        /// <summary>
        /// 修改工單資訊-系統組裝工程資訊
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<WipSystem>> PutWipSystem([FromBody] WipSystem wipSystem)
        {
            ResultModel<WipSystem> result = new ResultModel<WipSystem>();
            var wipNo = wipSystem.WipNo;
            try
            {
                if (_context.WipSystems.Any(e => e.WipNo == wipNo))
                {
                    _context.Entry(wipSystem).State = EntityState.Modified;
                    _context.Entry<WipSystem>(wipSystem).Property("CreateDate").IsModified = false;
                    _context.Entry<WipSystem>(wipSystem).Property("CreateUserID").IsModified = false;
                    wipSystem.UpdateDate = DateTime.Now;
                }
                else
                {
                    _context.WipSystems.Add(wipSystem);
                }

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

        // DELETE api/<WipSystemController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}