using Microsoft.JSInterop;
using System.Reflection.Metadata.Ecma335;

namespace AuthorBuddy.Web.Services;

public class ThemeService : IThemeService
{
    private readonly IJSRuntime _js;
    private string _theme = "";

    public ThemeService(IJSRuntime js)
    {
        _js = js;
    }

    public string Theme { get =>  _theme; }

    public async Task ApplyTheme(string theme)
    {
        _theme = theme;
        await _js.InvokeVoidAsync("eval",
            $"document.getElementById('theme-css')?.remove(); " +
            $"var link = document.createElement('link'); " +
            $"link.id = 'theme-css'; " +
            $"link.rel = 'stylesheet'; " +
            $"link.href = 'css/themes/{theme}.css'; " +
            $"document.head.appendChild(link);");
    }
}
