using FluentResults;
using Microsoft.JSInterop;
using NexusReader.Application.Abstractions.Services;

namespace NexusReader.Web.Client.Services;

public sealed class WebPlatformService : IPlatformService
{
    private readonly IJSRuntime _jsRuntime;

    public WebPlatformService(IJSRuntime jsRuntime)
    {
        _jsRuntime = jsRuntime;
    }

    public async Task<Result> VibrateSuccessAsync() => await VibrateAsync(100);
    public async Task<Result> VibrateErrorAsync() => await VibrateAsync(300);

    public async Task<Result> VibrateAsync(int milliseconds)
    {
        try 
        {
            await _jsRuntime.InvokeVoidAsync("navigator.vibrate", milliseconds);
            return Result.Ok();
        }
        catch (Exception ex)
        {
            return Result.Fail(ex.Message);
        }
    }

    public Result<DeviceContext> GetDeviceContext()
    {
        return Result.Ok(new DeviceContext(
            "Browser",
            "Web",
            DeviceType.Desktop, // Default for web, or could detect via JS
            DisplayOrientation.Unknown
        ));
    }
}
