using DevExpress.Web;
using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


public class HtmlConverter
{
    public string ConvertHtmlToPlainText(string htmlContent)
    {
        if (string.IsNullOrWhiteSpace(htmlContent))
        {
            return string.Empty;
        }

        // Remove script and style tags and their content
        htmlContent = Regex.Replace(htmlContent, @"<script[^>]*>[\s\S]*?</script>|<style[^>]*>[\s\S]*?</style>", string.Empty, RegexOptions.IgnoreCase);

        // Replace <br>, <div>, and <p> tags with new lines
        htmlContent = Regex.Replace(htmlContent, @"<br\s*/?>|</div>|</p>", "\n", RegexOptions.IgnoreCase);

        // Replace opening <div> and <p> tags with new lines to ensure content separation
        htmlContent = Regex.Replace(htmlContent, @"<div[^>]*>|<p[^>]*>", "\n", RegexOptions.IgnoreCase);

        // Replace header tags with new lines
        htmlContent = Regex.Replace(htmlContent, @"<h[1-6][^>]*>", "\n", RegexOptions.IgnoreCase);
        htmlContent = Regex.Replace(htmlContent, @"</h[1-6]>", "\n", RegexOptions.IgnoreCase);

        // Remove all other HTML tags
        htmlContent = Regex.Replace(htmlContent, @"<[^>]+>|&nbsp;", string.Empty).Trim();

        // Replace HTML entities
        htmlContent = Regex.Replace(htmlContent, @"&lt;", "<", RegexOptions.IgnoreCase);
        htmlContent = Regex.Replace(htmlContent, @"&gt;", ">", RegexOptions.IgnoreCase);
        htmlContent = Regex.Replace(htmlContent, @"&quot;", "\"", RegexOptions.IgnoreCase);
        htmlContent = Regex.Replace(htmlContent, @"&apos;", "'", RegexOptions.IgnoreCase);
        htmlContent = Regex.Replace(htmlContent, @"&amp;", "&", RegexOptions.IgnoreCase);

        // Normalize multiple consecutive newlines and spaces
        htmlContent = Regex.Replace(htmlContent, @"\n\s*\n", "\n\n"); // double newline for paragraphs
        htmlContent = Regex.Replace(htmlContent, @"[ \t\r\f\v]+", " "); // replace multiple spaces with a single space

        // Trim leading and trailing whitespace
        htmlContent = htmlContent.Trim();

        return htmlContent;
    }





}