using AyCode.Blazor.Components;
using AyCode.Interfaces.StorageHandlers;
using DevExpress.XtraPrinting.Native;
using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.Extensions.Localization;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TIAMSharedUI.Resources;
using TIAMWebApp.Shared.Application.Interfaces;
using TIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Models.ClientSide;
using TIAMWebApp.Shared.Application.Utility;

namespace TIAMSharedUI.Pages.Components
{
    public partial class SettingsBasic : ComponentBase
    {
        [Inject]
        public ISecureStorageHandler? secureStorageHandler { get; set; }

        [Inject]
        public IStringLocalizer<MyResources>? localizer { get; set; }
        [Inject]
        public NavigationManager navigationManager { get; set; }
        [Inject]
        public LogToBrowserConsole logToBrowserConsole { get; set; }
        [Inject]
        public IComponentUpdateService componentUpdateService { get; set; }

        public CultureInfo SelectedCulture { get; set; }
        IEnumerable<CultureInfo> Data { get; set; }

        [Parameter]
        public EventCallback<string> LanguageChanged { get; set; }      

        private Task<string>? GetCurrentSettings()
        {
            return secureStorageHandler?.GetFromSecureStorageAsync("Locale");
        }

        private void SaveSettings()
        {

            secureStorageHandler?.SaveToSecureStorageAsync(nameof(Setting.Locale), SelectedCulture.Name);
            componentUpdateService.CallRequestRefresh();
        }


        protected override async Task OnInitializedAsync()
        {
            Data = culture.ToList();
            string? current = await GetCurrentSettings();
            if (!string.IsNullOrEmpty(current))
            {
                logToBrowserConsole.LogToBC(current);
                
                //SelectedCulture = culture.FirstOrDefault(x => x.Name == Setting.Locale);
                foreach (var item in culture)
                {
                    logToBrowserConsole.LogToBC(item.Name);
                    if (item.Name == current)
                    {
                        SelectedCulture = item;
                        break;
                    }
                }

                logToBrowserConsole.LogToBC("Selected: " + SelectedCulture.Name);
            }
            else
            {
                SelectedCulture = culture[0];
            }
                        
            
        }

        CultureInfo[] culture = new[]
        {
        new CultureInfo("en-US"),
        new CultureInfo("hu-HU")
        };

        CultureInfo Culture
        {
            get => CultureInfo.CurrentCulture;
            set
            {
                if (CultureInfo.CurrentCulture != value)
                {
                    Thread.CurrentThread.CurrentCulture = value;
                    Thread.CurrentThread.CurrentUICulture = value;
                    CultureInfo.DefaultThreadCurrentCulture = value;
                    CultureInfo.DefaultThreadCurrentUICulture = value;
                    SelectedCulture = value;
                    SaveSettings();
                }               
            }
        }

    }
}
