<commit_before>﻿using Nostrum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using TCC.Interop;
using TCC.UI;
using TCC.UI.Windows;
using TCC.Utilities;
using TCC.Utils;
using TCC.ViewModels;
using TeraDataLite;
using MessageBoxImage = TCC.Data.MessageBoxImage;

namespace TCC
{
    //TODO: big refactor here
    public class GameEventManager : TSPropertyChanged
    {
        private static GameEventManager _instance;
        public static GameEventManager Instance => _instance ??= new GameEventManager();

        // TODO: not sure about other regions reset days
        private readonly Dictionary<RegionEnum, TeraServerTimeInfo> _serverTimezones = new Dictionary<RegionEnum, TeraServerTimeInfo>
        {
            { RegionEnum.EU,  new TeraServerTimeInfo("Central Europe Standard Time", 6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
            { RegionEnum.NA,  new TeraServerTimeInfo("Central Standard Time",        6, DayOfWeek.Tuesday,   DayOfWeek.Thursday) },
            { RegionEnum.RU,  new TeraServerTimeInfo("Russian Standard Time",        6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
            { RegionEnum.TW,  new TeraServerTimeInfo("China Standard Time",          6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
            { RegionEnum.JP,  new TeraServerTimeInfo("Tokyo Standard Time",          6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
            { RegionEnum.THA, new TeraServerTimeInfo("Indochina Time",               6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
            { RegionEnum.KR,  new TeraServerTimeInfo("Korea Standard Time",          6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
        };

        public const double SecondsInDay = 60 * 60 * 24;
        private const string BaseUrl = "https://tcc-web-99a64.firebaseapp.com/bam";

        public int ResetHour;
        public RegionEnum CurrentRegion { get; set; }
        public int ServerHourOffsetFromLocal;
        public int ServerHourOffsetFromUtc;

        public DateTime CurrentServerTime => DateTime.Now.AddHours(ServerHourOffsetFromLocal);

        private GameEventManager()
        {
            Dispatcher = Dispatcher.CurrentDispatcher;
            var s = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
            s.Tick += CheckNewDay;
            s.Start();
        }

        private void CheckCloseEvents()
        {
            var closeEventsCount = WindowManager.ViewModels.DashboardVM.EventGroups.Count(evGroup => evGroup.Events.Any(x => x.IsClose));
            if (closeEventsCount == 0) return;
            if (App.Settings.FloatingButtonSettings.ShowNotificationBubble) WindowManager.ViewModels.FloatingButtonVM.WarnCloseEvents(closeEventsCount);

        }

        private void CheckNewDay(object sender, EventArgs e)
        {
            if (CurrentServerTime.Hour == 0 && CurrentServerTime.Minute == 0)
                WindowManager.ViewModels.DashboardVM.LoadEvents(CurrentServerTime.DayOfWeek, CurrentRegion.ToString());
            if (CurrentServerTime.Second == 0 && CurrentServerTime.Minute % 3 == 0)
                CheckCloseEvents();
        }

        public void SetServerTimeZone(string lang)
        {
            if (string.IsNullOrEmpty(lang)) return;
            CurrentRegion = TccUtils.RegionEnumFromLanguage(lang);// region.StartsWith("EU") ? "EU" : region;

            //App.Settings.LastLanguage = lang; //TODO: needed?
            if (!_serverTimezones.ContainsKey(CurrentRegion))
            {
                CurrentRegion = RegionEnum.EU;
                App.Settings.LastLanguage = "EU-EN";
                TccMessageBox.Show("TCC",
                    "Current region could not be detected, so TCC will load EU-EN database. To force a specific language, use Region Override setting in Misc Settings.",
                    MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            var timezone = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(x => x.Id == _serverTimezones[CurrentRegion].Timezone);
            ResetHour = _serverTimezones[CurrentRegion].ResetHour;

            if (timezone != null)
            {
                ServerHourOffsetFromUtc = timezone.IsDaylightSavingTime(DateTime.UtcNow + timezone.BaseUtcOffset)
                    ? timezone.BaseUtcOffset.Hours + 1
                    : timezone.BaseUtcOffset.Hours;
                ServerHourOffsetFromLocal = -TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Hours + ServerHourOffsetFromUtc;
            }
<commit_msg>switch from TimeZone to TimeZoneInfo<commit_after>﻿using Nostrum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using TCC.Interop;
using TCC.UI;
using TCC.UI.Windows;
using TCC.Utilities;
using TCC.Utils;
using TCC.ViewModels;
using TeraDataLite;
using MessageBoxImage = TCC.Data.MessageBoxImage;

namespace TCC
{
    //TODO: big refactor here
    public class GameEventManager : TSPropertyChanged
    {
        private static GameEventManager _instance;
        public static GameEventManager Instance => _instance ??= new GameEventManager();

        // TODO: not sure about other regions reset days
        private readonly Dictionary<RegionEnum, TeraServerTimeInfo> _serverTimezones = new Dictionary<RegionEnum, TeraServerTimeInfo>
        {
            { RegionEnum.EU,  new TeraServerTimeInfo("Central Europe Standard Time", 6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
            { RegionEnum.NA,  new TeraServerTimeInfo("Central Standard Time",        6, DayOfWeek.Tuesday,   DayOfWeek.Thursday) },
            { RegionEnum.RU,  new TeraServerTimeInfo("Russian Standard Time",        6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
            { RegionEnum.TW,  new TeraServerTimeInfo("China Standard Time",          6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
            { RegionEnum.JP,  new TeraServerTimeInfo("Tokyo Standard Time",          6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
            { RegionEnum.THA, new TeraServerTimeInfo("Indochina Time",               6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
            { RegionEnum.KR,  new TeraServerTimeInfo("Korea Standard Time",          6, DayOfWeek.Wednesday, DayOfWeek.Thursday) },
        };

        public const double SecondsInDay = 60 * 60 * 24;
        private const string BaseUrl = "https://tcc-web-99a64.firebaseapp.com/bam";

        public int ResetHour;
        public RegionEnum CurrentRegion { get; set; }
        public int ServerHourOffsetFromLocal;
        public int ServerHourOffsetFromUtc;

        public DateTime CurrentServerTime => DateTime.Now.AddHours(ServerHourOffsetFromLocal);

        private GameEventManager()
        {
            Dispatcher = Dispatcher.CurrentDispatcher;
            var s = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
            s.Tick += CheckNewDay;
            s.Start();
        }

        private void CheckCloseEvents()
        {
            var closeEventsCount = WindowManager.ViewModels.DashboardVM.EventGroups.Count(evGroup => evGroup.Events.Any(x => x.IsClose));
            if (closeEventsCount == 0) return;
            if (App.Settings.FloatingButtonSettings.ShowNotificationBubble) WindowManager.ViewModels.FloatingButtonVM.WarnCloseEvents(closeEventsCount);

        }

        private void CheckNewDay(object sender, EventArgs e)
        {
            if (CurrentServerTime.Hour == 0 && CurrentServerTime.Minute == 0)
                WindowManager.ViewModels.DashboardVM.LoadEvents(CurrentServerTime.DayOfWeek, CurrentRegion.ToString());
            if (CurrentServerTime.Second == 0 && CurrentServerTime.Minute % 3 == 0)
                CheckCloseEvents();
        }

        public void SetServerTimeZone(string lang)
        {
            if (string.IsNullOrEmpty(lang)) return;
            CurrentRegion = TccUtils.RegionEnumFromLanguage(lang);// region.StartsWith("EU") ? "EU" : region;

            //App.Settings.LastLanguage = lang; //TODO: needed?
            if (!_serverTimezones.ContainsKey(CurrentRegion))
            {
                CurrentRegion = RegionEnum.EU;
                App.Settings.LastLanguage = "EU-EN";
                TccMessageBox.Show("TCC",
                    "Current region could not be detected, so TCC will load EU-EN database. To force a specific language, use Region Override setting in Misc Settings.",
                    MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            var timezone = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(x => x.Id == _serverTimezones[CurrentRegion].Timezone);
            ResetHour = _serverTimezones[CurrentRegion].ResetHour;

            if (timezone != null)
            {
                ServerHourOffsetFromUtc = timezone.IsDaylightSavingTime(DateTime.UtcNow + timezone.BaseUtcOffset)
                    ? timezone.BaseUtcOffset.Hours + 1
                    : timezone.BaseUtcOffset.Hours;
                ServerHourOffsetFromLocal = -TimeZoneInfo.Local.GetUtcOffset(DateTime.Now).Hours + ServerHourOffsetFromUtc;
            }

{'fraction_non_alphanumeric': 0.09070343638909835, 'fraction_numerical': 0.00538618980932888, 'mean_word_length': 2.665219107777339, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1655632', 'n_tokens_mistral': 2637, 'n_tokens_neox': 2500, 'n_words': 552}