using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using InABox.Core;

namespace InABox.DynamicGrid;

internal class DynamicJsonGrid<T> : DynamicGrid<T> where T : BaseObject, new()
{
    private int rowindex = -1;

    public override void DeleteItems(params CoreRow[] rows)
    {
        var indexes = rows.Select(x => x.Index).OrderBy(x => x).ToArray();
        foreach (var index in indexes)
            Data.Rows.RemoveAt(index);
    }

    public override T LoadItem(CoreRow row)
    {
        rowindex = row.Index;
        return row.ToObject<T>();
    }

    protected override void Reload(
        Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort, 
        CancellationToken token, Action<CoreTable?, Exception?> action)
    {
    }

    public override void SaveItem(T item)
    {
        CoreRow row;
        if (rowindex == -1)
        {
            row = Data.NewRow();
            Data.Rows.Add(row);
        }
        else
        {
            row = Data.Rows[rowindex];
        }

        Data.FillRow(row, item);
    }
}

internal class JsonEditorControl : DynamicEditorControl<string, JsonEditor>
{
    
                    
    static JsonEditorControl()
    {
        //DynamicEditorControlFactory.Register<JsonEditorControl, JsonEditor>();
    }
    
    private Button _button;

    private string _value = "";

    public JsonEditorControl()
    {
        Width = 150;
    }

    public override int DesiredHeight()
    {
        return 25;
    }

    public override int DesiredWidth()
    {
        return 150;
    }

    public override void SetColor(Color color)
    {
        _button.Background = new SolidColorBrush(color);
    }

    public override void SetFocus()
    {
        // Not Sure what to do here?
    }
    public override void Configure()
    {
    }

    protected override FrameworkElement CreateEditor()
    {
        _button = new Button();
        _button.Content = "Edit";
        _button.Click += _button_Click;
        return _button;
    }

    private void _button_Click(object sender, RoutedEventArgs e)
    {
        var type = (EditorDefinition as JsonEditor).Type;
        var listtype = typeof(List<>).MakeGenericType(type);
        var list = Activator.CreateInstance(listtype) as IList;
        if (!string.IsNullOrWhiteSpace(_value))
            Serialization.DeserializeInto(_value, list);

        var array = new object[list.Count];
        list.CopyTo(array, 0);

        var data = new CoreTable();
        data.LoadColumns(type);
        data.LoadRows(array);

        /*var gridtype = typeof(DynamicJsonGrid<>).MakeGenericType(type);
        var grid = Activator.CreateInstance(gridtype) as IDynamicGrid;
        grid.Data = data;
        if (grid.DirectEdit(data))
        {
            var saved = data.Rows.Select(x => x.ToObject(type)).ToArray();
            _value = Serialization.Serialize(saved, true);
            CheckChanged();
        }*/
    }

    protected override string RetrieveValue()
    {
        return _value;
    }

    protected override void UpdateValue(string value)
    {
        _value = "";
    }
}