using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using JetBrains.Annotations;

namespace InABox.Mobile
{
    
    public abstract class AbstractNumericArrayConverter<TValue> : TypeConverter
    {

        protected abstract TValue[] Convert(IEnumerable<string> values);
        
        public override object ConvertFrom(
            ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string list)
            {
                try
                {
                    var result =  Convert(list.Split(','));
                    return result;
                }
                catch
                {
                    
                }
            }

            return new TValue[] { };
        }
    
        public override bool CanConvertFrom(
            ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;
    
            return base.CanConvertFrom(context, sourceType);
        }
    }
    
    public abstract class AbstractNumericCalculator<TValue,TFunction> : AbstractCalculator<TValue, TFunction, NumericCalculatorType> 
        where TFunction : AbstractCalculatorFunction<TValue,NumericCalculatorType>, new()


    {
        
        [CanBeNull] public abstract TValue[] Constants { get; set; }
        
        protected override TValue Convert(IEnumerable<TValue> value, object parameter = null)
        {
            var enumerable = value as TValue[] ?? value.ToArray();
            var values = Constants != null
                ? Constants.Concat(enumerable.ToArray())
                : enumerable;
            return base.Convert(values, parameter);
        }
    }
}