using Managing.Application.Abstractions.Services;
using Managing.Common;
using Managing.Domain.Statistics;
using Managing.Domain.Trades;
using Managing.Domain.Users;

namespace Managing.Application.Shared;

public class MessengerService : IMessengerService
{
    private readonly IDiscordService _discordService;
    private readonly IWebhookService _webhookService;
    private readonly IUserService _userService;

    public MessengerService(IDiscordService discordService, IWebhookService webhookService, IUserService userService)
    {
        _discordService = discordService;
        _webhookService = webhookService;
        _userService = userService;
    }

    public async Task SendClosedPosition(string address, Trade oldTrade)
    {
        await _discordService.SendClosedPosition(address, oldTrade);
    }

    public async Task SendClosingPosition(Position position)
    {
        await _discordService.SendClosingPosition(position);
    }

    public async Task SendIncreasePosition(string address, Trade trade, string copyAccountName, Trade? oldTrade = null)
    {
        await _discordService.SendIncreasePosition(address, trade, copyAccountName, oldTrade);
    }

    public async Task SendDecreasePosition(string address, Trade newTrade, decimal decreaseAmount)
    {
        await _discordService.SendDecreasePosition(address, newTrade, decreaseAmount);
    }

    public async Task SendMessage(string message)
    {
        await _discordService.SendMessage(message);
    }

    public async Task SendPosition(Position position)
    {
        await _discordService.SendPosition(position);
    }

    public async Task SendSignal(string message, Enums.TradingExchanges exchange, Enums.Ticker ticker,
        Enums.TradeDirection direction, Enums.Timeframe timeframe)
    {
        await _discordService.SendSignal(message, exchange, ticker, direction, timeframe);
    }

    public async Task SendTradeMessage(string message, bool isBadBehavior = false, User user = null)
    {
        // Always send to Discord
        try
        {
            await _discordService.SendTradeMessage(message, isBadBehavior);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        // If user is provided, also send to webhook
        if (user != null)
        {
            user = _userService.GetUser(user.Name);
            await _webhookService.SendTradeNotification(user, message, isBadBehavior);
        }
    }

    public async Task SendBestTraders(List<Trader> traders)
    {
        await _discordService.SendBestTraders(traders);
    }

    public async Task SendBadTraders(List<Trader> traders)
    {
        await _discordService.SendBadTraders(traders);
    }

    public async Task SendDowngradedFundingRate(FundingRate oldRate)
    {
        await _discordService.SendDowngradedFundingRate(oldRate);
    }

    public async Task SendNewTopFundingRate(FundingRate newRate)
    {
        await _discordService.SendNewTopFundingRate(newRate);
    }

    public async Task SendFundingRateUpdate(FundingRate oldRate, FundingRate newRate)
    {
        await _discordService.SendFundingRateUpdate(oldRate, newRate);
    }
}