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

namespace AMESCoreStudio.WebApi.Controllers.AMES
{
    /// <summary>
    /// 錫膏基本資料檔
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class SolderPasteInfoController : ControllerBase
    {
        private readonly AMESContext _context;

        public SolderPasteInfoController(AMESContext context)
        {
            _context = context;
        }

        // GET: api/SolderPasteInfo
        [HttpGet]
        public async Task<ActionResult<IEnumerable<SolderPasteInfo>>> GetSolderPasteInfos()
        {
            return await _context.SolderPasteInfos.ToListAsync();
        }

        // GET: api/SolderPasteInfo/5
        [HttpGet("{id}")]
        public async Task<ActionResult<SolderPasteInfo>> GetSolderPasteInfo(int id)
        {
            var solderPasteInfo = await _context.SolderPasteInfos.FindAsync(id);

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

            return solderPasteInfo;
        }

        /// <summary>
        /// 錫膏資料查詢
        /// </summary>
        /// <param name="solderPasteNo">錫膏編號</param>
        /// <param name="status">狀態</param>
        /// <param name="page">頁數</param>
        /// <param name="limit">筆數</param>
        /// <returns></returns>
        [HttpGet("SolderPasteInfoQuery")]
        public async Task<ResultModel<SolderPasteInfoDto>> GetSolderPasteInfo(string solderPasteNo , string status, int page = 0, int limit = 10)
        {
            var q = await _context.SolderPasteInfos.ToListAsync();

            if (!string.IsNullOrWhiteSpace(solderPasteNo))
            {
                q = q.Where(w => w.SolderPasteNo.Contains(solderPasteNo)).ToList();
            }

            //if (!string.IsNullOrWhiteSpace(pcbPartNo))
            //{
            //    q = q.Where(w => w.PcbPartNo.Contains(pcbPartNo)).ToList();
            //}

            //if (!string.IsNullOrWhiteSpace(side))
            //{
            //    q = q.Where(w => w.Side.ToString() == side).ToList();
            //}

            if (!string.IsNullOrWhiteSpace(status))
            {
                q = q.Where(w => w.Status == status).ToList();

            }
            ResultModel<SolderPasteInfoDto> result = new ResultModel<SolderPasteInfoDto>();

            // Table 頁數
            if (page > 0)
            {
                q = q.Skip((page - 1) * limit).Take(limit).ToList();
            }

            // 紀錄筆數
            result.DataTotal = q.Count();

            result.Data = q.Select(s => new SolderPasteInfoDto
            {
                SolderPasteID = s.SolderPasteID,
                SolderPasteNo = s.SolderPasteNo,
                Description = s.Description,
                EffectiveDate = s.EffectiveDate,
                ManufactoringDate = s.ManufactoringDate,
                ReceiptDate = s.ReceiptDate,
                StatusName = s.Status == "0" ? "失效" : "有效",
                Remark = s.Remark
            }).ToList();
            return result;
        }

        /// <summary>
        /// 更新錫膏基本資料檔
        /// </summary>
        /// <param name="solderPasteInfo"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<SolderPasteInfo>> PutSolderPasteInfo(SolderPasteInfo solderPasteInfo)
        {
            ResultModel<SolderPasteInfo> result = new ResultModel<SolderPasteInfo>();
            _context.Entry(solderPasteInfo).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="solderPasteInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<SolderPasteInfo>> PostSolderPasteInfo(SolderPasteInfo solderPasteInfo)
        {
            ResultModel<SolderPasteInfo> result = new ResultModel<SolderPasteInfo>();
            Helper helper = new Helper(_context);
            solderPasteInfo.SolderPasteID = helper.GetIDKey("SOLDER_PASTE_ID").Result;
            _context.SolderPasteInfos.Add(solderPasteInfo);
            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/SolderPasteInfo/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<SolderPasteInfo>> DeleteSolderPasteInfo(int id)
        {
            var solderPasteInfo = await _context.SolderPasteInfos.FindAsync(id);
            if (solderPasteInfo == null)
            {
                return NotFound();
            }

            _context.SolderPasteInfos.Remove(solderPasteInfo);
            await _context.SaveChangesAsync();

            return solderPasteInfo;
        }

        private bool SolderPasteInfoExists(int id)
        {
            return _context.SolderPasteInfos.Any(e => e.SolderPasteID == id);
        }
    }
}