using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Discord.Commands;
using Discord;
using System.IO;
using System.Reflection;
using System;

namespace Terrabot
{
    /// <summary>
    /// This class does the jukebox fuctionality of terrabot. It facilitates the playing of raw music plays to terrabot and displaying the user the songs.
    /// </summary>
    [Group("Juke")]
    public class Juke : ModuleBase
    {
        public string dir = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\songs\\";
        private readonly AudioService _service;
        public Juke(AudioService service)
        {
            _service = service;
        }
        [Command(RunMode = RunMode.Async)]
        public async Task JukeCmd([Remainder] string parse)
        {
            // User request directory and terrabot displays it
            if (parse.Count().Equals(1))
            {
                var filenames4 = Directory
                    .EnumerateFiles(dir, "*")
                    .Select(Path.GetFileName);
                List<string> files = filenames4.ToList();
                if (files.Contains($"{parse}.txt"))
                {
                    List<string> AlphaList = System.IO.File.ReadAllLines(dir + parse + ".txt").ToList();
                    var everyother = AlphaList.Where((x, i) => i % 2 == 0);
                    string content = string.Join(System.Environment.NewLine, everyother.ToArray());
                    string output = $"```{content}```";
                    await ReplyAsync(output);
                }
            }
            // User requests a song and terabot plays it.
            if (parse.Count() > 1)
            {
                string queue = parse.Remove(1, parse.Count() - 1);
                string songnumber = parse.Remove(0, 1);
                if (!int.TryParse(songnumber, out int songint))
                {
                    await ReplyAsync($"Invalid Number. {songint}");
                    return;
                };
                string playbackdir = $"{dir}{queue}\\";
                List<string> AlphaList = System.IO.File.ReadAllLines(dir + queue + ".txt").ToList();
                var names = AlphaList.Where((x, i) => i % 2 == 0);
                var tracknames = AlphaList.Where((x, i) => i % 2 == 1);
                if (songint > tracknames.Count())
                {
                    await ReplyAsync($"Invalid Number. {songint}");
                    return;
                }
                Predicate<IGuild> guildFinder = (IGuild lockgf) => { return lockgf == Context.Guild; };
                IGuild Line = _service.LLock.Find(guildFinder);
                if (Line == Context.Guild)
                {
                    await _service.Queue("juke", Context.Guild, (Context.User as IVoiceState).VoiceChannel, Context.Message.Channel, $"{playbackdir}{tracknames.ElementAt(songint - 1)}");
                    await ReplyAsync($"{names.ElementAt(songint - 1)} has been added to the queue.");
                }
                else
                {
                    await ReplyAsync($"Now playing `{names.ElementAt(songint - 1)}`");
                    await _service.JoinAudio(Context.Guild, (Context.User as IVoiceState).VoiceChannel);
                    await _service.SendAudioAsync(Context.Guild, Context.Message.Channel, $"{playbackdir}{tracknames.ElementAt(songint - 1)}");
                    return;
                }
            }
        }
    }
}
