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

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

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        // GET: api/NGGroups
        [HttpGet]
        public async Task<ActionResult<IEnumerable<NGGroup>>> GetNGGroup(int page = 0, int limit = 10)
        {
            IQueryable<NGGroup> q = _context.NGGroups;
            if (page > 0)
            {
                q = q.OrderBy(p => p.NGGroupNo).Skip((page - 1) * limit).Take(limit);
            }
            else
            {
                q = q.OrderBy(p => p.NGGroupNo);
            }

            var ngGroup = await q.ToListAsync();
            return ngGroup;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET: api/NGGroups/5
        [HttpGet("{id}")]
        public async Task<ActionResult<IEnumerable<NGGroup>>> GetNGGroup(string id)
        {
            IQueryable<NGGroup> q = _context.NGGroups;
            q = q.Where(p => p.NGGroupNo.Equals(id));

            var ngGroup = await q.ToListAsync();

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

            return ngGroup;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="nGGroup"></param>
        /// <returns></returns>
        // PUT: api/NGGroups/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<ResultModel<NGGroup>> PutNGGroup(string id, [FromBody] NGGroup nGGroup)
        {
            ResultModel<NGGroup> result = new ResultModel<NGGroup>();

            if (id != nGGroup.NGGroupNo)
            {
                result.Success = false;
                result.Msg = "群組代碼錯誤";
                return result;
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NGGroupExists(id))
                {
                    result.Success = false;
                    result.Msg = "群組代碼不存在";
                    return result;
                }
                else
                {
                    throw;
                }
            }

            result.Success = true;
            result.Msg = "OK";
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nGGroup"></param>
        /// <returns></returns>
        // POST: api/NGGroups
        // 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<ResultModel<NGGroup>> PostNGGroup([FromBody] NGGroup nGGroup)
        {
            ResultModel<NGGroup> result = new ResultModel<NGGroup>();

            _context.NGGroups.Add(nGGroup);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (NGGroupExists(nGGroup.NGGroupNo))
                {
                    result.Success = false;
                    result.Msg = "群組代碼重複";
                    return result;
                }
                else
                {
                    throw;
                }
            }

            result.Success = true;
            result.Msg = "OK";

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // DELETE: api/NGGroups/5
        [HttpDelete("{id}")]
        public async Task<ResultModel<NGGroup>> DeleteNGGroup(string id)
        {
            ResultModel<NGGroup> result = new ResultModel<NGGroup>();

            var nGGroup = await _context.NGGroups.FindAsync(id);
            if (nGGroup == null)
            {
                result.Success = false;
                result.Msg = "群組代碼不存在";
                return result;
            }

            _context.NGGroups.Remove(nGGroup);
            await _context.SaveChangesAsync();

            result.Success = true;
            result.Msg = "OK";

            return result;
        }

        private bool NGGroupExists(string id)
        {
            return _context.NGGroups.Any(e => e.NGGroupNo == id);
        }
    }
}