// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using System.Globalization;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Helpers;

namespace SelmoStudio.Hmi.Server.Shared.Components.BaseComponents
{
    public partial class TimePickerComponent
    {
        private DateTime? _selectedDateTime;

        [Parameter]
        public TimeSpan? SelectedTime { get; set; }

        [Parameter]
        public EventCallback<TimeSpan?> SelectedTimeChanged { get; set; }

        protected override void OnInitialized()
        {
            if (SelectedTime.HasValue)
            {
                _selectedDateTime = DateTime.Today.Add(SelectedTime.Value);
            }
        }

        private async Task OnTimeChanged(DateTime? value)
        {
            if (value.HasValue)
            {
                SelectedTime = value.Value.TimeOfDay;
                await SelectedTimeChanged.InvokeAsync(SelectedTime);
            }
            else
            {
                SelectedTime = null;
                await SelectedTimeChanged.InvokeAsync(null);
            }
        }

        private string CurrentUICultureTimeFormat => DateTimeFormatter.CurrentUICultureTimeFormat;

        private string GetHourFormat()
        {
            var culture = CultureInfo.DefaultThreadCurrentUICulture ?? CultureInfo.CurrentUICulture;
            string pattern = culture.DateTimeFormat.ShortTimePattern;
            return pattern.Contains("H") ? "24" : "12";
        }
    }
}