using Discord;
using Managing.Domain.Statistics;
using Managing.Domain.Trades;

namespace Managing.Infrastructure.Messengers.Discord;

public static class DiscordHelpers
{
    public static Embed GetTradersEmbed(List<Trader> traders, string title)
    {
        var fields = new List<EmbedFieldBuilder>();

        traders = traders.OrderByDescending(t => t.Winrate).ToList();
        foreach (var trader in traders)
        {
            fields.Add(new EmbedFieldBuilder
            {
                Name = $"{GetExplorerUrl(trader.Address)}",
                Value =
                    $"Avg Win / Avg Loss / Winrate / ROI \n {trader.AverageWin:#.##}$ / {trader.AverageLoss:#.##}$ / {trader.Winrate}% / {Convert.ToDecimal(trader.Roi) * 100:#.##}%",
            });
        }

        var embed = new EmbedBuilder
        {
            Author = new EmbedAuthorBuilder() { Name = "GMX v2" },
            Title = $"{title} {DateTime.UtcNow:g}",
            Color = Color.Gold,
            Fields = fields,
        }.Build();

        return embed;
    }

    public static Embed GetFundingRateEmbed(FundingRate fundingRate, string title, FundingRate? oldRate = null)
    {
        var fields = new List<EmbedFieldBuilder>();

        var (ratePerYear, ratePerDay, ratePerMonth, ratePerHour) = GetRates(fundingRate);
        if (oldRate != null)
        {
            var (oldRatePerYear, oldRatePerDay, oldRatePerMonth, oldRatePerHour) = GetRates(oldRate);

            fields.Add(new EmbedFieldBuilder
            {
                Name = $"{fundingRate.Direction} {fundingRate.Ticker} - Previous Rate",
                Value =
                    $"Hour: {oldRatePerHour:#.##}% / Day: {oldRatePerDay:#.##}% / Month: {oldRatePerMonth:#.##}% / Year: {oldRatePerYear:#.##}%",
            });
        }

        fields.Add(new EmbedFieldBuilder
        {
            Name = $"{fundingRate.Direction} {fundingRate.Ticker} - Current rate",
            Value =
                $"Hour: {ratePerHour:#.##}% / Day: {ratePerDay:#.##}% / Month: {ratePerMonth:#.##}% / Year: {ratePerYear:#.##}%",
        });

        var embed = new EmbedBuilder
        {
            Author = new EmbedAuthorBuilder() { Name = "GMX" },
            Title = $"{title} {DateTime.UtcNow:g}",
            Color = Color.DarkGreen,
            Fields = fields,
        }.Build();

        return embed;
    }

    public static Embed GetEmbed(string address, string title, List<EmbedFieldBuilder> fields, Color color)
    {
        return new EmbedBuilder
        {
            Author = new EmbedAuthorBuilder() { Name = address },
            Title = title,
            Color = color,
            Fields = fields,
            Url = GetExplorerUrl(address)
        }.Build();
    }

    private static string GetExplorerUrl(string key)
    {
        return $"https://www.tradao.xyz/#/user/{key}/arb-gmx-v2/";
    }

    internal static Embed GetTradesEmbed(List<Trade> trades, string title)
    {
        var fields = new List<EmbedFieldBuilder>();

        foreach (var trade in trades)
        {
            fields.Add(new EmbedFieldBuilder
            {
                Name = $"{GetExplorerUrl(trade.ExchangeOrderId)}",
                Value =
                    $"Side / Ticker / Open / Qty / Leverage / LiqPrice \n {trade.Direction} / {trade.Ticker} / {trade.Price:#.##}$ / {trade.Quantity:#.##}$ / x{trade.Leverage:#.##} / {Convert.ToDecimal(trade.Message):#.##}$",
            });
        }

        fields.Add(new EmbedFieldBuilder
        {
            Name = "Summary",
            Value =
                $"Long / Short / \n {trades.Count(t => t.Direction == Common.Enums.TradeDirection.Long)} / {trades.Count(t => t.Direction == Common.Enums.TradeDirection.Short)}"
        });

        var embed = new EmbedBuilder
        {
            Author = new EmbedAuthorBuilder() { Name = "GMX" },
            Title = $"{title} {DateTime.UtcNow:g}",
            Color = Color.DarkBlue,
            Fields = fields,
        }.Build();

        return embed;
    }

    public static Embed GetFundingRatesEmbed(List<FundingRate> fundingRates, string leaderboardOpenPosition)
    {
        var fields = new List<EmbedFieldBuilder>();

        foreach (var fundingRate in fundingRates)
        {
            var (ratePerYear, ratePerDay, ratePerMonth, ratePerHour) = GetRates(fundingRate);
            fields.Add(new EmbedFieldBuilder
            {
                Name = $"{fundingRate.Ticker}",
                Value =
                    $"Hour: {ratePerHour:#.##}% / Day: {ratePerDay:#.##}% / Month: {ratePerMonth:#.##}% / Year: {ratePerYear:#.##}%",
            });
        }

        var embed = new EmbedBuilder
        {
            Author = new EmbedAuthorBuilder() { Name = "GMX" },
            Title = $"Best Funding Rate {DateTime.UtcNow:g}",
            Color = Color.DarkGreen,
            Fields = fields,
        }.Build();

        return embed;
    }

    private static (decimal ratePerYear, decimal ratePerDay, decimal ratePerMonth, decimal ratePerHour) GetRates(
        FundingRate fundingRate)
    {
        decimal ratePerHour = fundingRate.Rate; // Rate per hour
        decimal ratePerDay = ratePerHour * 24; // Rate per day
        decimal ratePerMonth = ratePerDay * 30; // Rate per month
        decimal ratePerYear = ratePerDay * 365; // Rate per year

        return (ratePerYear, ratePerDay, ratePerMonth, ratePerHour);
    }
}