using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System;
using System.Globalization;
using System.Threading.Tasks;

namespace FastReport.Web.Blazor.Components.Internal.ExportSettings
{
    public partial class Slider : Microsoft.AspNetCore.Components.ComponentBase
    {
        [Parameter]
        public double Step { get; set; } = 1;

        [Parameter]
        public double Value { get; set; }

        [Parameter]
        public EventCallback<double> ValueChanged { get; set; }

        [Parameter]
        public double Min { get; set; }

        [Parameter]
        public double Max { get; set; }

        [Parameter]
        public string Label { get; set; }

        string internalValue
        {
            get => Value.ToString(CultureInfo.InvariantCulture);
        }

        private async Task OnChange(ChangeEventArgs changeEvent)
        {
            var value = changeEvent.Value.ToString();
            if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out double newVal))
            {
                Value = newVal;
                await ValueChanged.InvokeAsync(Value);
            }
        }

        string internalMin
        {
            get => Min.ToString(CultureInfo.InvariantCulture);
            set
            {
                if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out double newVal))
                {
                    Min = newVal;
                }
            }
        }

        string internalMax
        {
            get => Max.ToString(CultureInfo.InvariantCulture);
            set
            {
                if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out double newVal))
                {
                    Max = newVal;
                }
            }
        }

        string internalStep
        {
            get => Step.ToString(CultureInfo.InvariantCulture);
            set
            {
                if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out double newVal))
                {
                    Step = newVal;
                }
            }
        }
    }
}
