using Discord;
using Discord.WebSocket;
using Flux_System_Assistant.Utility;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ILogger = Serilog.ILogger;

namespace Flux_System_Assistant.Services;

public class DiscordStartupService : IHostedService
{
    private readonly DiscordSocketClient _discord;
    private readonly IConfiguration _config;

    public DiscordStartupService(DiscordSocketClient discord, IConfiguration config, ILogger logger)
    {
        _discord = discord;
        _config = config;

        _discord.Log += msg => LoggerService.OnLogAsync(logger, msg);
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        await _discord.LoginAsync(TokenType.Bot, _config["token"]);
        await _discord.StartAsync();
    }

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        await _discord.StopAsync();
    }
}