public IHttpActionResult GetAgentThreads(long UserId, int Page)
        {
            List <MessageThreadModel> threads = null;

            try
            {
                using (AppDBContext context = new AppDBContext())
                {
                    var userThreads = new MessageThreadRepository(context).GetByAgentId(UserId);
                    userThreads = userThreads.Skip((Page - 1) * Constant.Paging.MESSAGE_THREADS_PER_PAGE).
                                  Take(Constant.Paging.MESSAGE_THREADS_PER_PAGE).ToList();
                    threads = userThreads.Select(t => new MessageThreadModel
                    {
                        Id        = t.Id,
                        AgentId   = t.AgentId,
                        BuyerId   = t.BuyerId,
                        RequestId = t.RequestId,
                        Date      = t.CreatedTime.GetAdjustedTime().ToString("dd/MMM"),
                        Time      = t.CreatedTime.GetAdjustedTime().ToString("HH:mm"),
                        Messages  = t.Messages.OrderBy(
                            m => m.Time).Select(m => new MessageModel
                        {
                            Id          = m.Id,
                            ThreadId    = m.ThreadId,
                            RecieverId  = m.RecieverId,
                            SenderId    = m.SenderId,
                            MessageText = m.MessageText,
                            Status      = m.Status,
                            QuotationId = m.QuotationId ?? 0,
                            Time        = m.Time.GetAdjustedTime().ToString("yyyy-MM-dd HH:mm")
                        }).ToList()
                    }).ToList();

                    foreach (var thread in threads)
                    {
                        var userRepo        = new UserRepository(context);
                        var userProfileRepo = new UserProfileRepository(context);
                        var buyer           = userRepo.GetByUserId(thread.BuyerId);
                        var agent           = userRepo.GetByUserId(thread.AgentId);
                        var buyerProfile    = userProfileRepo.GetByUserId(thread.BuyerId);
                        var agentProfile    = userProfileRepo.GetByUserId(thread.AgentId);
                        var request         = new ServiceRequestRepository(context).GetById(thread.RequestId);
                        thread.AgentName = agent.Name;
                        if (agentProfile != null)
                        {
                            thread.AgentName = agentProfile.FirstName + " " + agentProfile.LastName;
                        }
                        thread.CompanyName = agent.Company.Name;
                        thread.BuyerName   = buyer.Name;
                        if (buyerProfile != null)
                        {
                            thread.BuyerName = buyerProfile.FirstName + " " + buyerProfile.LastName;
                        }

                        thread.Description        = "Vehicle No: " + request.VehicleNo + " / Request: " + request.Code;
                        thread.VehicleNo          = request.VehicleNo;
                        thread.UnreadMessageCount = thread.Messages.Count(
                            m => m.Status == (int)Constant.MessageStatus.Initial && m.RecieverId == UserId);

                        foreach (var message in thread.Messages)
                        {
                            var sender = userRepo.GetByUserId(message.SenderId);
                            message.SenderName = sender.Name;
                            var senderProfile = userProfileRepo.GetByUserId(message.SenderId);
                            if (senderProfile != null)
                            {
                                message.SenderName = senderProfile.FirstName + " " + senderProfile.LastName;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(typeof(MessageController), ex.Message + ex.StackTrace, LogType.ERROR);
                return(InternalServerError(ex));
            }

            return(Ok(threads));
        }
        public IHttpActionResult GetBuyerThreads(long UserId, int Page)
        {
            List <MessageThreadModel> threads = null;

            try
            {
                using (AppDBContext context = new AppDBContext())
                {
                    var userThreads = new MessageThreadRepository(context).GetByBuyerId(UserId);
                    userThreads = userThreads.Skip((Page - 1) * Constant.Paging.MESSAGE_THREADS_PER_PAGE).
                                  Take(Constant.Paging.MESSAGE_THREADS_PER_PAGE).ToList();
                    threads = userThreads.Select(t => new MessageThreadModel
                    {
                        Id        = t.Id,
                        AgentId   = t.AgentId,
                        BuyerId   = t.BuyerId,
                        RequestId = t.RequestId,
                        Date      = t.CreatedTime.GetAdjustedTime().ToString("dd/MMM"),
                        Time      = t.CreatedTime.GetAdjustedTime().ToString("HH:mm"),
                        Messages  = t.Messages.OrderBy(
                            m => m.Time).Select(m => new MessageModel
                        {
                            Id          = m.Id,
                            ThreadId    = m.ThreadId,
                            RecieverId  = m.RecieverId,
                            SenderId    = m.SenderId,
                            MessageText = m.MessageText,
                            Status      = m.Status,
                            QuotationId = m.QuotationId ?? 0,
                            Time        = m.Time.GetAdjustedTime().ToString("yyyy-MM-dd HH:mm")
                        }).ToList()
                    }).ToList();

                    foreach (var thread in threads)
                    {
                        var userRepo        = new UserRepository(context);
                        var userProfileRepo = new UserProfileRepository(context);
                        var buyer           = userRepo.GetByUserId(thread.BuyerId);
                        var agent           = userRepo.GetByUserId(thread.AgentId);
                        var buyerProfile    = userProfileRepo.GetByUserId(thread.BuyerId);
                        var agentProfile    = userProfileRepo.GetByUserId(thread.AgentId);
                        var request         = new ServiceRequestRepository(context).GetById(thread.RequestId);
                        thread.AgentName = agent.Name;
                        if (agentProfile != null)
                        {
                            thread.AgentName = agentProfile.FirstName + " " + agentProfile.LastName;
                        }
                        thread.CompanyName = agent.Company.Name;
                        thread.BuyerName   = buyer.Name;
                        if (buyerProfile != null)
                        {
                            thread.BuyerName = buyerProfile.FirstName + " " + buyerProfile.LastName;
                        }

                        thread.Description        = "Vehicle No: " + request.VehicleNo + " / Request: " + request.Code;
                        thread.VehicleNo          = request.VehicleNo;
                        thread.UnreadMessageCount = thread.Messages.Count(
                            m => m.Status == (int)Constant.MessageStatus.Initial && m.RecieverId == UserId);

                        foreach (var message in thread.Messages)
                        {
                            var sender = userRepo.GetByUserId(message.SenderId);
                            message.SenderName = sender.Name;
                            var senderProfile = userProfileRepo.GetByUserId(message.SenderId);
                            if (senderProfile != null)
                            {
                                message.SenderName = senderProfile.FirstName + " " + senderProfile.LastName;
                            }
                        }
                    }

                    if (Page == 1)
                    {
                        var            promotion = new PromotionRepository(context).GetLatestPromotion(1); //TODO change the type to param
                        PromotionModel promModel = null;

                        if (promotion != null)
                        {
                            promModel = new PromotionModel
                            {
                                Id          = promotion.Id,
                                Title       = promotion.Title,
                                Header      = promotion.Header,
                                Description = promotion.Description,
                                CreatedDate = promotion.CreatedDate?.ToString(Constant.DateFormatType.YYYYMMDD),
                                Status      = promotion.Status ?? 0,
                                Type        = ((promotion.Type ?? 0) == Constant.PromotionType.OFFER) ? "Offers" : "Promotions"
                            };
                            MessageThreadModel promotionEntry = new MessageThreadModel();
                            promotionEntry.Description = promModel.Type;
                            promotionEntry.Promotion   = promModel;
                            if (threads == null)
                            {
                                threads = new List <MessageThreadModel>();
                            }
                            threads.Insert(0, promotionEntry);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(typeof(MessageController), ex.Message + ex.StackTrace, LogType.ERROR);
                return(InternalServerError(ex));
            }

            return(Ok(threads));
        }