private Grid GenerateWeek(TimetableStructureWeek structureWeek)
        {
            Grid gridWeek = new Grid()
            {
                VerticalAlignment   = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Center,
                Margin = new Thickness(0, 5, 10, 5)
            };

            Grid.SetIsSharedSizeScope(gridWeek, true);
            gridWeek.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = new GridLength(1, GridUnitType.Auto)
            });
            gridWeek.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = new GridLength(1, GridUnitType.Auto)
            });
            gridWeek.RowDefinitions.Add(new RowDefinition());        // set up container grid
            Border weekHeader = SetInternalBorder(new EditableText() // create the week name
            {
                Text          = structureWeek.Name,
                Background    = WHITE,
                Padding       = new Thickness(2),
                TextAlignment = TextAlignment.Center
            });

            weekHeader.BorderThickness = new Thickness(1);
            gridWeek.Insert(weekHeader, 0, 1);


            for (int day = 0; day < structureWeek.DayNames.Count; day++)
            {
                ColumnDefinition columnDay = new ColumnDefinition()
                {
                    SharedSizeGroup = "A"
                };
                gridWeek.ColumnDefinitions.Add(columnDay);

                EditableText dayHeading = new EditableText() // add the day name
                {
                    Text                = structureWeek.DayNames[day],
                    Background          = WHITE,
                    Padding             = new Thickness(2),
                    Tag                 = gridWeek,
                    HorizontalAlignment = HorizontalAlignment.Center
                };
                Border dayBorder = SetInternalBorder(dayHeading);
                dayBorder.BorderThickness = new Thickness(0, 1, 1, 1);
                gridWeek.Insert(dayBorder, 0, day + 2);
            }

            for (int period = 0; period < structureWeek.PeriodNames.Count; period++)
            {
                RowDefinition rowPeriod = new RowDefinition()
                {
                    //Height = new GridLength(1, GridUnitType.Star)
                };
                gridWeek.RowDefinitions.Add(rowPeriod);
                EditableText periodHeading = new EditableText() // add the period names
                {
                    Text       = structureWeek.PeriodNames[period],
                    Background = WHITE,
                    Padding    = new Thickness(2),
                    Tag        = gridWeek
                };
                Border periodBorder = SetInternalBorder(periodHeading);
                periodBorder.BorderThickness = new Thickness(1, 0, 1, 1);
                gridWeek.Insert(periodBorder, period + 1, 1);
                gridWeek.Children.Add(GenerateCross(period + 1));
                for (int day = 0; day < structureWeek.DayNames.Count; day++)
                {
                    bool      schedulable = structureWeek.PeriodIsSchedulable(day, period);
                    Rectangle rect        = new Rectangle() // add the individual period
                    {
                        Fill = schedulable ? GREEN : A8GRAY,
                        Tag  = schedulable
                    };
                    rect.MouseLeftButtonDown += RectLeftClick;
                    rect.MouseEnter          += MouseEntered; // add the appropriate mouse bindings
                    Border rectBorder = SetInternalBorder(rect);
                    gridWeek.Insert(rectBorder, period + 1, day + 2);
                }
            }
            Button newPeriod = new Button() // add the button that adds a new period
            {
                Content = new Image()
                {
                    Source = (ImageSource)Application.Current.Resources["PlusIcon"]
                },
                Tag = gridWeek,
                VerticalAlignment   = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Center,
                Height = 20,
                Margin = new Thickness(5)
            };

            newPeriod.Click += NewPeriodClick;
            gridWeek.RowDefinitions.Add(new RowDefinition()
            {
                Height = new GridLength(1, GridUnitType.Auto)
            });
            gridWeek.Insert(newPeriod, -1, 0);
            Image removeWeek = new Image() // add the image that removes the week
            {
                Source = (ImageSource)Application.Current.Resources["BlackBinIcon"],
                HorizontalAlignment = HorizontalAlignment.Center,
                Height = 30,
                Cursor = Cursors.Hand
            };

            Grid.SetColumnSpan(removeWeek, structureWeek.DayNames.Count + 1);
            gridWeek.Insert(removeWeek, -1, 1);
            removeWeek.MouseDown += RemoveWeekClick;
            return(gridWeek);
        }
        private void SaveData()
        {
            if (ShowWarning && ShowWarningBox("All data related to the timetable will be deleted. Are you sure you want to continue?") == MessageBoxResult.Cancel)
            {
                return;
            }
            string         weekName              = null;
            IList <string> periods               = new List <string>();
            IList <string> days                  = new List <string>();
            IList <int>    uv_periods            = new List <int>();
            IList <TimetableStructureWeek> weeks = new List <TimetableStructureWeek>();

            foreach (UIElement grid_element in spWeeks.Children)
            {
                periods.Clear();
                days.Clear();
                uv_periods.Clear(); //resets the lists for each iteration
                if (!(grid_element is Grid grid))
                {
                    continue;
                }
                foreach (UIElement parent_element in grid.Children)
                {
                    int row = Grid.GetRow(parent_element), column = Grid.GetColumn(parent_element) - 1;
                    if (!(parent_element is Border border))
                    {
                        continue;
                    }
                    UIElement element = border.Child;
                    if (row == 0)
                    {
                        if (column == 0)
                        {
                            weekName = ((EditableText)element).Text;
                            continue;
                        }
                        days.InsertDefaultIndex(column - 1, ((EditableText)element).Text);
                        continue;
                    }
                    if (column == 0)
                    {
                        periods.InsertDefaultIndex(row - 1, ((EditableText)element).Text);
                        continue;
                    }
                    if (!(bool)((FrameworkElement)element).Tag)
                    {
                        uv_periods.Add(column - 1);
                        uv_periods.Add(row - 1);
                    }
                }
                int num_periods = periods.Count;
                weeks.Add(new TimetableStructureWeek(weekName, new List <string>(days), new List <string>(periods),                                                                                                      // adds the correct weeks to the list
                                                     Enumerable.Range(0, uv_periods.Count / 2).Select(i => TimetableStructureWeek.IndexesToPeriodNum(uv_periods[2 * i], uv_periods[2 * i + 1], num_periods)).ToList())); // generates the days and the periods
            }
            App.Data.SetTimetableStructure(weeks);                                                                                                                                                                       // set the current structure data
        }