private void btnDelete_Click(object sender, RoutedEventArgs e)
 {
     if (lstProgram.SelectedIndex == -1)
         return;
     ProgrammeHelper client = new ProgrammeHelper();
     try
     {
         client.DeleteProgram(user, ((Program)lstProgram.SelectedItem).ProgramID);
         MessageBox.Show("Operation succeeded!");
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
     finally
     {
         client.Close();
     }
     loadPrograms();
 }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (validateInputs())
            {
                DateTime SegmentStartDateTime = eventDay_.StartDateTime.Date;
                DateTime SegmentEndDateTime = eventDay_.StartDateTime.Date;

                SegmentStartDateTime = SegmentStartDateTime
                    .AddHours(int.Parse(cboStartHr.SelectedValue.ToString()))
                    .AddMinutes(int.Parse(cboStartMin.SelectedValue.ToString()));

                int idx = cboBookDuration.SelectedIndex+1;
                //int duration = idx * 30;
                SegmentEndDateTime = SegmentStartDateTime.AddMinutes(idx * 30);

                ProgrammeHelper client = new ProgrammeHelper();
                try
                {

                    if (lstProgram.SelectedIndex != -1 && ((Program)lstProgram.SelectedItem).ProgramID != 0)
                    {
                        client.EditProgram(user, ((Program)lstProgram.SelectedItem).ProgramID, txtName.Text, SegmentStartDateTime, SegmentEndDateTime, txtDescription.Text, txtLocation.Text);

                    }
                    else
                    {
                        bool clashed = client.ValidateProgramTime(eventDay_.DayID, SegmentStartDateTime, SegmentEndDateTime);

                        if (clashed)
                        {
                            MessageBox.Show("Program cannot be overlapped!",
                                 "Overlapping Program Detected", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                            return;
                        }
                        else
                        {
                            client.AddProgram(user, txtName.Text, SegmentStartDateTime, SegmentEndDateTime, txtDescription.Text, eventDay_.DayID, txtLocation.Text);
                        }
                    }

                    MessageBox.Show("Operation succeeded!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);

                    clearAll();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    client.Close();
                }
                loadPrograms();
            }
        }
        private void Program_Swap(Program p1, Program p2)
        {
            DateTime tempstart;
            tempstart = p1.StartDateTime;

            TimeSpan p1ts = p1.EndDateTime - p1.StartDateTime;
            TimeSpan p2ts = p2.EndDateTime - p2.StartDateTime;

            p1.StartDateTime = p2.StartDateTime;
            p1.EndDateTime = p2.StartDateTime.AddMinutes(p1ts.TotalMinutes);

            p2.StartDateTime = tempstart;
            p2.EndDateTime = tempstart.AddMinutes(p2ts.TotalMinutes);

            List<Program> temp = new List<Program>();

            if (p1.ProgramID != 0)
                temp.Add(p1);

            if (p2.ProgramID != 0)
                temp.Add(p2);

            for (int i = 0; i < lstProgram.Items.Count; i++)
            {
                if (((Program)lstProgram.Items[i]).ProgramID != 0 &&
                    ((Program)lstProgram.Items[i]).ProgramID != p1.ProgramID &&
                    ((Program)lstProgram.Items[i]).ProgramID != p2.ProgramID)
                    temp.Add((Program)lstProgram.Items[i]);
            }

            if (Check_OverWrite(temp))
            {
                MessageBox.Show("OverLap or is over the event time boundary");
                return;
            }
            ProgrammeHelper client = new ProgrammeHelper();
            try
            {

                if (p1.ProgramID != 0 && p2.ProgramID != 0)
                {
                    client.SwapProgram(user, p1.ProgramID, p2.ProgramID);
                }
                else
                {
                    if (p1.ProgramID != 0)
                    {

                        client.EditProgram(user, p1.ProgramID, p1.Name, p1.StartDateTime, p1.EndDateTime, p1.Description, p1.Location);
                    }

                    if (p2.ProgramID != 0)
                        client.EditProgram(user, p2.ProgramID, p2.Name, p2.StartDateTime, p2.EndDateTime, p2.Description, p2.Location);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                client.Close();
            }

            loadPrograms();
        }
        private void lstProgram_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Helper.IdleHelper.startIdleTimer();
            if (lstProgram.SelectedIndex == -1)
            {
                clearAll();
                Helper.IdleHelper.stopIdleTimer();
                btnAdd.Content = "Add";
                return;
            }
            ProgrammeHelper client = new ProgrammeHelper();
            try
            {

                Program selectedProgram = (Program)lstProgram.SelectedItem;
                txtName.Text = selectedProgram.Name;
                cboStartHr.SelectedIndex = selectedProgram.StartDateTime.Hour;
                cboStartMin.SelectedIndex = selectedProgram.StartDateTime.Minute / 30;
                cboEndHr.SelectedIndex = selectedProgram.EndDateTime.Hour;
                cboEndMin.SelectedIndex = selectedProgram.EndDateTime.Minute / 30;
                txtDescription.Text = selectedProgram.Description;
                txtLocation.Text = selectedProgram.Location;
                btnAdd.Content = "Save";

                cboBookDuration.SelectedIndex = computeDurationIdx(selectedProgram) - 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }
        private void loadPrograms()
        {
            ProgrammeHelper client = new ProgrammeHelper();
            try
            {

                List<Program> progList = client.ViewProgram(eventDay_.DayID).ToList<Program>();

                DateTime curr = eventDay_.StartDateTime;
                DateTime end = eventDay_.EndDateTime;

                List<Program> newprogList = new List<Program>();
                while (curr.CompareTo(end) < 0)
                {
                    for (int i = 0; i < progList.Count; i++)
                    {
                        if (progList[i].StartDateTime.CompareTo(curr) == 0)
                        {
                            newprogList.Add(progList[i]);
                            curr = progList[i].EndDateTime;
                            goto next;
                        }
                    }

                    Program p = new Program();
                    p.Name = "";
                    p.StartDateTime = curr;
                    p.EndDateTime = curr.AddMinutes(30);
                    newprogList.Add(p);
                    curr = curr.AddMinutes(30);

                next:
                    continue;

                }

                lstProgram.ItemsSource = newprogList.OrderBy(x => x.StartDateTime)
                                                 .ThenBy(x => x.EndDateTime).ToList<Program>();
                lstProgram.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }
        private void loadPrograms()
        {
            ProgrammeHelper client = new ProgrammeHelper();
            List<Program> progs = client.ViewProgram(this.eventday_.DayID).ToList<Program>();

            if (progs.Count() == 0)
                txtProgramMsg.Text = "No Programmes Added Yet.";
            else
            {
                txtProgramMsg.Text = "There are " + progs.Count + " planned programmes";// from " + min.ToShortTimeString() + " to " + max.ToShortTimeString();
            }
            client.Close();
        }
 private void loadProgram()
 {
     lstProgram.Items.Clear();
     ProgrammeHelper client = new ProgrammeHelper();
     List<int> countList = client.GetEventProgCount(event_.EventID).ToList<int>();
     client.Close();
     for (int i = 0; i < countList.Count; i++)
     {
         string tempResult = DayList[i].StartDateTime.ToShortDateString() + "\t\t-\t" + countList[i] + " programme(s)";
         lstProgram.Items.Add(tempResult);
     }
 }