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 HolidayService {
        private readonly ILogger<HolidayService> _logger;
        private readonly FMSContext _context;

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

        public IQueryable<CmHoliday> GetAll(int siteId) {
            var data = _context.CmHoliday.Where(x => x.SiteId == siteId && x.IsUse == true);
            return data;
        }

        public IQueryable<CmHolidayCustom> CustomGetAll(int siteId) {
            var data = _context.CmHolidayCustom.Where(x => x.SiteId == siteId && x.IsUse == true);
            return data;
        }

        public IQueryable<CmHolidayWeekend> WeekendGetAll(int siteId) {
            var data = _context.CmHolidayWeekend.Where(x => x.SiteId == siteId && x.IsUse == true);
            return data;
        }

        public IQueryable<CmHoliday> Gets(int month, int day, int siteId) {
            var data = _context.CmHoliday.Where(x => x.SiteId == siteId && x.HolidayMonth == month && x.HolidayDay == day && x.IsUse == true);
            return data;
        }

        public IQueryable<CmHolidayCustom> CustomGets(string dates, int siteId) {
            DateTime dt = Convert.ToDateTime(dates.Substring(0, 4) + "-" + dates.Substring(4, 2) + "-" + dates.Substring(6, 2));
            var data = _context.CmHolidayCustom.Where(x => x.SiteId == siteId && x.HolidayDate == dt && x.IsUse == true);
            return data;
        }

        public IQueryable<CmHolidayWeekend> WeekendGets(int siteId) {
            var data = _context.CmHolidayWeekend.Where(x => x.SiteId == siteId && x.IsUse == true);
            return data;
        }

        public void Create(string dates, CmHoliday data) {
            string[] arr = dates.Split('-');
            data.HolidayMonth = Convert.ToInt16(arr[1]);
            data.HolidayDay = Convert.ToInt16(arr[2]);
            data.IsUse = true;
            _context.CmHoliday.Add(data);
            _context.SaveChanges();
        }

        public void Edit(int month, int day, int siteId, CmHoliday data) {
            var persist = _context.CmHoliday
                            .Where(x => x.HolidayMonth == month && x.HolidayDay == day && x.SiteId == siteId).FirstOrDefault();
            if (persist == null) {
                throw new ServiceException("정보를 찾을 수 없습니다.");
            }

            persist.SiteId = data.SiteId;
            persist.HolidayMonth = data.HolidayMonth;
            persist.HolidayDay = data.HolidayDay;
            persist.Name = data.Name;
            _context.CmHoliday.Update(persist);
            _context.SaveChanges();
        }

        public void Delete(string dates, int siteId) {
            string[] arr = dates.Split('-');
            short month = Convert.ToInt16(arr[0]);
            short day = Convert.ToInt16(arr[1]);
            var data = _context.CmHoliday.First(x => x.HolidayMonth == month && x.HolidayDay == day && x.SiteId == siteId);
            _context.CmHoliday.Remove(data);
            _context.SaveChanges();
        }
    }
}