using InABox.Core;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace InABox.DynamicGrid;

public class DFLabelControl : DynamicFormControl<DFLayoutLabel>
{
    protected override FrameworkElement Create()
    {
        var border = new Border
        {
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch
        };

        var style = Control.Style;

        var textBlock = new TextBlock
        {
            Text = Control.Caption,
            TextWrapping = TextWrapping.WrapWithOverflow,
            FontWeight = style.IsBold ? FontWeights.Bold : FontWeights.Normal,
            FontStyle = style.IsItalic ? FontStyles.Italic : FontStyles.Normal,
            VerticalAlignment = Control.Style.VerticalTextAlignment switch
            {
                DFLayoutAlignment.Start => VerticalAlignment.Top,
                DFLayoutAlignment.End => VerticalAlignment.Bottom,
                DFLayoutAlignment.Stretch => VerticalAlignment.Stretch,
                _ => VerticalAlignment.Center
            },
            HorizontalAlignment = HorizontalAlignment.Stretch,
            TextAlignment = Control.Style.HorizontalTextAlignment switch
            {
                DFLayoutAlignment.Middle => TextAlignment.Center,
                DFLayoutAlignment.End => TextAlignment.Right,
                DFLayoutAlignment.Stretch => TextAlignment.Justify,
                _ => TextAlignment.Left
            },
            Margin = new Thickness(5)
        };

        if(style.FontSize > 0)
        {
            textBlock.FontSize = style.FontSize;
        }
        if (style.BackgroundColour != System.Drawing.Color.Empty)
        {
            border.Background = new SolidColorBrush(ConvertColour(style.BackgroundColour));
        }
        if (style.ForegroundColour != System.Drawing.Color.Empty)
        {
            textBlock.Foreground = new SolidColorBrush(ConvertColour(style.ForegroundColour));
        }

        if (style.Underline == UnderlineType.Single)
        {
            textBlock.TextDecorations.Add(TextDecorations.Underline);
        }
        else if(style.Underline == UnderlineType.Double)
        {
            var underline1 = new TextDecoration
            {
                Pen = new Pen
                {
                    Brush = textBlock.Foreground,
                }
            };
            var underline2 = new TextDecoration
            {
                Pen = new Pen
                {
                    Brush = textBlock.Foreground,
                },
                PenOffset = 2
            };
            textBlock.TextDecorations.Add(underline1);
            textBlock.TextDecorations.Add(underline2);
        }

        border.Child = textBlock;

        return border;
    }

    private static Color ConvertColour(System.Drawing.Color colour)
    {
        return new Color { R = colour.R, G = colour.G, B = colour.B, A = colour.A };
    }
}
