private string GenerateRepCommentsText(EmailTemplateViewModel item, HTMLParserService parser)
        {
            Hashtable blockVars = new Hashtable();

            blockVars.Add("RepComments", item.RepComments);
            string parsedHtml = parser.ParseBlock("RepComments", blockVars);

            return(parsedHtml);
        }
        /// <summary>
        /// Parse all blocks in template
        /// </summary>
        private void ParseBlocks()
        {
            //int idxPrevious = 0;
            int idxCurrent = 0;

            while ((idxCurrent = this._strTemplateBlock.IndexOf(this.BlockTagBeginBegin, idxCurrent)) != -1)
            {
                string BlockName;
                int    idxBlockBeginBegin, idxBlockBeginEnd, idxBlockEndBegin;

                idxBlockBeginBegin = idxCurrent;
                idxCurrent        += this.BlockTagBeginBegin.Length;

                // Searching for BlockBeginEnd Index

                idxBlockBeginEnd = this._strTemplateBlock.IndexOf(this.BlockTagBeginEnd, idxCurrent);
                if (idxBlockBeginEnd == -1)
                {
                    throw new Exception("Could not find BlockTagBeginEnd");
                }

                // Getting Block Name

                BlockName  = this._strTemplateBlock.Substring(idxCurrent, (idxBlockBeginEnd - idxCurrent));
                idxCurrent = idxBlockBeginEnd + this.BlockTagBeginEnd.Length;

                // Getting End of Block index

                string EndBlockStatment = this.BlockTagEndBegin + BlockName + this.BlockTagEndEnd;
                idxBlockEndBegin = this._strTemplateBlock.IndexOf(EndBlockStatment, idxCurrent);
                if (idxBlockEndBegin == -1)
                {
                    throw new Exception("Could not find End of Block with name '" + BlockName + "'");
                }

                // Add Block to Dictionary

                HTMLParserService block = new HTMLParserService();
                block.TemplateBlock = this._strTemplateBlock.Substring(idxCurrent, (idxBlockEndBegin - idxCurrent));
                this._Blocks.Add(BlockName, block);

                // Remove Block Declaration From Template

                this._strTemplateBlock = this._strTemplateBlock.Remove(idxBlockBeginBegin, (idxBlockEndBegin - idxBlockBeginBegin));
                //Added to remove the block tag and inserting the tag list
                this._strTemplateBlock = this._strTemplateBlock.Insert(_strTemplateBlock.IndexOf(BlockTagEndBegin), BlockTagEndEnd + BlockName + BlockTagEndEnd);
                this._strTemplateBlock = this._strTemplateBlock.Remove(_strTemplateBlock.IndexOf(BlockTagEndBegin), (BlockTagEndBegin.Length + BlockName.Length + BlockTagEndEnd.Length));

                //End Add
                idxCurrent = idxBlockBeginBegin;
            }
        }
        public void InitialiseHTMLParser(EmailTemplateViewModel item, string template, string subject, string type = null)
        {
            EmailService mailService  = new EmailService();
            Hashtable    templateVars = new Hashtable();

            templateVars.Add("DefaultDW", item.EmailDWTemplateList.FirstOrDefault().DWName);

            HTMLParserService parser;
            string            baseTemplatepath = ConfigurationManager.AppSettings["EmailTemplate"];

            switch (template.ToLower())
            {
            case "initial":
                parser = new HTMLParserService(baseTemplatepath + "TemplateInitial.html", templateVars);
                break;

            case "idle":
                parser = new HTMLParserService(baseTemplatepath + "TemplateIdle.html", templateVars);
                break;

            case "active":
                parser = new HTMLParserService(baseTemplatepath + "TemplateActive.html", templateVars);
                break;

            case "outstanding":
                parser = new HTMLParserService(baseTemplatepath + "TemplateOutstanding.html", templateVars);
                break;

            case "initialtext":
                parser = new HTMLParserService(baseTemplatepath + "TemplateInitialText.html", templateVars);
                break;

            case "initialnewuser":
                parser = new HTMLParserService(baseTemplatepath + "TemplateInitalNewUser.html", templateVars);
                break;

            default:
                parser = new HTMLParserService(baseTemplatepath + "TemplateIdle.html", templateVars);
                break;
            }


            string DefaultDWLink = GenerateAnchorList(item.EmailDWTemplateList, parser, "DefaultDWLink");

            if (!string.IsNullOrEmpty(DefaultDWLink))
            {
                templateVars.Add("DefaultDWLink", DefaultDWLink);
            }

            //added by chandana for rep Comments
            bool ContainsRepComments = !string.IsNullOrEmpty(item.RepComments) ? true : false;

            templateVars.Add("ContainsRepComments", ContainsRepComments);
            string RepComments = GenerateRepCommentsText(item, parser);

            templateVars.Add("RepComments", RepComments);

            string videoLink = GenerateAnchorList(item.EmailDWTemplateList, parser, "VideoLink");

            if (!string.IsNullOrEmpty(videoLink))
            {
                templateVars.Add("VideoLink", videoLink);
            }

            string RepDetails = GenerateAnchorList(item.EmailDWTemplateList, parser, "RepDetails", item);

            templateVars.Add("RepDetails", RepDetails);


            bool isMultipleRows = item.EmailDWTemplateList.Count > 1 ? true : false;

            templateVars.Add("IsMultipleRows", isMultipleRows);

            bool ContainsItems = item.EmailDWTemplateList.FirstOrDefault().LstISBN.Count == 0 ? false : true;

            templateVars.Add("ContainsItems", ContainsItems);


            //if (template.ToLower() != "initial")
            //{

            string anchorList = GenerateAnchorList(item.EmailDWTemplateList, parser, "Link");

            if (!string.IsNullOrEmpty(anchorList))
            {
                templateVars.Add("Link", anchorList);
            }
            //}

            MailMessage message = new MailMessage();

            string mailBody = string.Empty;

            //added by faraaz
            if (template.ToLower() != "initialnewuser" && template.ToLower() != "initialtext")
            {
                EmailLinkedResourceModel imageLinkedResource = GenerateImageList(item.EmailDWTemplateList, parser);
                if (!string.IsNullOrEmpty(imageLinkedResource.HtmlImageContent))
                {
                    templateVars.Add("image", imageLinkedResource.HtmlImageContent);
                }
                mailBody = parser.Parse();
                //Check if image resource is attached
                AlternateView view = AlternateView.CreateAlternateViewFromString(mailBody, null, System.Net.Mime.MediaTypeNames.Text.Html);
                if (imageLinkedResource.ListImageResource.Count > 0)
                {
                    foreach (LinkedResource lstimage in imageLinkedResource.ListImageResource)
                    {
                        view.LinkedResources.Add(lstimage);
                    }
                    message.AlternateViews.Add(view);
                }
            }


            if (item.EmailDWTemplateList.FirstOrDefault() != null)
            {
                string emailType = string.IsNullOrEmpty(type) && type == null ? "DW" : type;
                if (!IsEmailSent(item.EmailDWTemplateList.FirstOrDefault().QuoteID, item.PersonID, item.FromAddress, item.ToAddress, DateTime.UtcNow, emailType))
                {
                    mailBody = parser.Parse();
                    //Enabled dynamic "from email" i.e item.FromAddress instead of "null" to test sending email from associated rep to Users.
                    if (!string.IsNullOrEmpty(item.ToAddress) && !string.IsNullOrEmpty(item.FromAddress))
                    {
                        mailService.SendMail(subject, mailBody, true, false, null, message, item.FromAddress, item.ToAddress, emailType, item.EmailDWTemplateList.FirstOrDefault().QuoteID, item.DisplayName, item.PersonID, item.EmailDWTemplateList.FirstOrDefault().DWName);
                    }
                }
            }
        }
        private string GenerateAnchorList(List <EmailTemplateModel> lstEmailTemplatemodel, HTMLParserService parser, string type, EmailTemplateViewModel item = null)
        {
            Hashtable links      = new Hashtable();
            string    dwBaseUrl  = ConfigurationManager.AppSettings["DWEmailUrl"];
            string    dwVideoUrl = ConfigurationManager.AppSettings["DWVideoUrl"];

            if (type == "DefaultDWLink")
            {
                EmailTemplateModel etm = lstEmailTemplatemodel.Take(1).FirstOrDefault();
                links.Add(etm.DWName + "~" + etm.QuoteID, dwBaseUrl + etm.QuoteID + "&type=Decision Wizard");
            }
            else if (type == "Link")
            {
                foreach (EmailTemplateModel etm in lstEmailTemplatemodel.Skip(1))
                {
                    links.Add(etm.DWName + "~" + etm.QuoteID, dwBaseUrl + etm.QuoteID + "&type=Decision Wizard");
                }
            }
            else if (type == "VideoLink")
            {
                links.Add("videolink", dwVideoUrl);
            }
            else
            {
                if (item != null)
                {
                    links.Add("repDetails", item.RepFirstName + "~" + item.FromAddress);
                }
            }
            //generate others DW's list
            StringBuilder         linksBlock   = new StringBuilder();
            IDictionaryEnumerator myEnumerator = links.GetEnumerator();

            while (myEnumerator.MoveNext())
            {
                Hashtable blockVars = new Hashtable();
                if (type != "VideoLink")
                {
                    if (item != null)
                    {
                        blockVars.Add("RepName", myEnumerator.Value.ToString().Split('~')[0]);
                        blockVars.Add("RepPhone", "");
                        blockVars.Add("RepEmailAddress", myEnumerator.Value.ToString().Split('~')[1]);
                    }
                    else
                    {
                        blockVars.Add("Url", myEnumerator.Value);
                        blockVars.Add("DWName", myEnumerator.Key.ToString().Split('~')[0]);
                    }
                }
                else
                {
                    blockVars.Add("VideoUrl", myEnumerator.Value);
                }
                string parsedHtml = parser.ParseBlock(type, blockVars);

                if (!string.IsNullOrEmpty(parsedHtml))
                {
                    linksBlock.Append(parsedHtml);
                }
            }

            return(linksBlock.ToString());
        }
        private EmailLinkedResourceModel GenerateImageList(List <EmailTemplateModel> lstEmailTemplatemodel, HTMLParserService parser)
        {
            Hashtable                imgpaths         = new Hashtable();
            StringBuilder            imagesblock      = new StringBuilder();
            List <LinkedResource>    lstImageResouces = new List <LinkedResource>();
            LinkedResource           resource;
            MailMessage              message           = new MailMessage();
            EmailLinkedResourceModel imagelinkResource = new EmailLinkedResourceModel();

            List <string> lstIsbns = lstEmailTemplatemodel.FirstOrDefault() != null?lstEmailTemplatemodel.FirstOrDefault().LstISBN : new List <string>();

            foreach (string isbn in lstIsbns)
            {
                string imagePath = ConfigurationManager.AppSettings["CommonRepository"] + ConfigurationManager.AppSettings["ImagesH135"] + @"\";
                if (imgpaths.Count < 5)
                {
                    if (!isbn.Contains(imagePath))
                    {
                        imagePath = imagePath + isbn + ".jpg";
                    }
                    else
                    {
                        imagePath = isbn;
                    }
                    if (System.IO.File.Exists(imagePath))
                    {
                        imgpaths.Add(lstEmailTemplatemodel.FirstOrDefault().DWName + "-" + isbn, imagePath);
                    }
                }
            }


            IDictionaryEnumerator myEnum = imgpaths.GetEnumerator();
            int i = 1;

            while (myEnum.MoveNext())
            {
                string imgFile = myEnum.Value.ToString();
                resource           = new LinkedResource(imgFile);
                resource.ContentId = "id" + i;
                lstImageResouces.Add(resource);

                Hashtable blockVars = new Hashtable();
                blockVars.Add("imgpath", "id" + i);
                blockVars.Add("DWName", myEnum.Key);

                string parsedHtml = parser.ParseBlock("image", blockVars);

                if (!string.IsNullOrEmpty(parsedHtml))
                {
                    imagesblock.Append(parsedHtml);
                }

                i = i + 1;
            }

            imagelinkResource.HtmlImageContent  = imagesblock.ToString();
            imagelinkResource.ListImageResource = lstImageResouces;
            return(imagelinkResource);
        }