import discord
from discord.ext import commands
from discord.utils import get
import hrochobot.utils.data as data

MAILS_JSON = "mails"

class Mail(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @discord.message_command()
    async def mail(self, ctx: discord.Interaction, message: discord.Message):
        mails_json = data.load_data(MAILS_JSON)
        if str(ctx.author.id) not in mails_json:
            return await ctx.respond(f"Mail not set.", ephemeral=True)
        mail = mails_json[str(ctx.author.id)]

        if message.author.nick:
            header = f"{message.author.nick} ({message.author})"
        else:
            header = f"{message.author}"
        
        time = message.created_at.strftime("%Y-%m-%d %H:%M:%S")
        header += f" wrote on {time} in channel '{message.channel}' server '{message.guild}':"
        
        # TODO: Send mail here
        print(mail)
        print(f"{header}\n{message.jump_url}\n\n{message.content}")
        
        return await ctx.respond("Message sent to email.", ephemeral=True)

    @discord.slash_command(description="Sets mail for sending emails.")
    @discord.option("mail", str, description="Mail to set.")
    async def set_mail(self, ctx: discord.Interaction, mail: str):
        mails_json = data.load_data(MAILS_JSON)
        mails_json[ctx.author.id] = mail
        data.dump_data(MAILS_JSON, mails_json)
        return await ctx.respond(f"Mail set to ``{mail}``.", ephemeral=True)


def setup(bot):
    bot.add_cog(Mail(bot))