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;
using System.Transactions;

namespace FMSApp.Repositories {
    public class HolidayRepository {
        private readonly ILogger<HolidayRepository> _logger;
        private readonly FMSContext _context;

        private readonly AppSettings _appSettings;
        private readonly StorageHelper _storage;

        public HolidayRepository(
            ILogger<HolidayRepository> logger,
            FMSContext context,
            IOptions<AppSettings> appSettings,
            StorageHelper storage
            ) {
            _logger = logger;
            _context = context;
            _appSettings = appSettings.Value;
            _storage = storage;
        }

        public IList<DateTime> GetHolidays(int siteId) {
            var list = new List<DateTime>();

            var holidays = _context.CmHoliday.Where(
                x => x.SiteId == siteId
                && x.IsUse == true
            );

            int year = DateTime.Now.Year;

            foreach (var item in holidays) {
                if (item.IsLunar) {
                    var solar = LunarSolarConverter.LunarToSolar(new Lunar() {
                        lunarYear = year,
                        lunarMonth = item.HolidayMonth,
                        lunarDay = item.HolidayDay
                    });
                    list.Add(new DateTime(solar.solarYear, solar.solarMonth, solar.solarDay));
                } else {
                    list.Add(new DateTime(year, item.HolidayMonth, item.HolidayDay));
                }
            }

            var customs = _context.CmHolidayCustom.Where(
                x => x.SiteId == siteId
                && x.HolidayDate >= DateTime.Now
                && x.IsUse == true
            );
            foreach (var item in customs) {
                list.Add(item.HolidayDate);
            }

            return list;
        }

        public bool IsHoliday(DateTime date, CmHolidayWeekend weekend, IList<DateTime> holidays) {
            if (date.DayOfWeek == DayOfWeek.Saturday && weekend.Saturday) return true;
            if (date.DayOfWeek == DayOfWeek.Sunday && weekend.Sunday) return true;

            if (holidays.Where(x => x.Month == date.Month && x.Day == date.Day).Any()) {
                return true;
            }

            return false;
        }

        public CmHolidayWeekend GetHolidayWeekend(int siteId) {
            var result = _context.CmHolidayWeekend.FirstOrDefault(
                x => x.SiteId == siteId
            );

            if (result == null) {
                result = new CmHolidayWeekend();
                result.Saturday = true;
                result.Sunday = true;
            }

            return result;
        }

    }
}