public static CallResult <ComposeEmailViewModel> FromTemplate(IUnitOfWork db,
                                                                      IEmailService emailService,
                                                                      string orderId,
                                                                      CompanyDTO company,
                                                                      string byName,
                                                                      EmailTypes type,
                                                                      EmailAccountDTO emailAccountInfo)
        {
            try
            {
                ComposeEmailViewModel model = new ComposeEmailViewModel();

                orderId = OrderHelper.RemoveOrderNumberFormat(orderId);

                var order = db.Orders.GetAll().FirstOrDefault(q => q.AmazonIdentifier.ToLower() == orderId || q.CustomerOrderId == orderId);

                if (order != null)
                {
                    IEmailInfo info = emailService.GetEmailInfoByType(db,
                                                                      type,
                                                                      company,
                                                                      byName,
                                                                      order?.AmazonIdentifier,
                                                                      new Dictionary <string, string>(),
                                                                      null,
                                                                      null);

                    model = ComposeEmailViewModel.BuildFrom(db, info);
                }

                model.FromName  = emailAccountInfo.DisplayName;
                model.FromEmail = emailAccountInfo.FromEmail;

                if (!StringHelper.ContainsNoCase(model.Subject, "[Important]") &&
                    (model.Market == MarketType.Amazon ||
                     model.Market == MarketType.AmazonEU ||
                     model.Market == MarketType.AmazonAU ||
                     model.Market == MarketType.None))
                {
                    var existEmailFromCustomer = db.Emails.GetAll().Any(e => e.From == model.ToEmail);
                    if (!existEmailFromCustomer)
                    {
                        model.Subject = "[Important] " + model.Subject;
                    }
                }

                return(CallResult <ComposeEmailViewModel> .Success(model));
            }
            catch (Exception ex)
            {
                return(CallResult <ComposeEmailViewModel> .Fail(ex.Message, ex));
            }
        }
        public static ComposeEmailViewModel BuildFrom(IUnitOfWork db, IEmailInfo emailInfo)
        {
            var model = new ComposeEmailViewModel();

            model.EmailType   = emailInfo.EmailType;
            model.OrderNumber = emailInfo.Tag;
            model.Market      = emailInfo.Market;

            if (emailInfo.From != null)
            {
                model.FromEmail = emailInfo.From.Address;
                model.FromName  = emailInfo.From.DisplayName;
            }

            if (emailInfo.ToList != null && emailInfo.ToList.Count > 0)
            {
                model.ToEmail = emailInfo.ToList[0].Address;
                model.ToName  = emailInfo.ToList[0].DisplayName;
            }

            model.Body    = emailInfo.Body;
            model.Subject = emailInfo.Subject;

            var order = db.Orders.GetByCustomerOrderNumber(emailInfo.Tag);

            if (order != null)
            {
                var label = db.Labels.GetByOrderIdAsDto(order.Id)
                            .OrderByDescending(l => l.LabelPurchaseDate)
                            .FirstOrDefault(l => !String.IsNullOrEmpty(l.TrackingNumber));
                if (label != null)
                {
                    model.ShipmentProvider = (ShipmentProviderType)label.ShipmentProviderType;
                }
            }

            return(model);
        }
        public static CallResult <ComposeEmailViewModel> GetTemplateInfo(IUnitOfWork db,
                                                                         IEmailService emailService,
                                                                         CompanyDTO company,
                                                                         string byName,
                                                                         EmailTypes type,
                                                                         EmailAccountDTO emailAccountInfo,
                                                                         string orderNumber,
                                                                         long?replyToId)
        {
            var model  = new ComposeEmailViewModel();
            var result = CallResult <ComposeEmailViewModel> .Success(model);

            if (!String.IsNullOrEmpty(orderNumber))
            {
                result = ComposeEmailViewModel.FromTemplate(db,
                                                            emailService,
                                                            orderNumber,
                                                            company,
                                                            byName,
                                                            (EmailTypes)type,
                                                            emailAccountInfo);

                if (result.IsSuccess)
                {
                    model = result.Data;
                }
            }

            var useReSubject = false;

            if (replyToId.HasValue)
            {
                var replyToEmail = db.Emails.Get(replyToId.Value);
                if (replyToEmail != null)
                {
                    model.Subject = "RE: " + replyToEmail.Subject;

                    if (String.IsNullOrEmpty(model.Body)) //For Custom template
                    {
                        model.Body = "<br/><br/><br/><br/>";
                    }
                    model.Body = model.Body + "<hr/><br/>" + replyToEmail.Message;

                    if (replyToEmail.From != "*****@*****.**" &&
                        !(StringHelper.ContainsNoCase(replyToEmail.From, "@walmart.com") &&
                          StringHelper.ContainsNoCase(replyToEmail.Subject, "Walmart Escalation")))      //NOTE: when reply to walmart support email do not use them
                    {
                        model.ToEmail = replyToEmail.From;
                    }

                    useReSubject = true;
                }
            }

            if ((model.Market == MarketType.Amazon ||
                 model.Market == MarketType.AmazonEU ||
                 model.Market == MarketType.AmazonAU))
            {
                if (!useReSubject)
                {
                    if (StringHelper.ContainsNoCase(model.Subject, "[Important]"))
                    {
                        model.Subject = "[Important] ";
                    }
                    else
                    {
                        model.Subject = "";
                    }
                    model.Subject += "Additional Information Required (Order: " + orderNumber + ")";
                }
            }

            model.FromName  = emailAccountInfo.DisplayName;
            model.FromEmail = emailAccountInfo.FromEmail;

            model.ReplyToEmailId = replyToId;

            return(result);
        }