from osuapi import OsuApi, AHConnector
from discord.ext import commands
from .utils import utils
import datetime
import discord
import logging
import aiohttp
import os

log = logging.getLogger(__name__)


class Core(commands.Cog):
    """
    The core of Nurevam, just essentials.
    """
    def __init__(self,bot):
        self.bot = bot
        self.redis=bot.db.redis
        self.bot.say_edit = bot.say

    def get_bot_uptime(self): #to calculates how long it been up
        now = datetime.datetime.utcnow()
        delta = now - self.bot.uptime
        hours, remainder = divmod(int(delta.total_seconds()), 3600)
        minutes, seconds = divmod(remainder, 60)
        days, hours = divmod(hours, 24)
        if days:
            fmt = '{d} days, {h} hours, {m} minutes, and {s} seconds'
        else:
            fmt = '{h} hours, {m} minutes, and {s} seconds'

        return fmt.format(d=days, h=hours, m=minutes, s=seconds)

    def get_time_delta(self,person):
        delta = datetime.datetime.utcnow() - person
        hours, remainder = divmod(int(delta.total_seconds()), 3600)
        minutes, seconds = divmod(remainder, 60)
        days, hours = divmod(hours, 24)
        if days:
            fmt = '{d} days, {h} hours, {m} minutes, and {s} seconds'
        else:
            fmt = '{h} hours, {m} minutes, and {s} seconds'

        return fmt.format(d=days, h=hours, m=minutes, s=seconds)


    @commands.command()
    async def uptime(self,ctx): #Showing Time that bot been total run
        """Prints the uptime."""
        await self.bot.say(ctx,content = "```py\nI have been up for {}\n```".format(self.get_bot_uptime()))

    @commands.command()
    async def prefix(self,ctx):
        prefix = (await self.redis.get("{}:Config:CMD_Prefix".format(ctx.message.guild.id)))
        prefix = set(prefix + ctx.prefix) #if user didnt set any, it will be default to ! which set prefix to be None? In case it is not, we can add current prefix to it.
        await self.bot.say(ctx,content = "```\n{}\n```".format(",".join(prefix)))

    @commands.command()
    async def info(self,ctx,*,person:discord.Member = None):
        """
        About Nurevam or person by mention info
        """

        if not person:
            guild = len(self.bot.guilds)
            member = len(set(self.bot.get_all_members()))
            app = await self.bot.application_info()
            msg = "Name:{}".format(self.bot.user)
            if ctx.message.guild.me.nick:
                msg += "\nNickname:{}".format(ctx.message.guild.me.nick)
            msg += "\nCreator: {}".format(app.owner)
            msg += "\nServer:{}\nMembers:{}".format(guild,member)
            link = "If you want to invite this bot to your server, you can check it out here <http://nurevam.site>!"
            return await self.bot.say(ctx,content = "```xl\n{}\n```\n{}\n".format(msg,link))
        else:
            e = discord.Embed()
            e.title = "{} - {}".format(person,person.id)
            e.set_thumbnail(url = person.avatar_url)
            e.add_field(name = "Created at", value="{} - ({})".format(person.created_at,self.get_time_delta(person.created_at)),inline=False)
            e.add_field(name = "Joined at", value="{} - ({})".format(person.joined_at,self.get_time_delta(person.joined_at)),inline=False)
            e.add_field(name = "Total Roles", value=str(len(person.roles)),inline=False)

            if person.colour.value:
                e.colour = person.color
            await self.bot.say(ctx,embed = e)

    @commands.command()