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 DFHeaderControl : DynamicFormControl<DFLayoutHeader>
{
    public FormHeader Header = null!;

    protected override FrameworkElement Create()
    {
        var style = Control.Style;

        Header = new FormHeader
        {
            Collapsed = Control.Collapsed,
            HeaderText = Control.Header,
            FontWeight = style.IsBold ? FontWeights.Bold : FontWeights.Normal,
            FontStyle = style.IsItalic ? FontStyles.Italic : FontStyles.Normal,
        };
        Header.CollapsedChanged += (o, c) =>
        {
            FormDesignGrid.CollapseRows(Header, c);
        };
        if (FormDesignGrid.IsDesigning)
        {
            Header.IsEnabled = false;
        }

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

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

        return Header;
    }

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