using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Bellwether.UIComponent
{
    /// <summary>
    /// Interaction logic for SpacedLabel.xaml
    /// </summary>
    public partial class SpacedLabel : UserControl
    {
        public SpacedLabel()
        {
            InitializeComponent();
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);

            if (e.Property == UserControl.FontSizeProperty ||
                e.Property == UserControl.FontFamilyProperty ||
                e.Property == UserControl.FontWeightProperty)
            {
                uxControl.ItemTemplate = new DataTemplate(typeof(Label)) { VisualTree = GetVisualTree() };
            }
        }

        private string text = string.Empty;
        public string Text
        {
            get => text;
            set
            {
                if(text == value)
                {
                    return;
                }
                text = value;
                uxControl.ItemsSource = value;
            }
        }

        private double spaceing;
        public double Spaceing
        {
            get => spaceing;
            set
            {
                if (spaceing == value)
                {
                    return;
                }
                spaceing = value;
                uxControl.ItemTemplate = new DataTemplate(typeof(Label)) { VisualTree = GetVisualTree() };
            }
        }

        private FrameworkElementFactory GetVisualTree()
        {
            FrameworkElementFactory factory = new FrameworkElementFactory(typeof(Label));
            factory.SetValue(Label.FontSizeProperty, FontSize);
            factory.SetValue(Label.FontFamilyProperty, FontFamily);
            factory.SetValue(Label.FontWeightProperty, FontWeight);
            factory.SetValue(Label.ForegroundProperty, Brushes.White);
            factory.SetValue(Label.MarginProperty, new Thickness(0,0, spaceing, 0));
            factory.SetBinding(Label.ContentProperty,new Binding());
            return factory;
        }
    }
}
