using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;

using FMSAdmin.Data;
using FMSAdmin.Helpers;
using FMSAdmin.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using FMSAdmin.Models;

namespace FMSAdmin.Services {
    public class HolidayCustomService {
        private readonly ILogger<HolidayCustomService> _logger;
        private readonly FMSContext _context;

        public HolidayCustomService(
            ILogger<HolidayCustomService> logger,
            FMSContext context) {
            _logger = logger;
            _context = context;
        }

        public void Create(string dates, CmHolidayCustom data) {
            DateTime dt = Convert.ToDateTime(dates);
            data.HolidayDate = dt;
            data.IsUse = true;
            _context.CmHolidayCustom.Add(data);
            _context.SaveChanges();
        }

        public void Edit(string HolidayDate, int siteId, CmHolidayCustom data) {
            DateTime dt = Convert.ToDateTime(HolidayDate.Substring(0, 4) + '-' + HolidayDate.Substring(4, 2) + '-' + HolidayDate.Substring(6, 2));
            var persist = _context.CmHolidayCustom
                            .Where(x => x.HolidayDate == dt && x.SiteId == siteId).FirstOrDefault();
            if (persist == null) {
                throw new ServiceException("정보를 찾을 수 없습니다.");
            }

            persist.SiteId = data.SiteId;
            persist.HolidayDate = data.HolidayDate;
            persist.Name = data.Name;
            _context.CmHolidayCustom.Update(persist);
            _context.SaveChanges();
        }

        public void Delete(string dates, int siteId) {
            DateTime dt = Convert.ToDateTime(dates.Substring(0, 4) + '-' + dates.Substring(4, 2) + '-' + dates.Substring(6, 2));
            var data = _context.CmHolidayCustom.First(x => x.HolidayDate == dt && x.SiteId == siteId);
            _context.CmHolidayCustom.Remove(data);
            _context.SaveChanges();
        }
    }
}