using System;
using System.Linq;
using System.Threading.Tasks;

using FMSAdmin.Data;
using FMSAdmin.Models;
using FMSAdmin.Entities;
using FMSAdmin.Services;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using FMSAdmin.Helpers;
using System.Data;
using System.IO;
using OfficeOpenXml;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Collections;

namespace FMSAdmin.Controllers {
    [Authorize]
    [ApiController]
    [ApiVersion("1")]
    [Route("api/[controller]")]
    public class HolidayCustomController : Controller {
        private readonly ILogger<HolidayCustomController> _logger;
        private readonly FMSContext _context;
        private readonly HolidayCustomService _service;
        private readonly StorageHelper _storage;

        public HolidayCustomController(
            ILogger<HolidayCustomController> logger,
            FMSContext context,
            HolidayCustomService service,
            StorageHelper storage
            ) {
            _logger = logger;
            _context = context;
            _service = service;
            _storage = storage;
        }

        /// <summary>
        /// 등록
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPut("{dates}")]
        public IActionResult Create(string dates, [FromBody] CmHolidayCustom data) {
            _logger.LogInformation("Create");
            //_logger.LogInformation(data.Dump());

            if (ModelState.IsValid) {
                _service.Create(dates, data);
            } else {
                return BadRequest(ModelState);
            }
            return Ok();
        }

        /// <summary>
        /// 수정
        /// </summary>
        [HttpPost("{siteId}/{dates}")]
        public IActionResult Edit(int siteId, string dates, [FromBody] CmHolidayCustom data) {
            if (dates != data.HolidayDate.ToString("yyyyMMdd")) {
                return BadRequest("잘못된 접근입니다.");
            }

            if (ModelState.IsValid) {
                _service.Edit(dates, siteId, data);
            } else {
                return BadRequest(ModelState);
            }

            return Ok();
        }

        /// <summary>
        /// 삭제
        /// </summary>
        [HttpDelete("{siteId}/{dates}")]
        public IActionResult Delete(int siteId, string dates) {
            _service.Delete(dates, siteId);

            return Ok();
        }
    }
}