/// <summary>
        /// The user only can upcomming appointment.
        /// When user click cancel appointment button,
        /// The program should check that the user select any appointment to cancel
        /// If not, the program will notify to user about that.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCancelAppointment_Click(object sender, RoutedEventArgs e)
        {
            Appointment selectedAppointment = (Appointment)dgTimeSchedule.SelectedItem;

            if (selectedAppointment == null)
            {
                MessageHelper.ShowWarningMessage("Please select an appointment first!", "Cancel Appointment");
                return;
            }

            if (selectedAppointment.DateTime < DateTime.Now)
            {
                MessageHelper.ShowWarningMessage("You only cancel upcoming appointments!", "Cancel Appointment");
                return;
            }

            MessageBoxResult answer = MessageHelper.ShowQuestionMessage(
                string.Format("Do you want to cancel appointment with {0} at {1:t}", selectedAppointment.Customer.FullName, selectedAppointment.DateTime), "Cancel Appointment");

            if (answer == MessageBoxResult.No || answer == MessageBoxResult.Cancel)
            {
                return;
            }
            //Reset select item of data grid
            dgTimeSchedule.SelectedItem = null;

            //Delete Appointment
            XMLHelpers.Instance().Appointments.Remove(selectedAppointment);
            scheduleViewModel.AppointmentList = XMLHelpers.Instance().Appointments;
            scheduleViewModel.RefreshAppointments();
        }
        /// <summary>
        /// Read default Data XML Default
        /// Load data into data grid
        /// </summary>
        private void UpdateDatagridSchedule()
        {
            LoadDefaultDataFile();

            NailSalonXMLDAO NailSalonDAO = XMLHelpers.Instance();

            scheduleViewModel.Today           = DateTime.Now;
            scheduleViewModel.AppointmentList = NailSalonDAO.Appointments;
        }
        private void BtnSaveData_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
            saveFileDialog.Filter           = "XML file (*.xml)|*.xml";

            if (saveFileDialog.ShowDialog() == true)
            {
                XMLHelpers.WriteToXML(saveFileDialog.FileName);
            }
        }
        public static bool IsEmployeeAvailableOn(DateTime dateTime, Employee employee, Appointment currentAppointment)
        {
            List <Appointment> appointments         = XMLHelpers.Instance().Appointments;
            List <Appointment> reservedAppointments = appointments.Where(appointment => appointment.DateTime.Date == dateTime.Date && appointment.Employee == employee).ToList();

            foreach (Appointment appointment in reservedAppointments)
            {
                if ((appointment.DateTime <= dateTime) && (dateTime <= appointment.TimeEnd) && (currentAppointment.ID != appointment.ID))
                {
                    return(false);
                }
            }
            return(true);
        }
        /// <summary>
        /// When program startup, the program will read default data file
        /// Default data file should be store in Debug or Release Folder
        /// If the defualt data file is not found, we should use Load Data feature
        /// to load data file into our program
        /// </summary>
        private void LoadDefaultDataFile()
        {
            string fileName = ConfigurationManager.AppSettings["XMLDataFile"];
            string path     = PathHelper.GetFileLocation(fileName);

            if (File.Exists(path))
            {
                try
                {
                    XMLHelpers.ReadFromXML(fileName);
                }
                catch
                {
                    //
                }
            }
        }
        private static int GetMaxApppointmentIDvalue(Random rand)
        {
            List <Appointment> appointments = XMLHelpers.Instance().Appointments;

            if (appointments == null || appointments.Count == 0)
            {
                return(0);
            }
            return(appointments.Select(appointment => {
                string id = appointment.ID.Substring(1);
                if (IsNumberString(id))
                {
                    return int.Parse(id);
                }
                return rand.Next();
            }).Max());
        }
        private static int GetMaxServiceIDvalue(Random rand)
        {
            List <Service> services = XMLHelpers.Instance().Services;

            if (services == null || services.Count == 0)
            {
                return(0);
            }
            return(services.Select(service => {
                string id = service.ID.Substring(1);
                if (IsNumberString(id))
                {
                    return int.Parse(id);
                }
                return rand.Next();
            }).Max());
        }
        private static int GetMaxCustomerIDvalue(Random rand)
        {
            List <Customer> customers = XMLHelpers.Instance().Customers;

            if (customers == null || customers.Count == 0)
            {
                return(0);
            }
            return(customers.Select(customer => {
                string id = customer.ID.Substring(1);
                if (IsNumberString(id))
                {
                    return int.Parse(id);
                }
                return rand.Next();
            }).Max());
        }
        private static int GetMaxEmployeeIDvalue(Random rand)
        {
            List <Employee> employees = XMLHelpers.Instance().Employees;

            if (employees == null || employees.Count == 0)
            {
                return(0);
            }
            return(employees.Select(employee => {
                string id = employee.ID.Substring(1);
                if (IsNumberString(id))
                {
                    return int.Parse(id);
                }
                return rand.Next();
            }).Max());
        }
        /// <summary>
        /// Open a dialog to select the xml file
        /// Load data from xml file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnLoadData_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
            openFileDialog.DefaultExt       = ".xml";
            openFileDialog.Filter           = "XML file (*.xml)|*.xml";
            if (openFileDialog.ShowDialog() == true)
            {
                // a flag for store status of reading xml file, and catch exception message to display for users.
                try
                {
                    XMLHelpers.ReadFromXML(openFileDialog.FileName);
                    scheduleViewModel.AppointmentList = XMLHelpers.Instance().Appointments;
                    scheduleViewModel.RefreshAppointments();
                } catch (Exception error)
                {
                    MessageBox.Show(error.Message, "Failed to read " + System.IO.Path.GetFileName(openFileDialog.FileName), MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }
        }
        /// <summary>
        /// Check the hour is available for booking
        /// </summary>
        /// <param name="date"></param>
        /// <param name="hour"></param>
        /// <returns></returns>
        public static bool IsValidHoursOf(DateTime date, DateTime hour)
        {
            List <Appointment> appointmentLisbyDate = SearchHelper.ListAppointmentsOfToday(XMLHelpers.Instance().Appointments, date);

            foreach (Appointment appointment in appointmentLisbyDate)
            {
                if (hour >= appointment.TimeStart && hour <= appointment.TimeEnd)
                {
                    return(false);
                }
            }
            return(true);
        }