using System.Net.Http.Json;
using Discord;
using EllieBot.Marmalade;

namespace Social;

public sealed class Social : Canary
{
    public new string Name = "Social";
    private static readonly HttpClient Client = new HttpClient();

    [svc(Lifetime.Singleton)]
    public sealed class SocialService
    {
        public async Task<string> GetWaifuPicsImage(ImageType imageType)
        {
            var img = await Client
                .GetFromJsonAsync<WaifuData>($"https://waifu.pics/api/sfw/{imageType}")
                .ConfigureAwait(false);

            return img.URL;
        }

        public async Task SendWaifuPicsEmbedAsync(AnyContext ctx, ImageType imageType, string text = null)
        {
            var emb = new EmbedBuilder();

            await ctx.Channel.TriggerTypingAsync().ConfigureAwait(false);

            var img = await GetWaifuPicsImage(imageType);

            if (!string.IsNullOrWhiteSpace(img))
            {
                emb.WithImageUrl(img);
            }

            if (!string.IsNullOrWhiteSpace(text))
            {
                switch (imageType)
                {
                    case ImageType.hug:
                        emb.WithDescription($"{ctx.User.Mention} hugged {text}");
                        break;
                    case ImageType.pat:
                        emb.WithDescription($"{ctx.User.Mention} petted {text}");
                        break;
                    case ImageType.kiss:
                        emb.WithDescription($"{ctx.User.Mention} kissed {text}");
                        break;
                    case ImageType.wave:
                        emb.WithDescription($"{ctx.User.Mention} waved to {text}");
                        break;
                    case ImageType.cuddle:
                        emb.WithDescription($"{ctx.User.Mention} cuddled {text}");
                        break;
                    case ImageType.slap:
                        emb.WithDescription($"{ctx.User.Mention} slapped {text}, they probably deserved it.");
                        break;
                    case ImageType.poke:
                        emb.WithDescription($"{ctx.User.Mention} booped {text}");
                        break;
                    case ImageType.yeet:
                        emb.WithDescription($"{ctx.User.Mention} threw {text}, watch them fly.");
                        break;
                    case ImageType.wink:
                        emb.WithDescription($"{ctx.User.Mention} winked at {text}");
                        break;
                    case ImageType.cringe:
                        emb.WithDescription($"{ctx.User.Mention} cringed at {text} they probably posted cringe.");
                        break;
                    case ImageType.bully:
                        emb.WithDescription($"{ctx.User.Mention} bullied {text}, you are a bad person.");
                        break;
                }
            }
            await ctx.Channel.EmbedAsync(emb);
        }

        public async Task SendWaifuPicsEmbedAsync(AnyContext ctx, ImageType imageType)
        {
            var emb = new EmbedBuilder();

            await ctx.Channel.TriggerTypingAsync().ConfigureAwait(false);

            var img = await GetWaifuPicsImage(imageType);

            if (!string.IsNullOrWhiteSpace(img))
            {
                emb.WithImageUrl(img);
            }

            await ctx.Channel.EmbedAsync(emb);
        }
    }

    public enum ImageType
    {
        hug,
        pat,
        kiss,
        wave,
        cuddle,
        slap,
        poke,
        yeet,
        wink,
        cringe,
        bully,
        waifu,
        neko,
        shinobu,
        megumin
    }

    public sealed class SocialInteractions(SocialService service) : Canary
    {
        [cmd]
        public async Task Hug(AnyContext ctx, [leftover] string text = null)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.hug, text);
        }

        [cmd]
        public async Task Pat(AnyContext ctx, [leftover] string text = null)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.pat, text);
        }

        [cmd]
        public async Task Kiss(AnyContext ctx, [leftover] string text = null)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.kiss, text);
        }

        [cmd]
        public async Task Wave(AnyContext ctx, [leftover] string text = null)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.wave, text);
        }

        [cmd]
        public async Task Cuddle(AnyContext ctx, [leftover] string text = null)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.cuddle, text);
        }

        [cmd]
        public async Task Slap(AnyContext ctx, [leftover] string text = null)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.slap, text);
        }

        [cmd]
        public async Task Boop(AnyContext ctx, [leftover] string text = null)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.poke, text);
        }

        [cmd]
        public async Task Yeet(AnyContext ctx, [leftover] string text = null)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.yeet, text);
        }

        [cmd]
        public async Task Wink(AnyContext ctx, [leftover] string text = null)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.wink, text);
        }

        [cmd]
        public async Task Cringe(AnyContext ctx, [leftover] string text = null)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.cringe, text);
        }

        [cmd]
        public async Task Bully(AnyContext ctx, [leftover] string text = null)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.bully, text);
        }
    }

    public sealed class Images(SocialService service) : Canary
    {
        [cmd]
        public async Task Waifu(AnyContext ctx)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.waifu);
        }

        [cmd]
        public async Task Neko(AnyContext ctx)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.neko);
        }

        [cmd]
        public async Task Shinobu(AnyContext ctx)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.shinobu);
        }

        [cmd]
        public async Task Megumin(AnyContext ctx)
        {
            await service.SendWaifuPicsEmbedAsync(ctx, ImageType.megumin);
        }
    }
}