using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
using InABox.Core;
using InABox.WPF;
using Syncfusion.Data;
using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.Grid.Cells;
using Syncfusion.UI.Xaml.TreeGrid;

namespace InABox.DynamicGrid;


public abstract class DynamicGridNumericColumn<TEditor, TGridColumn, TTreeColumn> : DynamicGridEditorColumn<TEditor, TGridColumn, TTreeColumn>, IDynamicGridNumericColumn 
    where TEditor : NumericEditor, new()
    where TGridColumn : GridColumn, new()
    where TTreeColumn : TreeGridColumn, new()
{
    
    protected int GetDigits(TEditor editor)
    {
        int digits = editor.Digits;
        if (!string.IsNullOrWhiteSpace(Definition?.Format))
        {
            try
            {
                var formatted = 0.ToString(Definition.Format);
                var separator = formatted.IndexOf(".", StringComparison.InvariantCulture);
                digits = separator == -1
                    ? 0
                    : formatted.Length - (separator + 1);
            }
            catch (Exception e)
            {
                Logger.Send(LogType.Error,"", $"{Definition.Format} is not a valid format specifier");
            }
        }
        return digits;
    }

    

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