public void AddEventGroup(EventGroup eg)
        {
            var g = EventGroups.ToSyncArray().FirstOrDefault(x => x.Name == eg.Name);

            if (g != null)
            {
                foreach (var ev in eg.Events)
                {
                    g.AddEvent(ev);
                }
            }
            else
            {
                EventGroups.Add(eg);
            }
        }
        private void LoadEventFile(DayOfWeek today, string region)
        {
            var yesterday = today - 1;

            if (region.StartsWith("EU"))
            {
                region = "EU";
            }
            var path = $"resources/config/events/events-{region}.xml";

            if (!File.Exists(path))
            {
                var root = new XElement("Events");
                var eg   = new XElement("EventGroup", new XAttribute("name", "Example event group"));
                var ev   = new XElement("Event",
                                        new XAttribute("name", "Example Event"),
                                        new XAttribute("days", "*"),
                                        new XAttribute("start", "12:00"),
                                        new XAttribute("end", "15:00"),
                                        new XAttribute("color", "ff5566"));
                var ev2 = new XElement("Event",
                                       new XAttribute("name", "Example event 2"),
                                       new XAttribute("days", "*"),
                                       new XAttribute("start", "16:00"),
                                       new XAttribute("duration", "3:00"),
                                       new XAttribute("color", "ff5566"));
                eg.Add(ev);
                eg.Add(ev2);
                root.Add(eg);
                if (!Directory.Exists($"resources/config/events"))
                {
                    Directory.CreateDirectory($"resources/config/events");
                }
                root.Save(path);
            }

            try
            {
                var d = XDocument.Load(path);
                foreach (var egElement in d.Descendants().Where(x => x.Name == "EventGroup"))
                {
                    var egName  = egElement.Attribute("name").Value;
                    var egRc    = egElement.Attribute("remote") != null && bool.Parse(egElement.Attribute("remote").Value);
                    var egStart = egElement.Attribute("start") != null
                        ? DateTime.Parse(egElement.Attribute("start").Value)
                        : DateTime.MinValue;

                    var egEnd = egElement.Attribute("end") != null
                        ? DateTime.Parse(egElement.Attribute("end").Value).AddDays(1)
                        : DateTime.MaxValue;

                    if (TimeManager.Instance.CurrentServerTime < egStart ||
                        TimeManager.Instance.CurrentServerTime > egEnd)
                    {
                        continue;
                    }

                    var eg = new EventGroup(egName, egStart, egEnd, egRc);
                    foreach (var evElement in egElement.Descendants().Where(x => x.Name == "Event"))
                    {
                        var isYesterday = false;
                        var isToday     = false;

                        if (evElement.Attribute("days").Value != "*")
                        {
                            if (evElement.Attribute("days").Value.Contains(','))
                            {
                                var days = evElement.Attribute("days").Value.Split(',');
                                foreach (var dayString in days)
                                {
                                    var day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), dayString);
                                    if (day == today)
                                    {
                                        isToday = true;
                                    }
                                    if (day == yesterday)
                                    {
                                        isYesterday = true;
                                    }
                                }
                            }
                            else
                            {
                                var eventDay = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), evElement.Attribute("days").Value);
                                isToday     = eventDay == today;
                                isYesterday = eventDay == yesterday;
                            }
                        }
                        else
                        {
                            isToday     = true;
                            isYesterday = true;
                        }

                        if (!isToday && !isYesterday)
                        {
                            continue;
                        }

                        var  name           = evElement.Attribute("name").Value;
                        var  parsedStart    = DateTime.Parse(evElement.Attribute("start").Value, CultureInfo.InvariantCulture);
                        var  parsedDuration = TimeSpan.Zero;
                        var  parsedEnd      = DateTime.Now;
                        bool isDuration;
                        if (evElement.Attribute("duration") != null)
                        {
                            parsedDuration = TimeSpan.Parse(evElement.Attribute("duration").Value, CultureInfo.InvariantCulture);
                            isDuration     = true;
                        }
                        else if (evElement.Attribute("end") != null)
                        {
                            parsedEnd  = DateTime.Parse(evElement.Attribute("end").Value, CultureInfo.InvariantCulture);
                            isDuration = false;
                        }
                        else
                        {
                            parsedDuration = TimeSpan.Zero;
                            parsedEnd      = parsedStart;
                            isDuration     = true;
                        }

                        var color = "5599ff";

                        var start = parsedStart.Hour + parsedStart.Minute / 60D;
                        var end   = isDuration ? parsedDuration.Hours + parsedDuration.Minutes / 60D : parsedEnd.Hour + parsedEnd.Minute / 60D;

                        if (evElement.Attribute("color") != null)
                        {
                            color = evElement.Attribute("color").Value;
                        }
                        if (isYesterday)
                        {
                            if (!EventUtils.EndsToday(start, end, isDuration))
                            {
                                var e1 = new DailyEvent(name, parsedStart.Hour, 24, 0, color, false);
                                end = start + end - 24;
                                var e2 = new DailyEvent(name, parsedStart.Hour, parsedStart.Minute, end, color, isDuration);
                                if (isToday)
                                {
                                    eg.AddEvent(e1);
                                }
                                eg.AddEvent(e2);
                            }
                            else if (isToday)
                            {
                                var ev = new DailyEvent(name, parsedStart.Hour, parsedStart.Minute, end, color, isDuration);
                                eg.AddEvent(ev);
                            }
                        }
                        else
                        {
                            var ev = new DailyEvent(name, parsedStart.Hour, parsedStart.Minute, end, color, isDuration);
                            eg.AddEvent(ev);
                        }
                    }
                    if (eg.Events.Count != 0)
                    {
                        AddEventGroup(eg);
                    }
                }
                SpecialEvents.Add(new DailyEvent("Reset", TimeManager.Instance.ResetHour, 0, 0, "ff0000"));
            }
            catch (Exception)
            {
                var res = TccMessageBox.Show("TCC", $"There was an error while reading events-{region}.xml. Manually correct the error and and press Ok to try again, else press Cancel to build a default config file.", MessageBoxButton.OKCancel);

                if (res == MessageBoxResult.Cancel)
                {
                    File.Delete(path);
                }
                LoadEventFile(today, region);
            }
        }