﻿using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Discord.WebSocket;
using Discord;
using Humanizer;
using CatBot.Utilities;
using static CatBot.Utilities.Bot;
using static CatBot.Utilities.Commands;
using static CatBot.Utilities.Enums;
using static CatBot.Utilities.ExternalNotifications;
using static CatBot.Utilities.Guilds;
using static CatBot.Utilities.Messages;
using static CatBot.Utilities.Time;
using static CatBot.Utilities.Users;

namespace CatBot.Services
{
    public static class NotifyOwnerService
    {
        private static DiscordSocketClient _client;
        private static SocketUser _catherine;

        public static async Task Install(DiscordSocketClient client)
        {
            await Task.Run( () =>
                {
                    _client = client;
                    _catherine = _client.GetUser(OWNER_ID);
                    Console.WriteLine($"Found owner: {FullUsername(_catherine)}");
                }
            );
        }

        public static async Task SendMessage(string message)
        {
            Console.WriteLine(message);
            string message1 = message;
            string message2 = "";
            string message3 = "";

            if (message.Length > 2000 && message.Length < 4000)
            {
                message1 = message.Remove(2000);
                message2 = message.Substring(2000);
            }
            else if (message.Length > 4000 && message.Length < 6000)
            {
                message1 = message.Remove(2000);
                message2 = message.Substring(2000, 2000);
                message3 = message.Substring(4000);
            }

            await (await _catherine.GetOrCreateDMChannelAsync()).SendMessageAsync(message1);
            if (!string.IsNullOrWhiteSpace(message2))
                await (await _catherine.GetOrCreateDMChannelAsync()).SendMessageAsync(message2);
            if (!string.IsNullOrWhiteSpace(message3))
                await (await _catherine.GetOrCreateDMChannelAsync()).SendMessageAsync(message3);
        }

        public static async Task SendServerMessage(string ebheader, SocketGuild guild)
        {
            var ebbot = new EmbedBuilder()
            {
                Color = BlueInfoEmbed,
                Title = $"{Emoji(EmojiList.Informational)} {ebheader}",
            };

            ebbot.AddField(x =>
            {
                x.IsInline = false;
                x.Name = "Server Name";
                x.Value = $"{guild.Name} (ID: {guild.Id})";
            });

            ebbot.AddField(x =>
            {
                x.IsInline = true;
                x.Name = "Region";
                x.Value = $"{(guild.VoiceRegionId).Humanize().Transform(To.LowerCase, To.TitleCase)}";
            });

            ebbot.AddField(x =>
            {
                x.IsInline = true;
                x.Name = "Owner";
                x.Value = $"{FullUsername(guild.Owner)}";
            });

            ebbot.AddField(x =>
            {
                x.IsInline = true;
                x.Name = "Roles";
                x.Value = $"{guild.Roles.Count}";
            });

            ebbot.AddField(x =>
            {
                x.IsInline = true;
                x.Name = "Members";
                x.Value = $"{guild.MemberCount}";
            });

            ebbot.AddField(x =>
            {
                x.IsInline = true;
                x.Name = $"Channels ({guild.Channels.Count})";
                x.Value = $"{guild.TextChannels.Count} Text | {guild.VoiceChannels.Count} Voice";
            });

            await (await _catherine.GetOrCreateDMChannelAsync()).SendMessageAsync("", embed: ebbot);

            ebbot = null;
            return;
        }

    }
}