// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using SelmoStudio.Hmi.Plugin.Services;
using SelmoStudio.Hmi.Server.Languages.Models;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.WellKnownTypes;
using SelmoStudio.Hmi.Server.Service;

namespace SelmoStudio.Hmi.Server.Languages.Components
{
    public partial class LanguageComponent
    {
        private List<HmiLanguage> _languages;
        private HmiLanguage _selectedLanguage;

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

        [Inject]
        public IHmiLanguageService _hmiLanguageService { get; set; }

        [Inject]
        private ICachingService _localStorageService { get; set; }

        private async Task LanguageDropDownCallBack(object value)
        {
            if (value is HmiLanguage hmiLanguage)
            {
                _selectedLanguage = hmiLanguage;
                await InvokeAsync(async () =>
                {
                    _hmiLanguageService.Set(hmiLanguage);

                    await _localStorageService.SetAsync(WellKnownLanguages.LanguageCodeIdentifier, hmiLanguage.Lcid);
                });
            }
        }

        protected override void OnInitialized()
        {
            _languages = _hmiLanguageService.SupportedLanguages.ToList();
            _selectedLanguage = _hmiLanguageService.CurrentLanguage;

            base.OnInitialized();
        }
    }
}