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 HolidayWeekendController : Controller {
        private readonly ILogger<HolidayWeekendController> _logger;
        private readonly FMSContext _context;
        private readonly HolidayWeekendService _service;
        private readonly StorageHelper _storage;

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

        /// <summary>
        /// 목록
        /// </summary>
        [HttpGet("{siteId}")]
        public IActionResult Get(string siteId) {
            var data = _service.Gets(Util.ToInt(siteId));
            var sYear = DateTime.Today.Year.ToString();

            if (data == null || data.Count() == 0) {
                var item = new CmHolidayWeekend {
                    SiteId = Util.ToInt(siteId),
                    Site = new CmSite(),
                    Saturday = false,
                    Sunday = false,
                    IsUse = true,
                };

                return Ok(item);
            } else {
                var item = data.Select(x => new {
                    x.SiteId,
                    Site = new {
                        x.Site.Name,
                    },
                    x.Saturday,
                    x.Sunday,
                    x.IsUse,
                });

                return Ok(item.First());
            }
        }

        /// <summary>
        /// 수정
        /// </summary>
        [HttpPost("{siteId}")]
        public IActionResult Save(int siteId, [FromBody] CmHolidayWeekend data) {
            if (ModelState.IsValid) {
                _service.Save(siteId, data);
            } else {
                return BadRequest(ModelState);
            }

            return Ok();
        }
    }
}