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

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

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

        public void Save(int siteId, CmHolidayWeekend data) {
            var persist = _context.CmHolidayWeekend
                            .Where(x => x.SiteId == data.SiteId).FirstOrDefault();
            if (persist == null || siteId != data.SiteId) {
                //현장ID가 바뀌면 바뀐 현장으로 새로 INSERT
                data.IsUse = true;
                _context.CmHolidayWeekend.Add(data);
                _context.SaveChanges();
            } else {
                persist.SiteId = data.SiteId;
                persist.Saturday = data.Saturday;
                persist.Sunday = data.Sunday;
                _context.CmHolidayWeekend.Update(persist);
                _context.SaveChanges();
            }
        }
    }
}