import discord
from discord.ext import commands
from .utils.chat_formatting import escape_mass_mentions, italics, pagify
from random import randint
from random import choice
from enum import Enum
from urllib.parse import quote_plus
import datetime
import time
import aiohttp
import asyncio

class RPS(Enum):
	rock	 = "\N{MOYAI}"
	paper	 = "\N{PAGE FACING UP}"
	scissors = "\N{BLACK SCISSORS}"


class General:
	"""General commands."""

	def __init__(self, bot):
		self.bot = bot
		self.stopwatches = {}

	@commands.command(hidden=True)
	async def ping(self):
		"""Pong."""
		await self.bot.say("Pong.")

	@commands.command()
	async def choose(self, *choices):
		"""Chooses between multiple choices.

		To denote multiple choices, you should use double quotes.
		"""
		choices = [escape_mass_mentions(c) for c in choices]
		if len(choices) < 2:
			await self.bot.say('Not enough choices to pick from.')
		else:
			await self.bot.say(choice(choices))

	@commands.command(pass_context=True)
	async def roll(self, ctx, number : int = 100):
		"""Rolls random number (between 1 and user choice)

		Defaults to 100.
		"""
		author = ctx.message.author
		if number > 1:
			n = randint(1, number)
			await self.bot.say("{} :game_die: {} :game_die:".format(author.mention, n))
		else:
			await self.bot.say("{} Maybe higher than 1? ;P".format(author.mention))

	@commands.command(aliases=["sw"], pass_context=True)
	async def stopwatch(self, ctx):
		"""Starts/stops stopwatch"""
		author = ctx.message.author
		if not author.id in self.stopwatches:
			self.stopwatches[author.id] = int(time.perf_counter())
			await self.bot.say(author.mention + " Stopwatch started!")
		else:
			tmp = abs(self.stopwatches[author.id] - int(time.perf_counter()))
			tmp = str(datetime.timedelta(seconds=tmp))
			await self.bot.say(author.mention + " Stopwatch stopped! Time: **" + tmp + "**")
			self.stopwatches.pop(author.id, None)

	@commands.command(no_pm=True, hidden=True)
	async def hug(self, user : discord.Member, intensity : int=1):
		"""Because everyone likes hugs

		Up to 10 intensity levels."""
		name = italics(user.display_name)
		if intensity <= 0:
			msg = "(っ˘̩╭╮˘̩)っ" + name
		elif intensity <= 3:
			msg = "(っ´▽｀)っ" + name
		elif intensity <= 6:
			msg = "╰(*´︶`*)╯" + name
		elif intensity <= 9:
			msg = "(つ≧▽≦)つ" + name
		elif intensity >= 10:
			msg = "(づ￣ ³￣)づ{} ⊂(´・ω・｀⊂)".format(name)
		await self.bot.say(msg)

def setup(bot):
	n = General(bot)
	bot.add_cog(n)
