using System;
using System.Windows.Controls;
using System.Windows.Media;

namespace InABox.DynamicGrid;

public class GuidValueNode : ValueNode
{

    private TextBox Editor;

    public override object? Value
    {
        get {
            if (Guid.TryParse(Editor.Text, out var guid))
                return guid;
            return Guid.Empty;
        }
        set
        {
            if (value is Guid guid || (value is string str && Guid.TryParse(str, out guid)))
            {
                if(guid == Guid.Empty)
                {
                    Editor.Text = "";
                }
                else
                {
                    Editor.Text = guid.ToString();
                }
            }
            else
            {
                Editor.Text = "";
            }
        }
    }
    
        
    public override bool IsEnabled
    {
        get => Editor.IsEnabled;
        set
        {
            Editor.IsEnabled = value;
            Editor.IsReadOnly = !value;
        }
    }

    public GuidValueNode()
    {
        Editor = new TextBox
        {
            VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
            VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
            Background = new SolidColorBrush(Colors.LightYellow)
        };
        Content = Editor;
    }
}