using System;
using System.Collections.Generic;
using System.Windows.Data;
using InABox.Core;
using Syncfusion.UI.Xaml.Grid;

namespace InABox.DynamicGrid;

public abstract class DynamicGridComboBoxColumn<TEntity, TEditor> : DynamicGridEditorColumn<TEntity, TEditor, GridComboBoxColumn>
    where TEntity : BaseObject
    where TEditor : class, ILookupEditor 

{
    protected override void Configure(GridComboBoxColumn column, TEditor editor)
    {
        var data = editor.Values(Definition.ColumnName, null);

        var items = new List<Tuple<object?, string>>
        {
            new(null, "")
        };
        foreach (var row in data.Rows)
            items.Add(new(row[data.Columns[0].ColumnName], (row["Display"] ?? "").ToString() ?? ""));

        column.SelectedValuePath = "Item1";
        column.DisplayMemberPath = "Item2";
        column.ItemsSource = items;
        column.UpdateTrigger = UpdateSourceTrigger.PropertyChanged;
    }

    protected DynamicGridComboBoxColumn(DynamicGridColumn definition) : base(definition)
    {
    }
}