using InABox.Core;
using System.Collections.Generic;

namespace InABox.DynamicGrid;

public interface IDynamicFormWindow
{
    DynamicFormDesignGrid Grid { get; }
}

public static class DynamicFormWindowExtensions
{
    public static void LoadLayout(this IDynamicFormWindow window, DigitalFormLayout layout, IList<DigitalFormVariable> variables)
    {
        window.Grid.Variables = variables;

        var f = new DFLayout();
        if (!string.IsNullOrWhiteSpace(layout.Layout))
        {
            f.LoadLayout(layout.Layout);
        }
        else
        {
            f = new DFLayout();
            f.RowHeights.Add("Auto");
            f.ColumnWidths.AddRange(new[] { "*", "Auto" });
        }

        f.LoadVariables(variables);

        window.Grid.Form = f;
    }

    /// <summary>
    /// Renders the form; after this is called, any changes to properties triggers a refresh.
    /// </summary>
    public static void Initialize(this IDynamicFormWindow window)
    {
        window.Grid.Initialize();
    }
}
