using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static C4IT.Logging.cLogManager;
using static System.Net.Mime.MediaTypeNames;

namespace FasdDesktopUi.Basics.Helper
{
    public static class cRichTextBoxHelper
    {
        #region Unicode

        public static void TraverseBlockAsUnicode(BlockCollection blocks, StringBuilder stringBuilder, bool isBlockFromList = false)
        {
            try
            {
                foreach (Block block in blocks)
                {
                    if (block is Paragraph paragraph)
                    {
                        TraverseParagraphAsUnicode(paragraph, stringBuilder);
                        stringBuilder.AppendLine();
                    }
                    else if (block is List list)
                    {
                        TraverseListAsUnicode(list, stringBuilder);
                        stringBuilder.AppendLine();
                    }
                    else if (block is BlockUIContainer container)
                    {
                        if (container.Child is System.Windows.Controls.Image image)
                        {
                            TraverseImageAsUnicode(image, stringBuilder);
                            stringBuilder.AppendLine();
                        }
                    }
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private static void TraverseParagraphAsUnicode(Paragraph paragraph, StringBuilder stringBuilder)
        {

            try
            {
                foreach (var inline in paragraph.Inlines)
                {
                    if (inline is Run run)
                    {
                        stringBuilder.Append(run.Text);
                    }
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private static void TraverseListAsUnicode(List list, StringBuilder stringBuilder)
        {

            try
            {

                if (list.MarkerStyle == TextMarkerStyle.Decimal)
                {

                    for (int i = 0; i < list.ListItems.Count; i++)
                    {

                        stringBuilder.Append(i + 1 + ". ");
                        TraverseBlockAsUnicode(list.ListItems.ElementAt(i).Blocks, stringBuilder, true);

                    }
                }
                else
                {
                    foreach (var item in list.ListItems)
                    {

                        stringBuilder.Append("- ");
                        TraverseBlockAsUnicode(item.Blocks, stringBuilder, true);

                    }
                }

            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private static void TraverseImageAsUnicode(System.Windows.Controls.Image image, StringBuilder stringBuilder)
        {


            try
            {
                stringBuilder.Append("[Image]");
            }
            catch (Exception E)
            {
                LogException(E);
            }


        }

        #endregion

        #region Html

        public static void TraverseBlockAsHtml(BlockCollection blocks, StringBuilder stringBuilder, bool isBlockFromList = false)
        {
            try
            {
                foreach (var block in blocks)
                {
                    if (block is Paragraph paragraph)
                    {
                        if (!isBlockFromList)
                            stringBuilder.Append("<p>");

                        TraverseParagraphAsHtml(paragraph, stringBuilder);

                        if (!isBlockFromList)
                            stringBuilder.Append("</p>");
                    }
                    else if (block is List list)
                    {
                        stringBuilder.Append(list.MarkerStyle == TextMarkerStyle.Decimal ? "<ol>" : "<ul>");
                        TraverseListAsHtml(list, stringBuilder);
                        stringBuilder.Append(list.MarkerStyle == TextMarkerStyle.Decimal ? "</ol>" : "</ul>");
                    }
                    else if (block is BlockUIContainer container)
                    {
                        if (container.Child is System.Windows.Controls.Image image)
                        {

                            TraverseImageAsHtml(image, stringBuilder);

                        }

                    }
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private static void TraverseParagraphAsHtml(Paragraph paragraph, StringBuilder stringBuilder)
        {
            try
            {
                foreach (var inline in paragraph.Inlines)
                {
                    TraverseInlineAsHtml(inline, stringBuilder);
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private static void TraverseInlineAsHtml(Inline inline, StringBuilder stringBuilder)
        {
            try
            {
                if (inline is Run run)
                    TraverseRunAsHtml(run, stringBuilder);
                else if (inline is Span span)
                {
                    foreach (var spanInline in span.Inlines)
                    {
                        TraverseInlineAsHtml(spanInline, stringBuilder);
                    }
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private static void TraverseListAsHtml(List list, StringBuilder stringBuilder)
        {
            try
            {
                foreach (var listItem in list.ListItems)
                {
                    stringBuilder.AppendLine("<li>");
                    TraverseBlockAsHtml(listItem.Blocks, stringBuilder, true);
                    stringBuilder.Append("</li>");
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private static void TraverseImageAsHtml(System.Windows.Controls.Image image, StringBuilder stringBuilder)
        {

            try
            {

                byte[] arr;
                using (MemoryStream ms = new MemoryStream())
                {
                    var bmp = image.Source as BitmapImage;
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bmp));
                    encoder.Save(ms);
                    arr = ms.ToArray();
                }

                stringBuilder.AppendLine("<img src=\"data:image/png;base64,");
                stringBuilder.Append(Convert.ToBase64String(arr));
                stringBuilder.Append("\"/>");
            }
            catch (Exception E)
            {
                LogException(E);
            }

        }

        public static void TraverseRunAsHtml(Run run, StringBuilder stringBuilder)
        {
            try
            {
                string styleString = string.Empty;
                string runString = run.Text;

                if (run.FontWeight == FontWeights.Bold)
                {
                    runString = runString.Insert(0, "<strong>");
                    runString += "</strong>";
                }

                if (run.FontStyle == FontStyles.Italic)
                {
                    runString = runString.Insert(0, "<em>");
                    runString += "</em>";
                }

                if (run.TextDecorations != null && run.TextDecorations.Count > 0)
                {
                    if (run.TextDecorations.FirstOrDefault()?.Location == TextDecorationLocation.Underline)
                    {
                        runString = runString.Insert(0, "<u>");
                        runString += "</u>";
                    }
                }

                if (run.FontSize != 12)
                    styleString += $"font-size: {run.FontSize}px; ";

                // default font size
                //if (run.FontFamily != null)
                //    styleString += $"font-family: {run.FontFamily}; ";

                //default color instead null:
                //if (run.Foreground != null) 
                //    styleString += $"color: {run.Foreground}; ";

                if (!string.IsNullOrWhiteSpace(styleString))
                {
                    styleString = styleString.Insert(0, "style=\"");
                    styleString += "\"";
                }

                stringBuilder.AppendLine("<span " + styleString + ">");
                stringBuilder.Append(runString);
                stringBuilder.Append("</span>");
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        #endregion

    }
}
