from discord import ButtonStyle, Interaction, Message, ui
from discord.errors import NotFound
from redbot.core import commands

from ..models.type import Type
from ..utilities.config import config
from ..utilities.factory import type_embed


class Types(ui.View):
    def __init__(self, ctx: commands.Context, message: Message, moderation_type: Type, timeout: int | None = None):
        super().__init__()
        self.ctx = ctx
        self.message = message
        self.type = moderation_type
        self.timeout = timeout

    async def on_timeout(self):
        try:
            await self.message.edit(view=None)
        except NotFound:
            pass

    @ui.button(label="Show in History", style=ButtonStyle.green, row=0)
    async def show_in_history(self, interaction: Interaction, button: ui.Button): # pylint: disable=unused-argument
        if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
            await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
            return
        await interaction.response.defer()
        current_setting = await config.custom("types", interaction.guild.id, self.type.key).show_in_history()
        await config.custom("types", interaction.guild.id, self.type.key).show_in_history.set(not current_setting)
        await interaction.message.edit(embed=await type_embed(self.ctx, self.type))

    @ui.button(label="Show Moderator", style=ButtonStyle.green, row=0)
    async def show_moderator(self, interaction: Interaction, button: ui.Button): # pylint: disable=unused-argument
        if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
            await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
            return
        await interaction.response.defer()
        current_setting = await config.custom("types", interaction.guild.id, self.type.key).show_moderator()
        await config.custom("types", interaction.guild.id, self.type.key).show_moderator.set(not current_setting)
        await interaction.message.edit(embed=await type_embed(self.ctx, self.type))

    @ui.button(label="Use Discord Permissions", style=ButtonStyle.green, row=0)
    async def use_discord_permissions(self, interaction: Interaction, button: ui.Button): # pylint: disable=unused-argument
        if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
            await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
            return
        await interaction.response.defer()
        current_setting = await config.custom("types", interaction.guild.id, self.type.key).use_discord_permissions()
        await config.custom("types", interaction.guild.id, self.type.key).use_discord_permissions.set(not current_setting)
        await interaction.message.edit(embed=await type_embed(self.ctx, self.type))

    @ui.button(label="DM Users", style=ButtonStyle.green, row=0)
    async def dm_users(self, interaction: Interaction, button: ui.Button): # pylint: disable=unused-argument
        if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
            await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
            return
        await interaction.response.defer()
        current_setting = await config.custom("types", interaction.guild.id, self.type.key).dm_users()
        await config.custom("types", interaction.guild.id, self.type.key).dm_users.set(not current_setting)
        await interaction.message.edit(embed=await type_embed(self.ctx, self.type))

    @ui.button(label="Reset", style=ButtonStyle.red, row=1)
    async def reset(self, interaction: Interaction, button: ui.Button): # pylint: disable=unused-argument
        if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
            await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
            return
        await interaction.response.defer()
        await config.custom("types", interaction.guild.id, self.type.key).clear()
        await interaction.message.edit(embed=await type_embed(self.ctx, self.type))
