using Avalonia;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Markup.Xaml;
using InABox.Avalonia.Converters;

namespace InABox.Avalonia.Components;

internal class TextEditVerticalAlignmentConverter : AbstractConverter<bool, VerticalAlignment>
{
    protected override VerticalAlignment Convert(bool value, object? parameter = null)
    {
        return value ? VerticalAlignment.Top : VerticalAlignment.Center;
    }
}

public partial class TextEditView : UserControl
{
    private TextEditViewModel Model => (DataContext as TextEditViewModel)!;

    public TextEditView()
    {
        InitializeComponent();
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        var baseSize = base.MeasureOverride(availableSize);
        if (Model.Multiline)
        {
            return new(
                Double.IsPositiveInfinity(availableSize.Width) ? baseSize.Width : availableSize.Width,
                Double.IsPositiveInfinity(availableSize.Height) ? baseSize.Height : availableSize.Height);
        }
        else
        {
            return baseSize.WithWidth(
                Double.IsPositiveInfinity(availableSize.Width) ? baseSize.Width : availableSize.Width);
        }
    }
}