/// <summary>
        /// Creates and saves tickets for given customer,show and seats. The seats also holds a boolean representation of if customer uses wheelchair
        /// </summary>
        /// <param name="customer">The customer who booked the tickets</param>
        /// <param name="show">The show that was booked</param>
        /// <param name="seats">The seats that are booked</param>
        /// <returns>A boolean representation if it succeeded or not</returns>
        public bool BookTickets(Customer customer, Show show, Dictionary <int, bool> seats)
        {
            bool success = true;

            customer = SaveCustomerInDB(customer);
            using (FestivalContext context = new FestivalContext(ConnectionString))
            {
                try
                {
                    foreach (var seat in seats)
                    {
                        context.Tickets.Add(new Ticket()
                        {
                            CustomerId = customer.CustomerId,

                            ShowId     = show.ShowId,
                            Seat       = seat.Key,
                            Wheelchair = seat.Value
                        });
                    }
                    context.SaveChanges();
                }
                catch (SqlException)
                {
                    success = false;
                }
            }
            return(success);
        }
        /// <summary>
        /// For the given show, all the tickets with corresponding seatnumbers are removed
        /// </summary>
        /// <param name="show">The show where tickets are to be removed</param>
        /// <param name="seatsToRemove">a list of integers, containing the seats to remove</param>
        /// <returns>a boolean representing if it was a success or not</returns>
        public bool RemoveTickets(Show show, List <int> seatsToRemove)
        {
            bool success = true;

            using (FestivalContext context = new FestivalContext(ConnectionString))
            {
                List <Ticket> tickets = (from ticket in context.Tickets
                                         where ticket.ShowId == show.ShowId
                                         select ticket).ToList();
                //Foreach through the tickets found, and get the ones to remove. Then use those to RemoveRange in the context
                List <Ticket> ticketsToRemove = new List <Ticket>();
                foreach (int seat in seatsToRemove)
                {
                    ticketsToRemove.Add(tickets.Find(x => x.Seat == seat));
                }
                try
                {
                    context.Tickets.RemoveRange(ticketsToRemove);
                    context.SaveChanges();
                }
                catch (SqlException)
                {
                    success = false;
                }
            }
            return(success);
        }
        /// <summary>
        /// Grabs the 10 movies in the database and returns them as a list of Movie, ordered by movieID
        /// </summary>
        /// <returns></returns>
        public List <Movie> GetMovies()
        {
            List <Movie> movies;

            using (FestivalContext context = new FestivalContext(ConnectionString))
            {
                movies = (from movie in context.Movies
                          orderby movie.MovieId ascending
                          select movie).ToList();
            }
            return(movies);
        }
        /// <summary>
        /// Grabs and returns all the tickets(as a list) found for a given show
        /// </summary>
        /// <param name="currentShow">the show whose tickets are going to be returned</param>
        /// <returns>A list of Ticket</returns>
        public List <Ticket> GetTicketsForShow(Show currentShow)
        {
            List <Ticket> tickets;

            using (FestivalContext context = new FestivalContext(ConnectionString))
            {
                tickets = (from ticket in context.Tickets
                           where ticket.Show.ShowId == currentShow.ShowId
                           select ticket).ToList();
            }
            return(tickets);
        }
        /// <summary>
        /// Finds all the shows for a given movie(its movieId), and returns them as a list of shows
        /// </summary>
        /// <param name="movie">The movie whose shows are wanted</param>
        /// <returns>List of Shows corresponding to the input movie (By movieId</returns>
        public List <Show> GetShowsForMovie(Movie movie)
        {
            List <Show> shows;

            using (FestivalContext context = new FestivalContext(ConnectionString))
            {
                shows = (from show in context.Shows
                         where show.Movie.MovieId == movie.MovieId
                         orderby show.StartTime ascending
                         select show).ToList();
            }
            return(shows);
        }
 /// <summary>
 /// Called on when a given customer wants to drop all tickets for a given show
 /// </summary>
 /// <param name="show">the show to drop tickets from</param>
 /// <param name="activeCustomer">The customer who wants to drop their tickets</param>
 /// <returns>a boolean representation of if it succeeded or not</returns>
 public bool DropTicketsForSpecificShowAndCustomer(Show show, Customer activeCustomer)
 {
     try
     {
         using (FestivalContext context = new FestivalContext(ConnectionString))
         {
             List <Ticket> toRemove = (from ticket in context.Tickets
                                       where ticket.CustomerId == activeCustomer.CustomerId &&
                                       ticket.ShowId == show.ShowId
                                       select ticket).ToList();
             context.Tickets.RemoveRange(toRemove);
             context.SaveChanges();
         }
     }
     catch (SqlException)
     {
         return(false);
     }
     return(true);
 }
        /// <summary>
        /// If the customer is NOT found in the database, saves it (To be called on when customer wants to book tickets
        /// to ensure that the customer is added).
        /// </summary>
        /// <param name="customer">The customer to check, and add if not found</param>
        /// <returns>a Customer, if the input customer didn't have a customerId, the returned one defenitivly does</returns>
        private Customer SaveCustomerInDB(Customer customer)
        {
            Customer toReturn = customer;

            using (FestivalContext context = new FestivalContext(ConnectionString))
            {
                Customer existingCustomer = (from cust in context.Customers
                                             where cust.Ssn == customer.Ssn
                                             select cust).FirstOrDefault();
                if (existingCustomer == null)
                {
                    context.Customers.Add(customer);
                    context.SaveChanges();
                    //We do this as to put the customer through the database and get it's customerId
                    toReturn = (from cust in context.Customers
                                where cust.Ssn == customer.Ssn
                                select cust).FirstOrDefault();
                }
            }
            return(toReturn);
        }
        /// <summary>
        /// This method returns a Dictionary of Shows and ints, where the shows are keys, and made up of all shows
        /// the given customer has booked tickets in. The integer is the value, and corresponds to the number of tickets said customer
        /// has in the given show(Key)
        /// </summary>
        /// <param name="customer">The customer to search with</param>
        /// <returns>Dictionary of Show and int</returns>
        public Dictionary <Show, int> GetCustomersShowsAndAmountOfTickets(Customer customer)
        {
            List <Ticket> tickets;
            List <Show>   shows;

            using (FestivalContext context = new FestivalContext(ConnectionString))
            {
                tickets = (from tic in context.Tickets
                           where tic.CustomerId == customer.CustomerId
                           select tic).ToList();
                //fetching the shows right away as to not have to do it each time in the foreach below
                shows = context.Shows.ToList();
                //Also fetching each shows movie for showing in the frontend later
                foreach (Show show in shows)
                {
                    show.Movie = (from mov in context.Movies
                                  where mov.MovieId == show.MovieId
                                  select mov).FirstOrDefault();
                }
            }
            Dictionary <Show, int> toReturn = new Dictionary <Show, int>();

            //Goes through each ticket, if the given show is not found in the dictionary, add it
            //If the show is found, increase the ticket-number by 1
            foreach (Ticket tick in tickets)
            {
                Show actualShow = shows.Find(x => x.ShowId == tick.ShowId);
                if (toReturn.ContainsKey(actualShow))
                {
                    toReturn[actualShow]++;
                }
                else
                {
                    toReturn.Add(actualShow, 1);
                }
            }
            return(toReturn);
        }
        /// <summary>
        /// Takes an ssn and checks if it is a current customer in the database. If it is, that customer is returned to the caller
        /// If it is not, a new Customer is created and returned (not written to the database though)
        /// A boolean is also returned, signaling if it is a new customer or not
        /// </summary>
        /// <param name="ssn">input ssn-parameter</param>
        /// <param name="isNewCustomer">boolean, signaling if it is a new customer or not</param>
        /// <returns>A customer</returns>
        public Customer GetCustomer(string ssn, out bool isNewCustomer)
        {
            Customer customer;

            using (FestivalContext context = new FestivalContext(ConnectionString))
            {
                customer = (from cust in context.Customers
                            where cust.Ssn == ssn
                            select cust).FirstOrDefault();
            }
            if (customer == null)
            {
                isNewCustomer = true;
                customer      = new Customer()
                {
                    Ssn = ssn
                };
            }
            else
            {
                isNewCustomer = false;
            }
            return(customer);
        }