using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Threading;
using detect.gui.VWMS;
using detect.gui.VWS;
using Splat;
using WebViewControl;

namespace detect.gui.Views;

public class WebViewService(MainWindow? w)
{
    public MainWindow? Self { get; set; } = w;

    // [Obsolete("过时的")]
    // public async Task<string?> ImportExcel()
    // {
    //     var dialog = new OpenFileDialog
    //     {
    //         Title = "打开文件",
    //         AllowMultiple = false,
    //         Filters =
    //         [
    //             new FileDialogFilter { Name = "Excel Files", Extensions = { "*.xls; *.xlsx" } }
    //         ]
    //     };
    //     var result = await dialog.ShowAsync(Self!);
    //     if (result is not { Length: 1 }) return null;
    //     try
    //     {
    //         return ConvertExcelToJson(result[0]);
    //     }
    //     catch
    //     {
    //         return null;
    //     }
    // }

    // public static string ConvertExcelToJson(string excelFilePath)
    // {
    //     ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
    //     var fileInfo = new FileInfo(excelFilePath);
    //     using var package = new ExcelPackage(fileInfo);
    //     var worksheet = package.Workbook.Worksheets[0];
    //     var rowCount = worksheet.Dimension.Rows;
    //     var colCount = worksheet.Dimension.Columns;
    //
    //     var data = new List<Dictionary<string, object>>();
    //     var headers = Enumerable.Range(1, colCount)
    //         .Select(col => worksheet.Cells[1, col].Text)
    //         .ToList();
    //     for (var row = 2; row <= rowCount; row++)
    //     {
    //         var rowData = new Dictionary<string, object>();
    //         for (var col = 1; col <= colCount; col++)
    //         {
    //             rowData[headers[col - 1]] = worksheet.Cells[row, col].Text;
    //         }
    //
    //         data.Add(rowData);
    //     }
    //     return JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) });
    // }

    public Task SetIsLoading(bool isLoading)
    {
        Locator.Current.GetService<MainWindowModel>()!.IsLoading = isLoading;
        Dispatcher.UIThread.InvokeAsync(() =>
        {
            var webView = Locator.Current.GetService<MainWindow>()!.Get<WebView>("WebView");
            if (!webView.IsVisible) webView.IsVisible = Locator.Current.GetService<MainWindowModel>()!.CurrentUser != null;
        });
        return Task.FromResult(default(object));
    }
    
    public Task SetMessage(string messageText, string messageType)
    {
        Locator.Current.GetService<MainWindowModel>()!.Message = messageType switch
        {
            "success" => new MessageItem(messageText, NotificationType.Success),
            "information" => new MessageItem(messageText, NotificationType.Information),
            "error" => new MessageItem(messageText, NotificationType.Error),
            _ => new MessageItem(messageText, NotificationType.Warning)
        };
        return Task.CompletedTask;
    }
}