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 SolderPasteRecordController : ControllerBase
    {
        private readonly AMESContext _context;

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

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

        // GET: api/SolderPasteRecord/5
        [HttpGet("{id}")]
        public async Task<ActionResult<SolderPasteRecord>> GetSolderPasteRecord(int id)
        {
            var solderPasteRecord = await _context.SolderPasteRecords.FindAsync(id);

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

            return solderPasteRecord;
        }

        /// <summary>
        /// 更新錫膏使用紀錄資料檔
        /// </summary>
        /// <param name="solderPasteRecord"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<SolderPasteRecord>> PutSolderPasteRecord(SolderPasteRecord solderPasteRecord)
        {
            ResultModel<SolderPasteRecord> result = new ResultModel<SolderPasteRecord>();
            _context.Entry(solderPasteRecord).State = EntityState.Modified;
            solderPasteRecord.UpdateDate = DateTime.Now;
            solderPasteRecord.UpdateUserID = 0;

            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="solderPasteRecord"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<SolderPasteRecord>> PostSolderPasteRecord(SolderPasteRecord solderPasteRecord)
        {
            ResultModel<SolderPasteRecord> result = new ResultModel<SolderPasteRecord>();
            Helper helper = new Helper(_context);
            solderPasteRecord.SpRecordID = helper.GetIDKey("SP_RECORD_ID").Result;
            _context.SolderPasteRecords.Add(solderPasteRecord);
            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/SolderPasteRecord/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<SolderPasteRecord>> DeleteSolderPasteRecord(int id)
        {
            var solderPasteRecord = await _context.SolderPasteRecords.FindAsync(id);
            if (solderPasteRecord == null)
            {
                return NotFound();
            }

            _context.SolderPasteRecords.Remove(solderPasteRecord);
            await _context.SaveChangesAsync();

            return solderPasteRecord;
        }

        private bool SolderPasteRecordExists(int id)
        {
            return _context.SolderPasteRecords.Any(e => e.SpRecordID == id);
        }
    }
}