private void AcceptButton_Click(object sender, RoutedEventArgs e)
        {
            Accepted = true;
            try
            {
                DateTime start;
                DateTime end;
                try
                {
                    start = (DateTime)StartDatePicker.SelectedDate;
                }
                catch (Exception)
                {
                    MessageBox.Show("Please enter a valid start date.");
                    Accepted = false;
                    return;
                } try
                {
                    end = (DateTime)EndDatePicker.SelectedDate;
                }
                catch (Exception)
                {
                    MessageBox.Show("Please enter a valid end date.");
                    Accepted = false;
                    return;
                }
                DateTime shiftStart;
                DateTime time;
                try
                {
                    time       = DateTime.ParseExact(StartTimeBox.Text, "hh:mm:ss tt", CultureInfo.CurrentCulture);
                    shiftStart = start + time.TimeOfDay;
                }
                catch
                {
                    MessageBox.Show("Shift start time is invalid.");
                    Accepted = false;
                    return;
                }
                TimeSpan duration;
                if (!TimeSpan.TryParse(EndTimeBox.Text, out duration))
                {
                    MessageBox.Show("Shift duration is invalid.");
                    Accepted = false;
                    return;
                }
                List <DayOfWeek> daysOfWeek = new List <DayOfWeek>();
                if (MondayCheckBox.IsChecked == true)
                {
                    daysOfWeek.Add(DayOfWeek.Monday);
                }
                if (TuesdayCheckBox.IsChecked == true)
                {
                    daysOfWeek.Add(DayOfWeek.Tuesday);
                }
                if (WednesdayCheckBox.IsChecked == true)
                {
                    daysOfWeek.Add(DayOfWeek.Wednesday);
                }
                if (ThursdayCheckBox.IsChecked == true)
                {
                    daysOfWeek.Add(DayOfWeek.Thursday);
                }
                if (FridayCheckBox.IsChecked == true)
                {
                    daysOfWeek.Add(DayOfWeek.Friday);
                }
                if (SaturdayCheckBox.IsChecked == true)
                {
                    daysOfWeek.Add(DayOfWeek.Saturday);
                }
                if (SundayCheckBox.IsChecked == true)
                {
                    daysOfWeek.Add(DayOfWeek.Sunday);
                }

                if (_shift == null)
                {
                    Shift = Shift.ShiftFactory(NameTextBox.Text, shiftStart, duration, start, end, null, daysOfWeek, _isCoating);
                }
                else
                {
                    _shift.Name      = NameTextBox.Text;
                    _shift.StartDate = start;
                    _shift.Duration  = duration;
                    _shift.StartTime = shiftStart;
                    _shift.EndDate   = end;
                    _shift.DaysList  = daysOfWeek;
                }
                Shift.ForegroundColor = ForegroundColorPicker.SelectedColor;
                Shift.BackgroundColor = BackgroundColorPicker.SelectedColor;
                Shift.LinesCanRunOn   = new List <string>(ViewModel.RunOnLines);
            }
            catch (Exception exception)
            {
                MessageBox.Show(String.Format("There was an error while trying to save the shift information.\nDeveloper information: {0}", exception.Message));
                Accepted = false;
            }
            Close();
        }
 public static ShiftTime ShiftTimeFactory(DateTime startTime, TimeSpan duration, bool isActive = true, bool isOvertime = false, Shift shift = null)
 {
     return(new ShiftTime(startTime, duration, isActive, isOvertime, shift));
 }
 private ShiftTime(DateTime startTime, TimeSpan duration, bool isActive, bool isOvertime, Shift shift)
 {
     _startTime  = startTime;
     _duration   = duration;
     _isActive   = isActive;
     _isOvertime = isOvertime;
     _shift      = shift;
 }
        public DateTime GetPreviousShiftStart(DateTime date, Shift currentShift)
        {
            DateTime startTime = StaticFunctions.GetDayAndTime(date, currentShift.StartTime);

            // account for shifts that span multiple days. Use the starting day...
            if ((new TimeSpan(currentShift.StartTime.Hour, currentShift.StartTime.Minute, currentShift.StartTime.Second) +
                 currentShift.Duration).TotalHours > 24)
            {
                startTime = startTime.AddDays(-1);
            }

            DateTime currentDateTime = startTime;
            bool     closer          = true;
            // Find the end time closest to the current shift start time while not going over - Welcome to The Shift is Right?
            TimeSpan closestdif = TimeSpan.MaxValue;

            int infinityPrevention = 356; // dont go back more than a year...

            do
            {
                --infinityPrevention;

                if (Shifts.Count == 1)
                {
                    var shift      = Shifts[0];
                    var found      = false;
                    var shiftStart = StaticFunctions.GetDayAndTime(currentDateTime, shift.StartTime);
                    // go back a day until there is a shift on that day
                    do
                    {
                        shiftStart = shiftStart.AddDays(-1);
                        if (shift.DaysList.Contains(shiftStart.DayOfWeek))
                        {
                            closestdif = startTime - shiftStart;
                            found      = true;
                        }
                    } while (!found && infinityPrevention > 0);
                    break;
                }
                else
                {
                    foreach (var shift in Shifts)
                    {
                        if (shift.DaysList.Contains(currentDateTime.DayOfWeek))
                        {
                            closer = false; // something runs on this day. If it's not closer, then quit
                            var shiftStart = StaticFunctions.GetDayAndTime(currentDateTime, shift.StartTime);
                            if ((new TimeSpan(shiftStart.Hour, shiftStart.Minute, shiftStart.Second) + shift.Duration).TotalHours > 24)
                            {
                                shiftStart = shiftStart.AddDays(-1);
                            }


                            TimeSpan dif = startTime - shiftStart;
                            if (dif.TotalHours > 0 && dif < closestdif)
                            {
                                closestdif = dif;
                                closer     = true;
                            }
                            else if (dif.TotalHours < 0)
                            {
                                closer = true;
                            }
                        }
                    }
                }
                currentDateTime = currentDateTime.AddDays(-1); // go back a day
            } while (closer && infinityPrevention > 0);

            if (infinityPrevention == 0)
            {
                throw new ArgumentOutOfRangeException("No shifts before the given one.");
            }

            return(startTime - closestdif);
        }