@using AliasVault.ImportExport.Models
@using AliasVault.ImportExport.Importers
@using Microsoft.Extensions.Localization
@inject NavigationManager NavigationManager
@inject GlobalNotificationService GlobalNotificationService
@inject IStringLocalizerFactory LocalizerFactory
@inject ILogger<ImportServiceGenericCsv> Logger
@inject JsInteropService JsInteropService

<ImportServiceCard
    ServiceName="Generic CSV"
    Description="@Localizer["GenericCsvDescription"]"
    LogoUrl="img/importers/generic-csv.svg"
    AcceptedFileExtensions="AcceptedExtensions"
    ProcessFileCallback="ProcessFile">
    <p class="text-gray-700 dark:text-gray-300 mb-4">@Localizer["GenericCsvInstructionsPart1"]</p>
    <ol class="text-sm text-gray-700 dark:text-gray-300 space-y-2 mb-4">
        <li class="flex items-start space-x-2">
            <span class="flex-shrink-0 w-5 h-5 bg-amber-400 text-white text-xs rounded-full flex items-center justify-center">1</span>
            <span>@Localizer["GenericCsvStep1"] <button @onclick="DownloadTemplate" class="text-amber-600 dark:text-amber-400 hover:text-amber-800 dark:hover:text-amber-200 underline">template</button></span>
        </li>
        <li class="flex items-start space-x-2">
            <span class="flex-shrink-0 w-5 h-5 bg-amber-400 text-white text-xs rounded-full flex items-center justify-center">2</span>
            <span>@Localizer["GenericCsvStep2"]</span>
        </li>
        <li class="flex items-start space-x-2">
            <span class="flex-shrink-0 w-5 h-5 bg-amber-400 text-white text-xs rounded-full flex items-center justify-center">3</span>
            <span>@Localizer["GenericCsvStep3"]</span>
        </li>
        <li class="flex items-start space-x-2">
            <span class="flex-shrink-0 w-5 h-5 bg-amber-400 text-white text-xs rounded-full flex items-center justify-center">4</span>
            <span>@Localizer["GenericCsvStep4"]</span>
        </li>
    </ol>
</ImportServiceCard>

@code {
    private IStringLocalizer Localizer => LocalizerFactory.Create("Components.Main.Settings.ImportExport.ImportServices", "AliasVault.Client");
    private static readonly string[] AcceptedExtensions = new[] { ".csv" };

    private static async Task<ImportFileResult> ProcessFile(string filename, byte[] fileBytes)
    {
        var fileContents = System.Text.Encoding.UTF8.GetString(fileBytes);
        return await GenericCsvImporter.ImportFromCsvAsync(fileContents);
    }

    private async Task DownloadTemplate()
    {
        try
        {
            var csvBytes = GenericCsvImporter.GetCsvTemplateBytes();
            var fileName = $"aliasvault-import-template.csv";

            // Create a memory stream from the byte array
            using (MemoryStream memoryStream = new MemoryStream(csvBytes))
            {
                // Invoke JavaScript to initiate the download
                await JsInteropService.DownloadFileFromStream(fileName, memoryStream.ToArray());
            }
        }
        catch (Exception ex)
        {
            Logger.LogError(ex, "Error downloading CSV template");
            GlobalNotificationService.AddErrorMessage(Localizer["GenericCsvTemplateDownloadError"], true);
        }
    }
}