public async Task<JsonResult> Preview(string SMSText, int SystemSMSTemplateID, int clientId)
 {
     var textResult = string.Empty;
     if (SystemSMSTemplateID > 0)
     {
         var result = await db.SystemSMSTemplates.FirstAsync(m => m.SystemSMSTemplateId == SystemSMSTemplateID);
         SMSText = result.Text;
     }
     var client = await db.Clients.FirstOrDefaultAsync(m => m.ClientId == clientId);
     if (client != null)
     {
         var smsSub = new SystemSMSTemplateModel(SMSText, client,db);
         textResult = smsSub.Generate();
     }
     return Json(textResult);
 }
        // GET: SystemSMSTemplates/Edit/5
        public async Task<ActionResult> Edit(int? id,int? clientId)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            SystemSMSTemplate systemSMSTemplate = await db.SystemSMSTemplates.FindAsync(id);
            if (systemSMSTemplate == null)
            {
                return HttpNotFound();
            }

            ViewBag.TemplateVariables = SystemSMSTemplateModel.GetVariableList();
            if (clientId.HasValue)
            {
                ViewBag.ClientID = clientId.Value;
                var client = db.Clients.FirstOrDefault(m => m.ClientId == clientId.Value);
                if (client != null)
                {
                    ViewBag.ClientID = clientId.Value;
                    var template = new SystemSMSTemplateModel(systemSMSTemplate.Text);
                    template.Populate(client, db);
                    ViewBag.Preview = template.Generate();
                }
            }
            return View(systemSMSTemplate);
        }
        public static void SendSMSEvent(AromaContext db, int systemSMSEventId, int clientId, Guid userId, Guid? source, params KeyValuePair<string, string>[] pars)
        {
            try
            {
                var systemSMSevent = db.SystemSMSEvents.Find(systemSMSEventId);
                var template = systemSMSevent.SystemSMSTemplate;
                var client = db.Clients.Find(clientId);
                if (!source.HasValue) source = userId;
                try
                {
                
                    if (systemSMSevent.Active)
                    {
                        

                        var contact = db.Contacts.Where(m => m.ContactTypeID == 3 && m.ClientID == clientId).FirstOrDefault();
                        var smsSub = new SystemSMSTemplateModel(template.Text, client, db);
                        smsSub.EventInfo = pars;
                        var textResult = smsSub.Generate();
                        if (contact != null)
                        {

                            var sms = (from item in db.SystemSMSes
                                       where item.ClientID == clientId
                                       && item.SMSDescription == textResult
                                       select item).FirstOrDefault();
                            if (sms == null)
                            {
                                sms = new SystemSMS()
                                {
                                    Active = true,
                                    ClientID = clientId,
                                    iDate = DateTime.Now,
                                    Number = contact.ContactName,
                                    SMSDescription = textResult,
                                    Source = source,
                                    SystemSMSStatusId = 1
                                };
                                db.SystemSMSes.Add(sms);
                                db.SaveChanges();
                            }
                        }
                        else
                        {
                            var ticket = new SupportTicket()
                            {
                                ClientID = clientId,
                                Description = string.Format("An attemt was made to send a SMS to this client ({0}) but the cell number could not be found.\r\n{1}", clientId, textResult),
                                iDate = DateTime.Now,
                                SupportTicketStatusID = 1,
                                SupportTicketTypeId = 2,
                                UserID = userId
                            };
                            db.SupportTickets.Add(ticket);
                            db.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    try
                    {
                        var ticket = new SupportTicket()
                        {
                            ClientID = client.ClientId,
                            Description = string.Format("Unable to create system sms:\"{0}\" Message:{1}", template.Description, ex.Message),
                            iDate = DateTime.Now,
                            SupportTicketStatusID = 1,
                            SupportTicketTypeId = 2,
                            UserID = userId
                        };
                        db.SupportTickets.Add(ticket);
                        db.SaveChanges();
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
            catch
            {
                throw;
            }
        }
        public async Task<ActionResult> Edit([Bind(Include = "SystemSMSTemplateId,Description,Text,Active")] SystemSMSTemplate systemSMSTemplate, int? clientId, bool preview)
        {
            if (ModelState.IsValid)
            {
                db.Entry(systemSMSTemplate).State = EntityState.Modified;
                await db.SaveChangesAsync();

                if (preview)
                {
                    if (!clientId.HasValue) clientId = db.Clients.FirstOrDefault(m=>m.Active)?.ClientId;
                    return RedirectToAction("Edit", new {id=systemSMSTemplate.SystemSMSTemplateId, clientId=clientId });
                }
                else
                {
                    return RedirectToAction("Index", new { clientId = clientId });
                }
            }

            ViewBag.TemplateVariables = SystemSMSTemplateModel.GetVariableList();
            if (clientId.HasValue)
            {
                ViewBag.ClientID = clientId.Value;
                var client = db.Clients.FirstOrDefault(m => m.ClientId == clientId.Value);
                if (client != null)
                {
                    ViewBag.ClientID = clientId.Value;
                    var template = new SystemSMSTemplateModel(systemSMSTemplate.Text);
                    template.Populate(client,db);
                    ViewBag.Preview = template.Generate();
                }
            }
            return View(systemSMSTemplate);
        }