using FoodsharingSiegen.Contracts.Helper;
using FoodsharingSiegen.Server.Service;
using Microsoft.AspNetCore.Components;
using System;
using System.Threading.Tasks;

namespace FoodsharingSiegen.Server.Pages
{
    public partial class Settings
    {
        [Inject]
        public IMailService MailService { get; set; } = null!;

        private string TestMailReceiver { get; set; } = string.Empty;

        protected override async Task InitializeDataAsync()
        {
            if (!CurrentUser.IsAdmin())
            {
                NavigationManager.NavigateTo("/");
                return;
            }
            TestMailReceiver = CurrentUser.Mail ?? string.Empty;
            await Task.CompletedTask;
        }

        private async Task SendTestMail()
        {
            if (string.IsNullOrWhiteSpace(TestMailReceiver))
            {
                await Notification.Error("Bitte eine Empfänger-Adresse angeben.");
                return;
            }

            try
            {
                await MailService.SendEmailAsync(TestMailReceiver, "Testmail", "Dies ist eine Testmail.");
                await Notification.Success("Testmail versendet.");
            }
            catch (Exception ex)
            {
                await Notification.Error($"Fehler beim Senden: {ex.Message}");
            }
        }
    }
}