/// <summary>
        /// Method use to manually generate random survey word of the day for the selected Site.
        /// Perform validation and checks to ensure that the surveyword of the day doesn't repeat.
        /// </summary>
        /// <param name="siteId"></param>
        public void ManuallyGeneratedSurveyWord(int siteId)
        {
            using (var context = new FSOSSContext())
            {
                // Get the assigned word for the selected Site for the current day
                SurveyWord wordToRemove = (from x in context.SurveyWords
                                           where x.date_used.Day == DateTime.Now.Day && x.site_id == siteId
                                           select x).FirstOrDefault();

                // If there is already an assigned word to the selected Site
                if (wordToRemove != null)
                {
                    // Remove the existing word
                    context.SurveyWords.Remove(wordToRemove);
                    context.SaveChanges();
                }


                //Create an instance of Random Object
                Random random = new Random();
                //Create a variable to hold the potential survey word to be used
                PotentialSurveyWord surveyWordToAdd = null;
                //Get the list of potential survey word which is currently active
                List <PotentialSurveyWord> potentialSurveyWordList = (from x in context.PotentialSurveyWords
                                                                      where x.archived_yn == false
                                                                      select x).ToList();
                //Boolean use to check if the word exists;
                bool wordIsUsed = true;
                //Loop that will run until a random word is choosen that doesn't exists on the current Survey Word Table
                do
                {
                    // Get a fresh list of survey words
                    List <SurveyWord> newSurveyWordList = (from x in context.SurveyWords
                                                           select x).ToList();
                    //Generate Random Number
                    int randomIndex = random.Next(0, potentialSurveyWordList.Count());
                    //Get the PotentialSurveyWord to be added based on the index
                    surveyWordToAdd = potentialSurveyWordList.ElementAt(randomIndex);
                    //Loop through the List of current SurveyWord and check if the word has been used by the current site.
                    wordIsUsed = newSurveyWordList.Any(word => word.survey_word_id == surveyWordToAdd.survey_word_id);
                    //If the Word doesn't exists after the loop on Survey Word List. Exit on the loop else start the process all over again.
                } while (wordIsUsed == true);
                //Add SurveyWord after the validation
                SurveyWord newSurveyWord = new SurveyWord()
                {
                    date_used      = DateTime.Now,
                    site_id        = siteId,
                    survey_word_id = surveyWordToAdd.survey_word_id
                };
                // Load newSurveyWord to be saved
                context.SurveyWords.Add(newSurveyWord);
                // Save the new added Survey Word
                context.SaveChanges();
                // clear the survey word
                newSurveyWord = null;
            }
        }
        public string ChangeAvailability(int surveyWordID, int adminID)
        {
            using (var context = new FSOSSContext())
            {
                string message = "";
                try
                {
                    // get the current date
                    DateTime dateTime = DateTime.Today;

                    // check if the current word is in use by matching the correct survey word ID and then checking todays day/month/year against the survey word in the database to see if it is in use today
                    var surveyWordAttachToHospital = (from x in context.SurveyWords
                                                      where x.PotentialSurveyWord.survey_word_id == surveyWordID && x.date_used.Day == dateTime.Day && x.date_used.Month == dateTime.Month && x.date_used.Year == dateTime.Year
                                                      select new SurveyWordPOCO()
                    {
                        siteID = x.site_id,
                        surveyWordID = x.survey_word_id,
                        dateUsed = x.date_used
                    }).FirstOrDefault();

                    if (surveyWordAttachToHospital == null)                                                        // if surveyWordAttachToHospital is null, it is not in use and can be disabled or enabled
                    {
                        PotentialSurveyWord potentialSurveyWord = context.PotentialSurveyWords.Find(surveyWordID); // find the survey word by matching it with the survey word ID
                        if (potentialSurveyWord.archived_yn == true)                                               // if the survey word is archived, toggle the archived boolean to false and display the success message that it has been enabled
                        {
                            potentialSurveyWord.archived_yn = false;
                            message = "Successfully enabled the survey word.";
                        }
                        else if (potentialSurveyWord.archived_yn == false) // if the survey word is active, toggle the archived boolean to true and display the success message that it has been disabled
                        {
                            potentialSurveyWord.archived_yn = true;
                            message = "Successfully disabled the survey word.";
                        }
                        // now update the survey word to either enabled or disabled, the modified date and admin ID from the admin that is logged in to the potential survey word table in the database
                        potentialSurveyWord.administrator_account_id = adminID;
                        potentialSurveyWord.date_modified            = DateTime.Now;
                        context.Entry(potentialSurveyWord).Property(y => y.administrator_account_id).IsModified = true;
                        context.Entry(potentialSurveyWord).Property(y => y.archived_yn).IsModified   = true;
                        context.Entry(potentialSurveyWord).Property(y => y.date_modified).IsModified = true;
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new Exception("Unable to change availability of the selected word. Word is currently in use.");
                    }
                }
                catch (Exception e) // catch the error and display it on the page with MessageUserControl
                {
                    throw new Exception(e.Message);
                }
                return(message); // return the success message
            }
        }
        public void AddWord(string newWord, int adminID)
        {
            using (var context = new FSOSSContext())
            {
                try
                {
                    Regex validWord = new Regex("^[a-zA-Z]+$");

                    // gets a list of survey words where the newWord is matching an existing word
                    var potentialSurveyWordList = from x in context.PotentialSurveyWords
                                                  where x.survey_access_word.ToLower().Equals(newWord.ToLower())
                                                  select new PotentialSurveyWordPOCO()
                    {
                        surveyWord = x.survey_access_word
                    };

                    if (potentialSurveyWordList.Count() > 0) // if a survey word was found display an error that the word already exists
                    {
                        throw new Exception("The word \"" + newWord.ToLower() + "\" already exists. Please choose another word.");
                    }
                    else
                    {
                        if (newWord == "" || newWord == null) // if no survey word was entered, display an error
                        {
                            throw new Exception("You must type in a word to add. Field cannot be empty.");
                        }
                        else if (!validWord.IsMatch(newWord)) // if the survey word entered is not valid (no special characters, spaces, or numbers), display an error
                        {
                            throw new Exception("Please enter only alphabetical letters and no spaces.");
                        }
                        else if (newWord.Length < 4 || newWord.Length > 8)  // if the survey word is not the correct length (between 4 and 8 characters), display an error
                        {
                            throw new Exception("New survey word must be between 4 to 8 characters characters in length.");
                        }
                        else // else, add the new survey word, the current date and admin ID from the admin that is logged in to the potential survey word table in the database
                        {
                            PotentialSurveyWord potentialSurveyWord = new PotentialSurveyWord();
                            potentialSurveyWord.administrator_account_id = adminID;
                            potentialSurveyWord.survey_access_word       = newWord.Trim();
                            potentialSurveyWord.date_modified            = DateTime.Now;
                            context.PotentialSurveyWords.Add(potentialSurveyWord);
                            context.SaveChanges();
                        }
                    }
                }
                catch (Exception e) // catch the error and display it on the page with MessageUserControl
                {
                    throw new Exception(e.Message);
                }
            }
        }
        /// <summary>
        /// Method use to assign a new survey word if a new Site is created
        /// </summary>
        /// <param name="siteName"> Site Name of the new site created</param>
        public void NewSite_NewWord(string siteName)
        {
            // Start of Transaction
            using (var context = new FSOSSContext())
            {
                //grab the new site id,
                int siteId = (from x in context.Sites
                              where x.site_name.Equals(siteName) && x.archived_yn == false
                              select x.site_id).FirstOrDefault();

                //get all the active words
                List <PotentialSurveyWord> potentialSurveyWordList = (from x in context.PotentialSurveyWords
                                                                      where x.archived_yn == false
                                                                      select x).ToList();
                //get all the surveywords
                List <SurveyWord> surveyWordList = (from x in context.SurveyWords
                                                    select x).ToList();
                // If the count of survey words being used
                if (surveyWordList.Count >= potentialSurveyWordList.Count)
                {
                    SurveyWord wordToBeRemoved = (from x in context.SurveyWords
                                                  orderby x.date_used
                                                  select x).FirstOrDefault();
                    context.SurveyWords.Remove(wordToBeRemoved);
                    context.SaveChanges();
                }
                Random random     = new Random();
                bool   wordIsUsed = true;
                PotentialSurveyWord surveyWordToAdd = null;

                do
                {
                    List <SurveyWord> newSurveyWordList = (from x in context.SurveyWords
                                                           select x).ToList();
                    int randomIndex = random.Next(0, potentialSurveyWordList.Count());
                    surveyWordToAdd = potentialSurveyWordList.ElementAt(randomIndex);
                    wordIsUsed      = newSurveyWordList.Any(word => word.survey_word_id == surveyWordToAdd.survey_word_id);
                } while (wordIsUsed == true);

                SurveyWord newSurveyWord = new SurveyWord()
                {
                    date_used      = DateTime.Now,
                    site_id        = siteId,
                    survey_word_id = surveyWordToAdd.survey_word_id
                };
                context.SurveyWords.Add(newSurveyWord);
                context.SaveChanges();
            }
        }
        /// <summary>
        /// Method use to generate random survey word of the day from the list of potential survey words.
        /// Perform validation and checks to ensure that the surveyword of the day doesn't repeat.
        /// Remove few old survey words once all the words are being used.
        /// </summary>
        public void GenerateSurveyWordOfTheDay()
        {
            // Start of Transaction
            using (var context = new FSOSSContext())
            {
                //Get the list of survey word in the database
                List <SurveyWord> surveyWordList = (from x in context.SurveyWords
                                                    select x).ToList();
                //Get the list of potential survey word which is currently active
                List <PotentialSurveyWord> potentialSurveyWordList = (from x in context.PotentialSurveyWords
                                                                      where x.archived_yn == false
                                                                      select x).ToList();
                //Get the list of all site to be added
                List <Site> siteList = (from x in context.Sites
                                        select x).ToList();
                // Check if theres enough active Potential Survey Word on the list
                if (siteList.Count > potentialSurveyWordList.Count())
                {
                    // Throw an exception if there are not enough survey word on the pool to be use on the list
                    throw new Exception("Please add more potential survey word. Not enough potential survey word available to be assign on the hospitals.");
                }
                else
                {
                    //Check if all potential survey word is used
                    if (surveyWordList.Count >= potentialSurveyWordList.Count)
                    {
                        List <SurveyWord> newSurveyListForCheck = null;
                        do
                        {
                            // Get the fresh list of survey words
                            newSurveyListForCheck = (from x in context.SurveyWords
                                                     select x).ToList();

                            //Get the last Survey Word by date
                            SurveyWord surveyWord = (from x in context.SurveyWords
                                                     orderby x.date_used
                                                     select x).FirstOrDefault();
                            //Remove the last Survey Word
                            context.SurveyWords.Remove(surveyWord);
                            context.SaveChanges();
                            surveyWord = null;

                            //Check if the there are enough Potential Survey word available from the List in the database to be used for all the hospital
                            //Repeat the step above until theres enough potentail word to lookupon
                        } while (newSurveyListForCheck.Count() < potentialSurveyWordList.Count() - siteList.Count());
                    }
                    //Create an instance of Random Object
                    Random random = new Random();
                    //Create a variable to hold the potential survey word to be used
                    PotentialSurveyWord surveyWordToAdd = null;
                    foreach (Site site in siteList)
                    {
                        //Boolean use to check if the word exists;
                        bool wordIsUsed = true;
                        //Loop that will run until a random word is choosen that doesn't exists on the current Survey Word Table
                        do
                        {
                            // Get a fresh list of survey words
                            List <SurveyWord> newSurveyWordList = (from x in context.SurveyWords
                                                                   select x).ToList();
                            //Generate Random Number
                            int randomIndex = random.Next(0, potentialSurveyWordList.Count());
                            //Get the PotentialSurveyWord to be added based on the index
                            surveyWordToAdd = potentialSurveyWordList.ElementAt(randomIndex);
                            //Loop through the List of current SurveyWord and check if the word has been used by the current site.
                            wordIsUsed = newSurveyWordList.Any(word => word.survey_word_id == surveyWordToAdd.survey_word_id);
                            //If the Word doesn't exists after the loop on Survey Word List. Exit on the loop else start the process all over again.
                        } while (wordIsUsed == true);

                        //Add SurveyWord after the validation
                        SurveyWord newSurveyWord = new SurveyWord()
                        {
                            date_used      = DateTime.Now,
                            site_id        = site.site_id,
                            survey_word_id = surveyWordToAdd.survey_word_id
                        };
                        // Load newSurveyWord to be saved
                        context.SurveyWords.Add(newSurveyWord);
                        // Save the new added Survey Word
                        context.SaveChanges();
                        // clear the survey word
                        newSurveyWord = null;
                    }
                }
                // Initialze current Date and Time
                DateTime currentDateTime = DateTime.Now;
                // Set timeOffset to be use to setup date and time for the next scheduled job.
                // Current setup will be set to fire at 12:00 midnight after one day
                DateTime timeOffset = new DateTime(currentDateTime.Year, currentDateTime.Month, currentDateTime.Day + 1, 0, 0, 0);
                // Setup new job schedule that will fire GenerateSurveyWordOfTheDay after the given offset
                BackgroundJob.Schedule(() => GenerateSurveyWordOfTheDay(), timeOffset);
            }
        }