public void TestFullDocumentGenerateStack()
        {

            var db = new PortugalVillasContext();

            //Test WITH PARENT

            //TEST WITH BOOKING
            Booking booking = db.Bookings.Where(x => x.BookingID.Equals(4)).FirstOrDefault();
            booking.PropertyID = 221;
            /*           Booking booking = null;*/



            //TEST WITH CUSTOMER
            Customer aCustomer = db.Customers.Where(x => x.CustomerID == 1).FirstOrDefault();
            //TEST WITH BES
            var bes = db.BookingExtraSelections.Where(x => x.BookingExtraSelectionID == 3).First();
            bes.BookingExtraID = 1;

            //set up vars


            var type = PRCDocument.PRCDocumentType.UK_WineTasting;




            //create event  - need to give it type thus name
            var anEvent = new Event();
            anEvent.Documents = new List<Document>();
            //TEST ADD ID - PLEASE REMOVE
            anEvent.EventID = 2;
            //END TEST CODE

            anEvent.WhenCreated = DateTime.Now;
            var commandsAndResultsToLog = new List<EventCommand>();


            //create correct command
            //doc
            //email out with doc

            EventCommandCreateDocument createDocCommand;
            EventCommandSendEmail sendEmail;
            //HOW DO WE DECIDE WHAT TYPE OF EVENT WE WANT??? depends on type??

            if (booking != null)
            {
                createDocCommand = new EventCommandCreateDocument(anEvent, aCustomer, type, booking);
                /* sendEmail = new EventCommandSendEmail(anEvent, aCustomer, booking);*/

            }
            else
            {
                createDocCommand = new EventCommandCreateDocument(anEvent, aCustomer, type, null, bes);
                /*/sendEmail = new EventCommandSendEmail(anEvent, aCustomer, null, bes);*/
            }



            //create all commands
            anEvent.EventCommands.Add(createDocCommand);
            /*       anEvent.EventCommands.Add(sendEmail);*/


            //create document using executes
            var result = new EventCommandResult();
            foreach (var command in anEvent.EventCommands)
            {
                command.EventCommandResults.Add(command.ExecuteCommand());
                commandsAndResultsToLog.Add(command);
            }

            //render

            //save to DB with all correct commands etc

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

            //save event
            eventLogger.LogEvent(anEvent);
            //save eventcoomand and result


            foreach (var commandAndResult in commandsAndResultsToLog)
            {
                EventCommandLogger.Log(anEvent.EventID, commandAndResult);
            }

            //save document generated

            //NEED TO ADD CORRECT PARAMS (which are what??)

            //if there's any docs, write them to the DB
            foreach (var doc in anEvent.Documents)
            {
                doc.EventID = anEvent.EventID;
                doc.DocumentDescription = type.ToString();
                doc.EmailTo = aCustomer.EmailAddress;
                doc.CustomerID = aCustomer.CustomerID;

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





        }
        //
        // GET: /Test/
        public void TestEventCommandLogging()
        {
            var db = new PortugalVillasContext();

            var log = new CommandLogger(db);



            log.Log(35, new EventCommand());


        }
        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();
        }