using InABox.Core;
using InABox.DynamicGrid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InABox.Wpf.DynamicGrid.Editors.ColumnsEditor;

internal class ColumnsEditorItem : BaseObject
{
    public string ColumnName { get; set; } = "";
}

internal class ColumnsEditorGrid : DynamicItemsListGrid<ColumnsEditorItem>
{
    private readonly Type Type;

    private readonly string[] ColumnNames;

    public ColumnsEditorGrid(Type type, IColumns columns)
    {
        Type = type;

        var properties = DatabaseSchema.Properties(Type)
            .Where(x => x.IsSerializable)
            .Select(x => x.Name)
            .ToList();
        properties.Sort();
        ColumnNames = properties.ToArray();

        Items.AddRange(columns.ColumnNames().Select(x => new ColumnsEditorItem { ColumnName = x }));
    }

    public IColumns GetColumns()
    {
        return Columns.Create(Type, ColumnTypeFlags.None).Add(Items.Select(x => x.ColumnName));
    }

    protected override void DoReconfigure(DynamicGridOptions options)
    {
        base.DoReconfigure(options);
        options.Clear();

        options.RecordCount = true;
        options.MultiSelect = true;
        options.AddRows = true;
        options.DeleteRows = true;
        options.EditRows = true;
        options.ReorderRows = true;
    }

    protected override void MoveRows(CoreRow[] rows, int index, bool isCopy)
    {
        var targetCol = index < Items.Count ? Items[index] : null;
 
        var cols = LoadItems(rows);
        if (isCopy)
        {
            cols = cols.ToArray(x => new ColumnsEditorItem { ColumnName = x.ColumnName });
        }
        else
        {
            foreach(var col in cols)
            {
                Items.Remove(col);
            }
        }
        if(targetCol is not null)
        {
            var idx = Items.IndexOf(targetCol);
            Items.InsertRange(idx, cols);
        }
        else
        {
            Items.AddRange(cols);
        }
        Refresh(false, true);
        SelectedRows = cols.Select(x => Items.IndexOf(x)).Select(x => Data.Rows[x]).ToArray();
    }

    protected override IDynamicGridUIComponent<ColumnsEditorItem> CreateUIComponent()
    {
        return new DynamicGridGridUIComponent<ColumnsEditorItem>()
        {
            Parent = this,
            ShowHeader = false
        };
    }

    public override DynamicGridColumns GenerateColumns()
    {
        var columns = new DynamicGridColumns<ColumnsEditorItem>
        {
            x => x.ColumnName
        };
        return columns;
    }

    protected override void DoAdd(bool openEditorOnDirectEdit = false)
    {
        if(DynamicGridColumnNameSelectorGrid.SelectColumnName(Type, ColumnNames, out var column, showVisibilityButton: true))
        {
            CreateItems(() => [
                new ColumnsEditorItem { ColumnName = column }
                ]);
        }
    }

    protected override void DoEdit()
    {
        if(DynamicGridColumnNameSelectorGrid.SelectColumnName(Type, ColumnNames, out var column))
        {
            var items = LoadItems(SelectedRows);
            foreach(var item in items)
            {
                item.ColumnName = column;
            }
            SaveItems(items);
            UpdateRows(SelectedRows, items);
        }
    }
}
