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

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

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

        // GET: api/FqcResultMasterBlob/5
        [HttpGet("{id}")]
        public async Task<ActionResult<FqcResultMasterBlob>> GetFqcResultMasterBlob(int id)
        {
            var fqcResultMasterBlob = await _context.FqcResultMasterBlobs.FindAsync(id);

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

            return fqcResultMasterBlob;
        }

        /// <summary>
        /// 檢驗結果上傳圖檔資料表 By FQCID
        /// </summary>
        /// <param name="id">FQCID</param>
        /// <returns></returns>
        [HttpGet("ByFQCID/{id}")]
        public async Task<ActionResult<IEnumerable<FqcResultMasterBlob>>> GetFqcResultMasterBlobByFqcID(int id)
        {
            var fqcResultMasterBlobs = await _context.FqcResultMasterBlobs.Where(w => w.FqcID == id).ToListAsync();
            return fqcResultMasterBlobs;
        }

        /// <summary>
        /// 更新 檢驗結果上傳圖檔資料表
        /// </summary>
        /// <param name="fqcResultMasterBlob"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<FqcResultMasterBlob>> PutFqcResultMasterBlob(FqcResultMasterBlob fqcResultMasterBlob)
        {
            ResultModel<FqcResultMasterBlob> result = new ResultModel<FqcResultMasterBlob>();
            _context.Entry(fqcResultMasterBlob).State = EntityState.Modified;
            fqcResultMasterBlob.UpdateDate = DateTime.Now;

            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="fqcResultMasterBlob"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<FqcResultMasterBlob>> PostFqcResultMasterBlob(FqcResultMasterBlob fqcResultMasterBlob)
        {
            ResultModel<FqcResultMasterBlob> result = new ResultModel<FqcResultMasterBlob>();
            fqcResultMasterBlob.CreateDate = DateTime.Now;
            fqcResultMasterBlob.UpdateDate = DateTime.Now;

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

        /// <summary>
        /// 刪除 FQC附件
        /// </summary>
        /// <param name="fqcId">fqcId</param>
        /// <param name="fileName">檔案名稱</param>
        /// <returns></returns>
        [HttpDelete("ByFileName")]
        public async Task<ResultModel<FqcResultMasterBlob>> DeleteFqcResultMasterBlob(int fqcId, string fileName)
        {
            ResultModel<FqcResultMasterBlob> result = new ResultModel<FqcResultMasterBlob>();
            if (fqcId == 0 || string.IsNullOrWhiteSpace(fileName))
            {
                result.Success = false;
                result.Msg = "請輸入要刪除的資料";
            }
            else
            {
                var fqcResultMasterBlobs = await _context.FqcResultMasterBlobs.Where(w => w.FqcID == fqcId &&
                                                                         w.ImageName.Trim().ToUpper() == fileName.Trim().ToUpper()).ToListAsync();

                if (fqcResultMasterBlobs.Count() != 0)
                {
                    try
                    {
                        _context.FqcResultMasterBlobs.Remove(fqcResultMasterBlobs.FirstOrDefault());
                        await _context.SaveChangesAsync();
                        result.Success = true;
                        result.Msg = "OK";
                    }
                    catch (Exception ex)
                    {
                        result.Success = false;
                        result.Msg = ex.InnerException.Message;
                    }
                }
            }
            return result;
        }

        private bool FqcResultMasterBlobExists(int id)
        {
            return _context.FqcResultMasterBlobs.Any(e => e.FqcID == id);
        }
    }
}