using System.Threading.Tasks;
using Discord.Commands;
using SteamStoreQuery;
using System.Collections.Generic;
using System;

namespace Terrabot
{
    /// <summary>
    /// This runs misc commands for terrabot.
    /// </summary>
    public class TerraModule : ModuleBase<ICommandContext>
    {
        private readonly TerraService _service;
        public TerraModule(TerraService service)
        {
            _service = service;
        }
        [Command("ping")]
        public async Task PingCmd()
        {
            await ReplyAsync("Pong!");
        }
        [Command("steam")]
        public async Task SteamCmd([Remainder] string text)
        {
            string searchQuery = text;
            List<Listing> results = Query.Search(searchQuery);
            try
            {
                await ReplyAsync($"The first result is {results[0].Name}, and it costs ${results[0].PriceUSD}. You can find it here: {results[0].StoreLink}");
            }
            catch (System.ArgumentOutOfRangeException)
            {
                await ReplyAsync("I couldn't find what you're asking for.");
            }
        }
        [Command("r")]
        public async Task RollCmd([Remainder] string text)
        {
            int rollmax;
            if (Int32.TryParse(text, out rollmax))
            {
                int random = _service.GetRand();
                if (random < 0) { random = random * -1; }
                double rolled = random / (int.MaxValue / rollmax-1);
                int dicebal = Convert.ToInt32(rolled+1);
                await ReplyAsync($"Your Roll is {dicebal}");
            }
            else{ await ReplyAsync("Please Enter a Number"); }
        }
        [Command("ID")]
        public async Task IDCmd()
        {
            await ReplyAsync($"Your ID is {Context.User.Id.ToString()}");
            ulong uid = Context.User.Id;
        }
        [Command("GUILD")]
        public async Task GUILDCmd()
        {
            await ReplyAsync($"Your Guild ID is {Context.Guild.Id.ToString()}");
        }
        [Command("Terrabot")]
        public async Task TerraCmd()
        {
            string content = string.Join(System.Environment.NewLine, System.IO.File.ReadAllLines("terrabot.txt"));
            await ReplyAsync($"```{content}```");
        }
    }
}