using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace BerniceBobsHerHair
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        void OnRichTextBlockSizeChanged(object sender, SizeChangedEventArgs args)
        {
            RichTextBlock richTextBlock = sender as RichTextBlock;
            
            if (richTextBlock.ActualHeight == 0)
                return;
            
            // Usuń poprzednie obiekty RichTextBlockOverflow
            while (stackPanel.Children.Count > 1)
                stackPanel.Children.RemoveAt(1);

            if (!richTextBlock.HasOverflowContent)
                return;

            // Utwórz pierwszy RichTextBlockOverflow
            RichTextBlockOverflow richTextBlockOverflow = new RichTextBlockOverflow();
            richTextBlock.OverflowContentTarget = richTextBlockOverflow;
            stackPanel.Children.Add(richTextBlockOverflow);

            // Zmierz go
            richTextBlockOverflow.Measure(new Size(richTextBlockOverflow.Width, this.ActualHeight));

            // Jeżeli posiada nadmiarową treść, powtórz proces
            while (richTextBlockOverflow.HasOverflowContent)
            {
                RichTextBlockOverflow newRichTextBlockOverflow = new RichTextBlockOverflow();
                richTextBlockOverflow.OverflowContentTarget = newRichTextBlockOverflow;
                richTextBlockOverflow = newRichTextBlockOverflow;
                stackPanel.Children.Add(richTextBlockOverflow);
                richTextBlockOverflow.Measure(new Size(richTextBlockOverflow.Width, this.ActualHeight));
            }
        }
    }
}
