public SendSettingsEntity GetSendSettings()
        {
            using (var context = new CorpSMSEntities())
            {
                context.CommandTimeout = 0;
                var setting = context.SendSettings.Select(p => p).FirstOrDefault();
                var entity = new SendSettingsEntity()
                {
                    IsLive = setting.IsLive,
                    ReturnMessageSuccessCount = setting.ReturnMessageSuccessCount,
                    ReturnMessageFailedCount = setting.ReturnMessageFailedCount,
                    ReturnCredits = setting.ReturnCredits,
                    ReturnEntriesSuccessStatus = setting.ReturnEntriesSuccessStatus,
                    ReturnEntriesFailedStatus = setting.ReturnEntriesFailedStatus,
                    DefaultText1 = setting.DefaultText1,
                    DefaultText2 = setting.DefaultText2,
                    DefaultDate = DateTime.Now.ToString(setting.DefaultDateFormat),
                    DefaultTime = DateTime.Now.ToString(setting.DefaultTimeFormat),
                    DefaultSender = setting.DefaultSender,
                    Flash = setting.Flash,
                    Type = setting.Type,
                    CostCentre = setting.CostCentre      
                };

                return entity;
            }
        }
        public GeneralSettingsEntity GetSentSettings(int accountID)
        {
            using (var context = new CorpSMSEntities())
            {
                var setting = context.SentSettings.Where(p => p.AccountID == accountID && p.IsActive == true).FirstOrDefault();
                var entity = new GeneralSettingsEntity()
                {
                    LatestId = setting.LatestID,
                    RecordCount = setting.RecordCount,
                    ReturnColumns = setting.ReturnColumns,
                    DateFormat = setting.DateFormat
                };

                return entity;
            }
        }
        public ShortCodeSettingsEntity GetShortCodeSettings(int accountID)
        {
            using (var context = new CorpSMSEntities())
            {
                ShortCodeSettingsEntity entity = null;
                var setting = context.ShortCodeSettings.Where(p => p.AccountID == accountID && p.IsActive == true).FirstOrDefault();
                if (setting != null)
                {
                    entity = new ShortCodeSettingsEntity()
                    {
                        LatestId = setting.LatestID,
                        DateFormat = setting.DateFormat
                    };
                }

                return entity;
            }
        }
        public ICollection<MessageQueueEntity> GetQueuedMessages(Guid batchId)
        {
            using (var context = new CorpSMSEntities())
            {
                var query = context.MessageQueues.Where(p => p.BatchId == batchId);

                List<MessageQueueEntity> messageList = new List<MessageQueueEntity>();
                foreach (var message in query)
                {
                    messageList.Add(MessageQueueToEntity(message));
                }

                return messageList;
            }
        }
        public void SetCredit(CreditEntity entity)
        {
            try
            {
                using (var context = new CorpSMSEntities())
                {
                    var credit = Credit.CreateCredit(
                        0,
                        entity.AccountId,
                        entity.Amount,
                        DateTime.Now);

                    context.AddToCredits(credit);
                    context.SaveChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ICollection<MessageEntity> GetShortCodeMessages(DateTime startTime, DateTime endTime)
        {
            using (var context = new CorpSMSEntities())
            {
                List<MessageEntity> messages = new List<MessageEntity>();
                context.GetShortCodeMessage("AsymmetricKey1", "GWEST2003%", startTime, endTime).ForAll(message =>
                    {
                        messages.Add(ConvertToMessageEntity(message));
                    });

                return messages;
            }
        }
        public ICollection<MessageEntity> GetDeliveryReportByOriginId(int originId, int uniqueId)
        {
            using (var context = new CorpSMSEntities())
            {
                List<MessageEntity> messages = new List<MessageEntity>();
                var query = context.Messages.Include("MessageDetails")
                    .Where(p => p.OriginId == originId && p.UniqueId == uniqueId);

                query.ForAll(message =>
                {
                    messages.Add(ConvertToMessageEntity(message));
                });

                return messages;
            }
        }
        public ICollection<MessageEntity> GetDeliveryReports()
        {
            using (var context = new CorpSMSEntities())
            {
                List<MessageEntity> messages = new List<MessageEntity>();
                var query = context.Messages.Include("MessageDetails");

                query.ForAll(message =>
                    {
                        messages.Add(ConvertToMessageEntity(message));
                    });

                return messages;
            }
        }
        /// <summary>
        /// Remove the Message from the Queue if it has been send successfully or failed more than 5 times
        /// </summary>
        /// <param name="uniqueID"></param>
        public void RemoveSentMessages(int uniqueID)
        {
            using (var context = new CorpSMSEntities())
            {

                context.spRemoveSentMessages(uniqueID);
            }
        }
 public void InsertMessageDetail(ShortCodeDataEntity entity)
 {
     try
     {
         using (var context = new CorpSMSEntities())
         {
             context.SetShortCodeMessage(
                 entity.ShortCode,
                 TypeSafety.GetValue<string>(entity.Number),
                 TypeSafety.GetValue<string>(entity.Text),
                 entity.ChangeId,
                 TypeSafety.GetValue<DateTime>(entity.DateReceived),
                 entity.BatchId,
                 "AsymmetricKey1");
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
        public MessageReplyEntity InsertMessageReply(MessageReplyEntity entity)
        {
            using (var context = new CorpSMSEntities())
            {
                var reply = context.MessageReplies.Where(p => p.MessageReplyId == entity.MessageReplyId).FirstOrDefault();

                if (reply != null)
                {
                    // Detach the entity first
                    context.Detach(reply);

                    reply = EntityToMessageReply(entity, reply);

                    // Attach the entity again.
                    context.Attach(reply);
                    context.ObjectStateManager.ChangeObjectState(reply, System.Data.EntityState.Modified);
                }
                else
                {
                    reply = EntityToMessageReply(entity, new MessageReply());
                    context.AddToMessageReplies(reply);
                }

                context.SaveChanges();

                return MessageReplyToEntity(reply);
            }
        }
        public DeliveryReportEntity InsertDeliveryReport(DeliveryReportEntity entity)
        {
            using (var context = new CorpSMSEntities())
            {
                var report = context.DeliveryReports.Where(p => p.DeliveryReportId == entity.DeliveryReportId).FirstOrDefault();

                if (report != null)
                {
                    // Detach the entity first
                    context.Detach(report);

                    report = EntityToDeliveryReport(entity, report);

                    // Attach the entity again.
                    context.Attach(report);
                    context.ObjectStateManager.ChangeObjectState(report, System.Data.EntityState.Modified);
                }
                else
                {
                    report = EntityToDeliveryReport(entity, new DeliveryReport());
                    context.AddToDeliveryReports(report);
                }

                context.SaveChanges();

                return DeliveryReportToEntity(report);
            }
        }
        public MessageEntity GetMessageByRowGuid(Guid guid)
        {
            using (var context = new CorpSMSEntities())
            {
                var message = context.Messages.Where(p => p.RowGuid == guid).FirstOrDefault();
                MessageEntity entity = null;

                if (message != null)
                {
                    entity = MessageToEntity(message);
                }

                return entity;
            }
        }
        public MessageEntity SetMessageForced(MessageEntity entity)
        {
            using (var context = new CorpSMSEntities())
            {
                var message = context.Messages.Where(p => p.RowGuid == entity.RowGuid).FirstOrDefault();

                if (message != null)
                {
                    // Detach the entity first
                    context.Detach(message);

                    message = EntityToMessage(entity, message);

                    // Attach the entity again.
                    context.Attach(message);
                    context.ObjectStateManager.ChangeObjectState(message, System.Data.EntityState.Modified);
                }
                else
                {
                    message = EntityToMessage(entity, new Message());
                    context.AddToMessages(message);
                }

                context.SaveChanges();

                return MessageToEntity(message);
            }
        }
        public void UpdateShortCodeSettingsLatestID(long latestID, int accountID)
        {
            using (var context = new CorpSMSEntities())
            {
                var setting = context.ShortCodeSettings.Where(p => p.AccountID == accountID).FirstOrDefault();

                if (setting != null)
                {
                    // Detach the entity first
                    context.Detach(setting);

                    setting.LatestID = latestID;

                    // Attach the entity again.
                    context.Attach(setting);
                    context.ObjectStateManager.ChangeObjectState(setting, System.Data.EntityState.Modified);

                    context.SaveChanges();
                }
            }
        }
        public ICollection<MessageQueueEntity> GetTopQueuedMessages()
        {
            using (var context = new CorpSMSEntities())
            {
                var query = ( from item in context.MessageQueues orderby item.Accessed ascending select item).Take(1000);         

                List<MessageQueueEntity> messageList = new List<MessageQueueEntity>();
                foreach (var message in query)
                {
                    messageList.Add(MessageQueueToEntity(message));
                    message.Accessed = DateTime.Now;                    
                }

                context.SaveChanges();

                return messageList;
            }
        }
        public ICollection<AccountEntity> GetAccounts()
        {
            using (var context = new CorpSMSEntities())
            {
                var query = context.Accounts;

                List<AccountEntity> accountList = new List<AccountEntity>();
                foreach (var account in query)
                {
                    accountList.Add(new AccountEntity()
                        {
                            AccountId = account.AccountID,
                            Description = account.Description,
                            CountryCodePrefix = account.CountryCodePrefix,
                            Username = account.Username,
                            Password = account.Password
                        });
                }

                return accountList;
            }
        }
        public AccountEntity GetDefaultAccount()
        {
            using (var context = new CorpSMSEntities())
            {
                var query = context.Accounts.Where(p => p.IsDefault).FirstOrDefault();
                AccountEntity entity = null;

                if (query != null)
                {
                    entity = new AccountEntity()
                   {
                       AccountId = query.AccountID,
                       CountryCodePrefix = query.CountryCodePrefix,
                       Description = query.Description,
                       Username = query.Username,
                       Password = query.Password
                   };
                }

                return entity;
            }
        }
 /// <summary>
 /// Remove the Message from the Queue in the Barred List
 /// </summary>
 public void RemoveBarredMessages()
 {
     using (var context = new CorpSMSEntities())
     {
         context.spRemoveBarredMessages();
     }
 }
 /// <summary>
 /// RAdd numbers  in the Barred List
 /// </summary>
 public void AddStopReplies()
 {
     using (var context = new CorpSMSEntities())
     {
         context.spAddStopReplies();
     }
 }