public void SendNotification(string author, string message)
        {
            PointerdbEntities db = new PointerdbEntities();
            SignalRMessageTable OBJM = new SignalRMessageTable()
            {
                 Name=author,
                  message = message
            };

            string password = PasswordGenerator.GeneratePassword("8").ToString(); 
            string string1 = "Hi Firstname and lastname, An Account has been created for you by PointerWebApp.com ";
            string string2 = "The login information for apartment: apartment#: is below";
            string string3 = "Username: EmailAddress";
            string string4 = "Temporary password: "******"Click the on this link and follow the instructions to initiate your account. ";
            string string6 = "Company Description";
            string string7 = "Find more information...";

            string x = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n", string1, string2, string3, string4, string5, string6, string7);

            Gmail gmail = new Gmail("pointerwebapp", "Dmc10040!");
            MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**");
            msg.Subject = "Pointer Security New Account Notification";
            msg.Body = x;
            gmail.Send(msg);


            db.SignalRMessageTable.Add(OBJM);
            db.SaveChanges();
            Clients.All.broadcastNotification(author, message);
        }
       public JsonResult ApplyUpdatesRepairRequest(string UserID, int RepairRequestID, string Notes, bool whichone)
       {

           if (whichone == true) //Building Staff========================================================================
           {
               pdfWorker pdfworker = new pdfWorker();
               RepairRequest RR = db.RepairRequest.Find(RepairRequestID);

               RepairRequestNote RN = new RepairRequestNote
               {
                   DateCreated = DateTime.Now,
                   Notes = Notes
               };

               db.RepairRequestNote.Add(RN);
               db.SaveChanges();

               RR.RepairRequestNoteID = RN.Id;
               RR.AssignID = UserID;
               RR.AssignContractorID = null;
               RR.Status = "Assigned";

               db.RepairRequest.Attach(RR);
               var Entry = db.Entry(RR);

               Entry.Property(c => c.RepairRequestNoteID).IsModified = true;
               Entry.Property(c => c.AssignID).IsModified = true;
               Entry.Property(c => c.AssignContractorID).IsModified = true;
               Entry.Property(c => c.Status).IsModified = true;

               db.SaveChanges();

               var Worker = db.BuildingUser.Where(c => c.UserID == UserID).FirstOrDefault();

               //string string1 = "<div style='font-size:20px; colo:blue; display:bloc'>Hi " + Worker.FirstName + " " + Worker.LastName + ",</div> ";
               //string string2 = "You have a new assignemt and the description is bellow:";
               //string string3 = "The Category of this Request is " + RR.RepairRequestCategories.Categories;
               //string string4 = "The Decription of the request is: " + RR.ProblemDescription;
               //string string5 = "The Urgency is: " + RR.RepairUrgency.Urgency;
               //string string6 = "For questions about this email Contact management at: " + RR.Buildings.BuildingPhone;
               //string string7 = "Find more information...";

               //string x = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n", string1, string2, string3, string4, string5, string6, string7);

               string contenttobemail = " <div style='font-size:20px; display:block;  width:100%; background:#0071bc;  height:50px;  line-height:50px; padding:0 15px; border:1px solid lightgrey;   color:white;' >"+
                Worker.FirstName + " " + Worker.LastName +"</div>"+
                                   "<div style='   display:block;   width:100%;   margin:10px 0 10px 0;   padding:10px 15px;   background:#F0F0F0;   border:1px solid lightgrey;   '>     You have a new assignemt and the description is bellow:<br/>"+
                                   " <hr/>"+
                                   " <br/>"+
                                   " <b> The Category of this Request is</b> "+
                                   "<br/>"+
                                   RR.RepairRequestCategories.Categories +
                                   " <hr/>"+
                                   " <br/>"+
                                   "<b>The Urgency is:</b>"+
                                   " <br/>" + RR.RepairUrgency.Urgency +
                                   "<hr/>"+
                                   " <br/>"+
                                   " <b>The Decription of the request is:</b>"+
                                   "<br/>"+
                                  RR.ProblemDescription+
                                   " <hr/>"+
                                   "<br/>"+
                                   "</div>"+
                                   "<div style='font-size:20px; display:block; width:100%; background:#0071bc; height:50px;line-height:50px; padding:0 15px; border:1px solid lightgrey; color:white;' >For questions about this email Contact management at: " + RR.Buildings.BuildingPhone + "Find more information...  </div>";

               Gmail gmail = new Gmail("pointerwebapp", "dmc10040");
               MailMessage msg = new MailMessage("*****@*****.**", Worker.Email);
               msg.Subject = "New Assignment Notification";
               msg.Body = contenttobemail;
               msg.IsBodyHtml = true;
               
               //new
               PdfContractContent pdfContent = new PdfContractContent {
                   Address = RR.Buildings.Address + " " + RR.Tenant.Apartment.ApartmentNumber + " " + RR.Buildings.City +" " + RR.Buildings.State+" " + RR.Buildings.Zipcode,
                 Category = RR.RepairRequestCategories.Categories,
                 Priority = RR.RepairUrgency.Urgency,
                 Status = RR.Status,
                 Issued = RR.RequestedDate.Month.ToString() +"/"+ RR.RequestedDate.Day.ToString() +"/"+ RR.RequestedDate.Year.ToString(),
                 primaryContact = RR.Tenant.FirstName + " " + RR.Tenant.LastName,
                 PrimaryPhone = RR.Tenant.Phone,
                 PrimaryEmail = RR.Tenant.Username,
                 OfficeNotes = RR.RepairRequestNote.Notes,
                 RequestNumber =RR.RequestNumber,
                 problem = RR.ProblemDescription,
                 TenantInstruction = RR.Instructions_   
               };
               //new
               var result = pdfworker.CreateTable1(Server.MapPath("~/ContractPDF/"), Server.MapPath("~/img/"), Server.MapPath("~/fonts/"), pdfContent);
              
               Attachment attachment;
               attachment = new Attachment(Server.MapPath("~/ContractPDF/newContractFile.pdf"));
               msg.Attachments.Add(attachment);     

               gmail.Send(msg);
               attachment.Dispose();//needs to be dispose because the process is use and cannot be open twice at the same time.
               msg.Dispose();
           }
           else if (whichone == false) //Company========================================================================================================
           {
               pdfWorker pdfworker = new pdfWorker();
               RepairRequest RR = db.RepairRequest.Find(RepairRequestID);

               RepairRequestNote RN = new RepairRequestNote
               {
                   DateCreated = DateTime.Now,
                   Notes = Notes

               };

               db.RepairRequestNote.Add(RN);
               db.SaveChanges();

               RR.RepairRequestNoteID = RN.Id;
               RR.AssignContractorID = UserID;
               RR.AssignID = null;
               RR.Status = "Assigned";

               db.RepairRequest.Attach(RR);
               var Entry = db.Entry(RR);

               Entry.Property(c => c.RepairRequestNoteID).IsModified = true;
               Entry.Property(c => c.AssignContractorID).IsModified = true;
               Entry.Property(c => c.AssignID).IsModified = true;
               Entry.Property(c => c.Status).IsModified = true;
               db.SaveChanges();

               var Worker = db.Contractor.Where(c => c.Id == UserID).FirstOrDefault();

               string contenttobemail = " <div style='font-size:20px; display:block;  width:100%; background:#0071bc;  height:50px;  line-height:50px; padding:0 15px; border:1px solid lightgrey;   color:white;' >" +
                 Worker.CompanyName + "</div>" +
                                        "<div style='   display:block;   width:100%;   margin:10px 0 10px 0;   padding:10px 15px;   background:#F0F0F0;   border:1px solid lightgrey;   '>     You have a new assignemt and the description is bellow:<br/>" +
                                        " <hr/>" +
                                        " <br/>" +
                                        " <b> The Category of this Request is</b> " +
                                        "<br/>" +
                                        RR.RepairRequestCategories.Categories +
                                        " <hr/>" +
                                        " <br/>" +
                                        "<b>The Urgency is:</b>" +
                                        " <br/>" + RR.RepairUrgency.Urgency +
                                        "<hr/>" +
                                        " <br/>" +
                                        " <b>The Decription of the request is:</b>" +
                                        "<br/>" +
                                       RR.ProblemDescription +
                                        " <hr/>" +
                                        "<br/>" +
                                        "</div>" +
                                        "<div style='font-size:20px; display:block; width:100%; background:#0071bc; height:50px;line-height:50px; padding:0 15px; border:1px solid lightgrey; color:white;' >For questions about this email Contact management at: " + RR.Buildings.BuildingPhone + "Find more information...  </div>";


               Gmail gmail = new Gmail("pointerwebapp", "dmc10040");
               MailMessage msg = new MailMessage("*****@*****.**", Worker.Email);
               msg.Subject = "New Assignment Notification";
               msg.Body = contenttobemail;

               msg.IsBodyHtml = true;
               //new
               PdfContractContent pdfContent = new PdfContractContent
               {
                   Address = RR.Buildings.Address + " " + RR.Tenant.Apartment.ApartmentNumber + " " + RR.Buildings.City + " " + RR.Buildings.State + " " + RR.Buildings.Zipcode,
                   Category = RR.RepairRequestCategories.Categories,
                   Priority = RR.RepairUrgency.Urgency,
                   Status = RR.Status,
                   Issued = RR.RequestedDate.Month.ToString() + "/" + RR.RequestedDate.Day.ToString() + "/" + RR.RequestedDate.Year.ToString(),
                   primaryContact = RR.Tenant.FirstName + " " + RR.Tenant.LastName,
                   PrimaryPhone = RR.Tenant.Phone,
                   PrimaryEmail = RR.Tenant.Username,
                   OfficeNotes = RR.RepairRequestNote.Notes,
                   RequestNumber =RR.RequestNumber,
                   problem = RR.ProblemDescription,
                   TenantInstruction = RR.Instructions_
               };
               //new
               var result = pdfworker.CreateTable1(Server.MapPath("~/ContractPDF/"), Server.MapPath("~/img/"), Server.MapPath("~/fonts/"), pdfContent);

               Attachment attachment;
               attachment = new Attachment(Server.MapPath("~/ContractPDF/newContractFile.pdf"));
               msg.Attachments.Add(attachment); 
               gmail.Send(msg);
               attachment.Dispose();
           
           }
            
           var JSONdATA = Json("");
           return new JsonResult {Data = JSONdATA, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
       }
        private static string ResetPwdSendNotification(string ID)
        {
            PointerdbEntities db1 = new PointerdbEntities();
            try
            {

                 var password = GeneratePassword("8").ToString();
                 
                 PasswordHasher passwordsher = new PasswordHasher();
                 var pwdhashed = passwordsher.HashPassword(password);
               
                Tenant ObjTenant = db1.Tenant.Find(ID);
                AspNetUsers ObjAspnet = db1.AspNetUsers.Where(c => c.Email == ObjTenant.Username).FirstOrDefault();
                ObjAspnet.PasswordHash = pwdhashed;
                db1.AspNetUsers.Attach(ObjAspnet);
                var Entry = db1.Entry(ObjAspnet);
                Entry.Property(c=>c.PasswordHash).IsModified = true;
                db1.SaveChanges();

            if (ObjTenant!= null)
            {
                var aptNumber = db1.Apartment.Find(ObjTenant.aptID).ApartmentNumber;


                string string1 = "Hi " + ObjTenant.FirstName + " " + ObjTenant.LastName + ", your password has been reset by PointerWebApp.com ";
                string string2 = "The login information for apartment: " + aptNumber + " is below";
                string string3 = "Username: "******"Temporary password: "******"Click the on this http://localhost:64083/Account/Manage link and follow the instructions to initiate your account ";
                string string6 = "Company Description";
                string string7 = "Find more information...";

                string x = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n", string1, string2, string3, string4, string5, string6, string7);

                Gmail gmail = new Gmail("pointerwebapp", "Dmc10040!");
                MailMessage msg = new MailMessage("*****@*****.**", ObjTenant.Username);
                msg.Subject = "Pointer Security Password Reset Notification";
                msg.Body = x;
                gmail.Send(msg);
            }



            return "successful";

            }
            catch (Exception e)
            {
                throw e;
            }
        
        }
        public async Task<JsonResult> AddingTenant(TenantVM newTenant)
        {
            try { 
            
           
            if (ModelState.IsValid)
            {

                string password ="";
                if (newTenant.GenerateAutomaticPassword == false){ password = newTenant.Password;}
                else{ password = PasswordGenerator.GeneratePassword("8").ToString();}

              
                var UserID = RegisterUsers.InsertUser(newTenant.Username, newTenant.Phone, password);
                var result = RegisterUsers.InserToRole("Tenant", UserID);
                
                var tenantdb = new Tenant
                {
                    ID = UserID,
                    FirstName = newTenant.FirstName,
                    LastName = newTenant.LastName,
                    Phone = newTenant.Phone,
                    Created = DateTime.Now,
                    aptID = newTenant.aptID,
                    Username = newTenant.Username
                };
                db.Tenant.Add(tenantdb);
                
                await db.SaveChangesAsync();         
                
                if(newTenant.EmailNotification==true)
                {
                    var aptNumber = db.Apartment.Find(newTenant.aptID).ApartmentNumber;

                    
                    string string1 = "Hi " + newTenant.FirstName +" "+ newTenant.LastName + ", An Account has been created for you by PointerWebApp.com ";
                    string string2 = "The login information for apartment: "+ aptNumber+" is below";
                    string string3 = "Username: "******"Temporary password: "******"Click the on this http://localhost:64083/Account/Manage link and follow the instructions to initiate your account ";
                    string string6 = "Company Description";
                    string string7 = "Find more information...";

                    string x = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n", string1, string2, string3, string4, string5, string6, string7);

                    Gmail gmail = new Gmail("pointerwebapp", "Dmc10040!");
                    MailMessage msg = new MailMessage("*****@*****.**", tenantdb.Username);
                    msg.Subject = "Pointer Security New Account Notification";
                    msg.Body = x;
                    gmail.Send(msg);
                }

                var mydata = Json("");

                return new JsonResult { Data = mydata, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
             
                //return RedirectToAction("ApartmentProfile", new { ApartmentID = newTenant.aptID, BuildingID= newTenant.BuildingID });

            }
            }
                catch(Exception e){

                    ViewBag.Message = e.Message;
                
            }
            var mydata1 = Json("");

            return new JsonResult { Data = mydata1, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; ;
        }