public int UpdateCurrencyExchangeRates()
        {

            try
            {
                //new up everything
                IMailService mailerService = new MaintainanceMailer();
                CurrencyExchangeService ces = new CurrencyExchangeService(mailerService);
                

                //get all the currencies
                using (var _db = new PortVillasContext())
                {

                    var currencies = _db.CurrencyExchanges.ToList();

                    foreach (var currency in currencies)
                    {
                        ces.UpdateCurrency(currency);
                    }

                }

                return 0;
            }
            catch (Exception ex)
            {

                
                
            }
            return -1;


        }
        public void TestMail()
        {

            MaintainanceMailer letsMail = new MaintainanceMailer();
            letsMail.SendEmail();

        }
        public void TestCurrencyService()
        {
            //new up everything
            IMailService mailerService = new MaintainanceMailer();
            CurrencyExchangeService ces = new CurrencyExchangeService(mailerService);

            //send a cheeky testmail


            //get all the currencies
            using (var _db = new PortVillasContext())
            {

                var currencies = _db.CurrencyExchanges.ToList();

                foreach (var currency in currencies)
                {
                    ces.UpdateCurrency(currency);
                }

            }

        }
        public void TestMaintainanceMailService()
        {
            IMailService mailerService = new MaintainanceMailer();
            CurrencyExchangeService ces = new CurrencyExchangeService(mailerService);

            mailerService.Mail("*****@*****.**", "Help");
        }
        public int SendMaintainanceMail(string emailTo, string message)
        {
            try
            {
                IMailService ms = new MaintainanceMailer();
                ms.Mail("*****@*****.**", message);

                return 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public ActionResult AddBookingEvent(FormCollection eventAndBooking)
        {


            try
            {

                var bookingId = Convert.ToInt32(eventAndBooking["BookingID"]);
                var eventTypeId = Convert.ToInt32(eventAndBooking["EventTypeID"]);

                //get the booking
                var booking = db.Bookings.Where(x => x.BookingID.Equals(bookingId)).FirstOrDefault();
                var property = booking.Property;

                //get the customer
                var customer = db.Customers.FirstOrDefault(x => x.CustomerID.Equals(booking.CustomerID));
                var documentType = new PRCDocument.PRCDocumentType();



                //create new event and assign the correct typeID HERE, then can make event creation generic                
                var eventToAdd = CreateBookingEventViaFactory(eventTypeId, booking);

                /*var whatAmIReturning = FullyInstantiateAndRunEvent(customer, booking);*/

                //////////GET TYPE OF DOCUMENT//////
                //STORE IN EVENT - DocumentEnum and Email Enum
                if (eventToAdd.EventType.DocumentEnumID != null)
                {
                    documentType = (PRCDocument.PRCDocumentType)eventToAdd.EventType.DocumentEnumID;
                }


                ////BEGIN GENERIC CODE
                /////////////////////////////////////////
                ////////CREATE EVENT DEPENDING ON SUB TYPE
                EventCommandCreateDocument createDocCommand;
                EventCommandSendEmail sendEmail;
                EventCommandDocumentOutDirectoryBundleAndEmail docBundle;

                switch (eventToAdd.EventType.EventSubTypeID)
                {
                    case 1:
                    //does nothing, just add event
                    case 2:
                        //email out                        
                        sendEmail = new EventCommandSendEmail(eventToAdd, (int)eventToAdd.EventType.EmailTemplateId, customer, booking);
                        eventToAdd.EventCommands.Add(sendEmail);
                        break;
                    case 3:
                        createDocCommand = new EventCommandCreateDocument(eventToAdd, customer, documentType, booking);
                        sendEmail = new EventCommandSendEmail(eventToAdd, (int)eventToAdd.EventType.EmailTemplateId, customer, booking);
                        eventToAdd.EventCommands.Add(createDocCommand);
                        eventToAdd.EventCommands.Add(sendEmail);
                        //document out + email that document
                        break;
                    case 4:
                        //email reminder
                        sendEmail = new EventCommandSendEmail(eventToAdd, (int)eventToAdd.EventType.EmailTemplateId, customer, booking);
                        eventToAdd.EventCommands.Add(sendEmail);
                        break;
                    case 5:
                        //create only
                        sendEmail = new EventCommandSendEmail(eventToAdd, (int)eventToAdd.EventType.EmailTemplateId, customer, booking);
                        eventToAdd.EventCommands.Add(sendEmail);
                        break;
                    case 6:
                        //composite bundle and email
                        //get directory for docs
                        var bundleDir = HttpRuntime.AppDomainAppPath + "FinalBookingDocuments\\" + property.LegacyReference.ToLower() + "\\";
                        docBundle = new EventCommandDocumentOutDirectoryBundleAndEmail(bundleDir, eventToAdd, customer, documentType, booking);
                        sendEmail = new EventCommandSendEmail(eventToAdd, (int)eventToAdd.EventType.EmailEnumID, customer, booking);
                        eventToAdd.EventCommands.Add(docBundle);
                        eventToAdd.EventCommands.Add(sendEmail);
                        break;
                }



                var commandsAndResultsToLog = new List<EventCommand>();

                //execute the commands //create document using executes //create emails and bodies //do all sending

                var result = new EventCommandResult();
                foreach (var command in eventToAdd.EventCommands)
                {
                    command.EventCommandResults.Add(command.ExecuteCommand());
                    commandsAndResultsToLog.Add(command);
                }


                /////END CREATE EVENT


                ////////////////////////////
                //DO LOGGING
                //if successful log the event and the commands that ran
                //save event
                EventLogger log = new EventLogger(db);

                //strip out event wiring before loggin 
                eventToAdd.EventCommands = null;
                var doLogging = log.LogEvent(eventToAdd);

                var EventCommandLogger = new CommandLogger(db);
                var eventLogger = new EventLogger(db, EventCommandLogger);



                //save eventcoomand and result
                foreach (var commandAndResult in commandsAndResultsToLog)
                {
                    EventCommandLogger.Log(eventToAdd.EventID, commandAndResult);
                }


                //save document generated
                //if there's any docs, write them to the DB
                foreach (var doc in eventToAdd.Documents)
                {
                    doc.EventID = eventToAdd.EventID;
                    doc.DocumentDescription = documentType.ToString();
                    doc.EmailTo = customer.EmailAddress;
                    doc.CustomerID = customer.CustomerID;

                    db.Documents.Add(doc);
                    db.SaveChanges();
                }


                //for view
                ViewBag.Bookingid = bookingId;

                var eventsForDDL =
                    db.EventTypes.Where(x => x.EventSchemeTypeID == 1 || x.EventSchemeTypeID == 2).ToList();
                ViewBag.EventTypeDDL = eventsForDDL;
            }
            catch (Exception ex)
            {
                MaintainanceMailer mail = new MaintainanceMailer();

                mail.theAsposeMessage.Subject = "Adding Booking Event Failed";

                mail.theAsposeMessage.Body = ex.Message.ToString();
                mail.theAsposeMessage.Body += "----------------------------------";
                mail.theAsposeMessage.Body += ex.InnerException.ToStringDescriptive();

                throw ex;

            }


            return View();
        }
        public ActionResult GetBookingDetails(Customer cus, CustomerBankDetail bank, List<Booking> bookings = null, List<BookingExtraSelection> bookingExtraSelections = null, List<BookingParticipant> bookingParticipants = null)
        {
            //set customer preferred currency symbol and currency

            using (var db = new PortugalVillasContext())
            {

                var account = new AccountController();
                var eventService = new EventController();
                var bookingRepo = new FinalBookingDetailGatheringController();
                //CUSTOMER
                //does customer have ID? if not create new Customer
                if (cus.CustomerID.Equals(0))
                {
                    var returnedCus = bookingRepo.CreateCustomer(cus, db);
                    cus = returnedCus;
                    Session["prc_customer"] = returnedCus;
                }
                else
                {
                    //update customer with new details
                    cus.BookingExtraSelections = null;
                    cus.Bookings = null;

                    //Do the UPDATE
                    if (ModelState.IsValid)
                    {
                        db.Entry(cus).State = EntityState.Modified;
                        var objContextCus = ((IObjectContextAdapter)db).ObjectContext;

                        var refreshableObjectsCus =
                            (from entry in objContextCus.ObjectStateManager.GetObjectStateEntries(
                                EntityState.Added
                                | EntityState.Deleted
                                | EntityState.Modified
                                | EntityState.Unchanged)
                             where entry.EntityKey != null
                             select entry.Entity);

                        objContextCus.Refresh(RefreshMode.ClientWins, refreshableObjectsCus);

                        //if it works, do the update for the userContext, else, don't as it failed
                        if (objContextCus.SaveChanges() > 0)
                        {
                            /*_usersContext.Entry(user).State = EntityState.Modified;
                            _usersContext.SaveChanges();*/
                        }



                    }
                }

                //BANK DETAIL
                if (bank.CustomerBankDetailID.Equals(0))
                {
                    var returnedBank = bookingRepo.CreateCustomerBankDetail(bank, cus, db);
                    Session["prc_customerBankingDetail"] = returnedBank;
                }
                else
                {
                    bank.CustomerID = cus.CustomerID;
                    db.Entry(bank).State = EntityState.Modified;

                    var objContext = ((IObjectContextAdapter)db).ObjectContext;

                    var refreshableObjects = (from entry in objContext.ObjectStateManager.GetObjectStateEntries(
                        EntityState.Added
                        | EntityState.Deleted
                        | EntityState.Modified
                        | EntityState.Unchanged)
                                              where entry.EntityKey != null
                                              select entry.Entity);

                    objContext.Refresh(RefreshMode.ClientWins, refreshableObjects);


                    if (ModelState.IsValid)
                    {

                        if (objContext.SaveChanges() > 0) ;
                        {
                            //great it worked
                        }
                    }

                }


                //parent
                BookingParentContainer parentContainer = new BookingParentContainer();
                parentContainer.CustomerID = cus.CustomerID;


                parentContainer = bookingRepo.CreateBookingParentContainer(parentContainer, db);
                //need to add booking reference then propagate to all others below


                //////////////////
                //BOOKING
                //create a parent booking thingy and link i
                if (bookings != null)
                {
                    foreach (var booking in bookings)
                    {
                        booking.Test = false; //it's real, from a customer
                        //convert if neededed

                        //link to parent booking
                        booking.BookingParentContainerID = parentContainer.BookingParentContainerID;
                        //fill em out and push them to the DB

                        var property = db.Properties.Find(booking.PropertyID);

                        booking.Property = property;

                        booking.BookingParticipants = null;
                        bookingRepo.CreateBooking(booking, property, cus, db);



                        var participantsThisRound =
                            bookingParticipants.Where(x => x.StepNo.Equals(bookingParticipants.Min(y => y.StepNo)))
                                .ToList();
                        //create the participants

                        var partstoAdd = new List<BookingParticipant>();
                        foreach (var bookingParticipant in participantsThisRound)
                        {
                            if (bookingParticipant.BookingParticipantFirstName != "" &&
                                bookingParticipant.BookingParticipantFirstName != null
                                && bookingParticipant.BookingParticipantLastName != "" &&
                                bookingParticipant.BookingParticipantLastName != null)
                            {
                                partstoAdd.Add(bookingParticipant);


                            }
                        }



                        foreach (var bookingParticipant in participantsThisRound)
                        {
                            bookingParticipants.Remove(bookingParticipant);
                        }

                        if (partstoAdd.Count > 0)
                        {
                            bookingRepo.CreateBookingParticipant(partstoAdd, booking, db); //create all ones we need
                        }


                        //now check which booking form to send depending on location
                        string EventTypeID;

                        TimeSpan dateRemainder = ((DateTime)booking.StartDate).Subtract(DateTime.Now);
                        int dateRemainderDays = dateRemainder.Days;


                        //late booking form

                        if (cus.Country.ToLower() == "united kingdom" && ConfigurationManager.AppSettings["defaultCurrency"] == "GBP")
                        {
                            if (dateRemainderDays <= 30)
                            {
                                //late uk
                                EventTypeID = "51";
                            }
                            else
                            {
                                //standard UK
                                EventTypeID = "37";
                            }

                        }
                        else
                        {

                            //convert all the bookings to euros                         
                            if (dateRemainderDays <= 30)
                            {
                                //late EU
                                EventTypeID = "52";
                            }
                            else
                            {
                                //standard EU
                                EventTypeID = "20";
                            }

                        }

                        //email all docs
                        var form = new FormCollection();
                        form.Add("BookingID", booking.BookingID.ToString());
                        form.Add("EventTypeID", EventTypeID);

                        eventService.AddBookingEvent(form);

                        //email the user of the system 

                    }
                }



                if (bookingExtraSelections != null)
                {
                    foreach (var bes in bookingExtraSelections)
                    {
                        //?????//convert all BES to EUROS 



                        var extraType = db.BookingExtras.Find((long)bes.BookingExtraID);
                        bes.BookingExtraID = extraType.BookingExtraID;
                        //link to parent booking
                        bes.BookingParentContainerID = parentContainer.BookingParentContainerID;


                        //need
                        //reference
                        //price fully done
                        //all extras on form fully taken care of for each one
                        var createdBes = bookingRepo.CreateBookingExtraSelection(bes, extraType, cus, db);

                        string EventTypeID = "";
                        //car 
                        if (extraType.BookingExtraTypeID == 1)
                        {
                            if (cus.Country.ToLower() == "united kingdom")
                            {
                                EventTypeID = "43";
                            }
                            else
                            {
                                EventTypeID = "62";
                            }
                        }
                        //wine
                        else if (extraType.BookingExtraTypeID == 2)
                        {
                            if (cus.Country.ToLower() == "united kingdom")
                            {
                                EventTypeID = "45";
                            }
                            else
                            {
                                EventTypeID = "60";
                            }
                        }
                        //airport
                        else if (extraType.BookingExtraTypeID == 3)
                        {
                            if (cus.Country.ToLower() == "united kingdom")
                            {
                                EventTypeID = "31";
                            }
                            else
                            {
                                EventTypeID = "61";
                            }
                        }
                        //tour
                        else if (extraType.BookingExtraTypeID == 4)
                        {
                            if (cus.Country.ToLower() == "united kingdom")
                            {
                                EventTypeID = "33";
                            }
                            else
                            {
                                EventTypeID = "63";
                            }
                        }

                        var form = new FormCollection();
                        form.Add("BookingExtraSelectionID", bes.BookingExtraSelectionID.ToString());
                        form.Add("EventTypeID", EventTypeID);

                        eventService.AddBookingExtraSelectionEvent(form);



                    }
                }


                //wipe cars = booking and bes
                Session["Cart_PropertyBookings"] = null;
                Session["Cart_ExtraBookings"] = null;


                //email the main user with the new services and bookings
                var prc = db.PRCInformations.First();

                MaintainanceMailer mail = new MaintainanceMailer();
                mail.theAsposeMessage.Subject = "You have new bookings:";
                mail.theAsposeMessage.HtmlBody =
                    "<h1>Automated email: You have a new booking</h1><br><p>Customer: ID - " + cus.CustomerID + "-" + cus.FirstName + " " +
                    cus.LastName
                    + "</p>";


                if (bookings != null)
                {
                    foreach (var booking in bookings)
                    {
                        mail.theAsposeMessage.HtmlBody += "<p>Booking: ID - " + booking.BookingID + "-" +
                                                          booking.Property.LegacyReference + " for " +
                                                          booking.NumberOfNights + " " + "nights starting " +
                                                          booking.StartDate.ToString().Substring(0, 10)
                                                          + "</p>";
                    }
                }


                if (bookingExtraSelections != null)
                {
                    foreach (var bes in bookingExtraSelections)
                    {
                        mail.theAsposeMessage.HtmlBody += "<p>Booking: ID - " + bes.BookingExtraSelectionID + " - " +
                                                          bes.BookingExtra.BookingExtraName + " for " + bes.NumberOfDays +
                                                          " " + "days starting " +
                                                          bes.ExtraRentalDate.ToString().Substring(0, 10) + " for " +
                                                          bes.NumberOfGuests + " people"
                                                          + "</p>";
                    }
                }

                var addressees = new MailAddressCollection();
                    addressees.Add(prc.PRCNotificationEmailAddress);
                    if (prc.PRCNotificationEmailAddress2 != null)
                        if (prc.PRCNotificationEmailAddress2 != "")
                        {
                            addressees.Add(prc.PRCNotificationEmailAddress2);
                        }
                    if (prc.PRCNotificationEmailAddress3 != null)
                        if (prc.PRCNotificationEmailAddress3 != "")
                        {
                            addressees.Add(prc.PRCNotificationEmailAddress3);
                        }

                    mail.SendEmail();
               


                return RedirectToAction("EndOfBookingProcess", "EndOfProcess");
            }

        }