import discord
from redbot.core import Config, app_commands, commands, checks
from . import modals

class Issues(commands.Cog):
    """This cog allows you to create Gitea issues through a Discord modal.
    Developed by SeaswimmerTheFsh."""

    def __init__(self, bot):
        self.bot = bot
        self.config = Config.get_conf(self, identifier=4285273314713, force_registration=True)
        self.config.register_global(
            request_channel = None,
            gitea_root_url = None,
            gitea_repository_owner = None,
            gitea_repository = None,
            gitea_token = None
        )

    @commands.command()
    @checks.is_owner()
    async def issuesconfig(self, ctx: commands.Context, channel: discord.TextChannel = None):
        if channel:
            await self.config.request_channel.set(channel.id)
            await ctx.send(content=f"Channel set to {channel.mention}.\nRun this command again without a channel argument to configure the other settings.")
        else:
            await ctx.channel.send(content="Click the button below to configure the cog.", view=self.IssueConfigurationButton(self.config, ctx))

    @app_commands.command()
    async def issues(self, interaction: discord.Interaction):
        """Found a bug or have a suggestion for the Galaxy bot? Use this command."""
        color = await self.bot.get_embed_color(None)
        embed = discord.Embed(title="Issue Reporting & Suggestions", color=await self.bot.get_embed_color(None), description="Have a problem or a suggestion for the Galaxy bot or GalaxyCogs? Read this!")
        embed.add_field(name="Bot Issues & Suggestions", value="If you'd like to submit a suggestion or a bug report to the developers of the Galaxy bot, please do so with the buttons below or by going [here](https://coastalcommits.com/SeaswimmerTheFsh/GalaxyCogs/issues/new/choose).\n**Please make sure whatever you're suggesting or reporting doesn't have an existing issue! If it does, you can comment on that issue with additional details if necessary.**")
        embed.add_field(name="Cog Issues & Suggestions", value="If you'd like to submit a suggestion or a bug report to the developers of GalaxyCogs, please do so with the buttons below or by going [here](https://coastalcommits.com/SeaswimmerTheFsh/GalaxyCogs/issues/new/choose).\n**Please make sure whatever you're suggesting or reporting doesn't have an existing issue! If it does, you can comment on that issue with additional details if necessary.**")
        await interaction.response.send_message(embed=embed, view=self.IssueButtons(color, self, interaction), ephemeral=True)

    async def submit_issue_request(self, interaction: discord.Interaction, original_interaction: discord.Interaction, embed: discord.Embed):
        channel = self.bot.get_channel(await self.config.request_channel())
        if channel is None:
            await original_interaction.edit_original_response(content="Command cancelled.", view=None)
            await interaction.response.send_message(content="The cog is misconfigured, please report this error.", ephemeral=True)
        try:
            message = await channel.send(content=".")
            await message.edit(content="", embed=embed, view=self.IssueResponseButtons(channel, message.id, interaction.user))
            await original_interaction.edit_original_response(content="Issue request sent!", embed=embed, view=None)
            await interaction.response.defer()
        except (discord.HTTPException, discord.Forbidden) as error:
            await original_interaction.edit_original_response(content="Command cancelled.", view=None)
            await interaction.response.send_message(content=f"The cog is misconfigured, please report this error.\n```{error}```", ephemeral=True)

    class IssueButtons(discord.ui.View):
        def __init__(self, color, cog_instance, original_interaction):
            super().__init__()
            self.color = color
            self.cog_instance = cog_instance
            self.original_interaction = original_interaction

        @discord.ui.button(label="Bot Bug", style=discord.ButtonStyle.danger)
        async def issue_button_bot_bug(self, interaction: discord.Interaction, button: discord.ui.Button): # pylint: disable=unused-argument
            await interaction.response.send_modal(modals.BotBugModal(self.color, self.cog_instance, self.original_interaction))

        @discord.ui.button(label="Cog Bug", style=discord.ButtonStyle.danger)
        async def issue_button_cog_bug(self, interaction: discord.Interaction, button: discord.ui.Button): # pylint: disable=unused-argument
            await interaction.response.send_modal(modals.CogBugModal(self.color, self.cog_instance, self.original_interaction))

        @discord.ui.button(label="Bot Suggestion", style=discord.ButtonStyle.blurple)
        async def issue_button_bot_suggestion(self, interaction: discord.Interaction, button: discord.ui.Button): # pylint: disable=unused-argument
            await interaction.response.send_modal(modals.BotSuggestionModal(self.color, self.cog_instance, self.original_interaction))

        @discord.ui.button(label="Cog Suggestion", style=discord.ButtonStyle.blurple)
        async def issue_button_cog_suggestion(self, interaction: discord.Interaction, button: discord.ui.Button): # pylint: disable=unused-argument
            await interaction.response.send_modal(modals.CogSuggestionModal(self.color, self.cog_instance, self.original_interaction))

    class IssueConfigurationButton(discord.ui.View):
        def __init__(self, config, ctx):
            super().__init__()
            self.config = config
            self.ctx = ctx

        @discord.ui.button(label="Change Configuration", style=discord.ButtonStyle.blurple, row=0)
        async def issue_configuration_button(self, interaction: discord.Interaction, button: discord.ui.Button): # pylint: disable=unused-argument
            await interaction.response.send_modal(modals.IssuesConfigurationModal(self.config, self.ctx))

    class IssueResponseButtons(discord.ui.View):
        def __init__(self, channel, message_id, user):
            super().__init__()
            self.channel = channel
            self.message_id = message_id
            self.user = user

        @discord.ui.button(label="Approve", style=discord.ButtonStyle.green)
        async def issue_response_button_approve(self, interaction: discord.Interaction, button: discord.ui.Button): # pylint: disable=unused-argument
            await interaction.response.send_modal(modals.IssueResponseModal(self.channel, self.message_id, self.user, True))

        @discord.ui.button(label="Deny", style=discord.ButtonStyle.danger)
        async def issue_response_button_deny(self, interaction: discord.Interaction, button: discord.ui.Button): # pylint: disable=unused-argument
            await interaction.response.send_modal(modals.IssueResponseModal(self.channel, self.message_id, self.user, False))
