using InABox.Core;
using InABox.Wpf;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace InABox.DynamicGrid;

/// <summary>
///     Interaction logic for DynamicGridColumnsEditor.xaml
/// </summary>
public partial class DynamicGridColumnsEditor : ThemableWindow
{
    private readonly DynamicColumnGrid ColumnGrid;

    public DynamicGridColumnsEditor(IDynamicGridColumnSchema schema, Type? type)
    {
        InitializeComponent();

        if(type is null)
        {
            Title = "Select Columns";
        }
        else
        {
            Title = $"Select Columns for {CoreUtils.Neatify(type.Name)}";
        }

        ColumnGrid = new DynamicColumnGrid(schema);
        ColumnGrid.SetValue(Grid.ColumnSpanProperty, 3);
        ColumnGrid.Margin = new Thickness(5F, 5F, 5F, 5F);
        grid.Children.Add(ColumnGrid);

        Columns = ColumnGrid.Columns;

        //ColumnGrid.OnEditItem += Columns_OnEditItem;
    }

    public DynamicGridColumns Columns { get; }

    public bool DirectEdit
    {
        get => ColumnGrid.DirectEdit;
        set => ColumnGrid.DirectEdit = value;
    }

    //private bool Columns_OnEditItem(object sender, object item)
    //{
    //	DynamicEditor editor = new DynamicEditor();
    //	editor.OnDefineGridColumns += Editor_OnDefineGridColumns;
    //	editor.Item = item;
    //	if (editor.ShowDialog() == true)
    //		return true;
    //	return false;
    //}

    //private DynamicGridColumns Editor_OnDefineGridColumns(object sender, DynamicGridColumns master)
    //{
    //	return ColumnGrid.DefineColumns();
    //}

    private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = true;
        Close();
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = false;
        Close();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        ColumnGrid.Refresh(true, true);
    }
}