using Discord;
using Discord.Commands;
using System;
using System.Threading.Tasks;

namespace Terrabot
{
    /// <summary>
    /// This module does the commands for joining leaving and running the audio functions of terrabot
    /// </summary>
    public class AudioModule : ModuleBase<ICommandContext>
    {
        private readonly AudioService _service;
        public AudioModule(AudioService service)
        {
            _service = service;
        }
        [Command("join", RunMode = RunMode.Async)]
        public async Task JoinCmd()
        {
            await _service.JoinAudio(Context.Guild, (Context.User as IVoiceState).VoiceChannel);
        }
        [Command("leave", RunMode = RunMode.Async)]
        public async Task LeaveCmd()
        {
            await _service.LeaveAudio(Context.Guild);
        }
        [Command("next", RunMode = RunMode.Async)]
        public async Task NextCmd()
        {
            await _service.LeaveAudio(Context.Guild);
            if (_service.LQueue.Count > 0)
            {
                await _service.YouProcess(Context.Guild);
            }
        }
        [Command("vol")]
        public async Task VolCmd([Remainder] string vols)
        {
            Int32.TryParse(vols, out int volparse);
            double vol = volparse / 100;
            vol = _service.vol;
            if (_service.vol.Equals(vol))
            {
                await ReplyAsync($"Volume has been set to {vols}%");
            }
            return;
        }
        [Command("Replay", RunMode = RunMode.Async)]
        public async Task ReplayCmd()
        {
            Predicate<IGuild> guildFinder = (IGuild lockgf) => { return lockgf == Context.Guild; };
            IGuild Line = _service.LLock.Find(guildFinder);
            if (Line != Context.Guild)
            {
                string path = System.IO.File.ReadAllText($"{_service.lastplaypath}{Context.Guild.Id}.replay.txt");
                await _service.JoinAudio(Context.Guild, (Context.User as IVoiceState).VoiceChannel);
                await _service.SendAudioAsync(Context.Guild, Context.Channel, path);
            }
            return;
        }
        [Command("Repeat", RunMode = RunMode.Async)]
        public async Task RepeatCmd()
        {
            if (!System.IO.File.ReadAllText($"{_service.lastplaypath}{Context.Guild.Id}.repeat.txt").Equals("-1"))
            {
                System.IO.File.WriteAllText($"{_service.lastplaypath}{Context.Guild.Id}.repeat.txt", "-1");
                await ReplyAsync($"Repeat is now on.");
            }
            else
            {
                System.IO.File.WriteAllText($"{_service.lastplaypath}{Context.Guild.Id}.repeat.txt", "0");
                await ReplyAsync($"Repeat is now off.");
            }
        }
            [Command("ClearQueue")]
            public async Task ClearQueueCMD()
            {
            int count = _service.LQueue.Count;
            await _service.YouClearQueue(Context.Guild);
            await ReplyAsync($"Cleared {count} items from queue");
            }
        }
    }
