# The MIT License (MIT)

# Copyright (c) 2021-present EQUENOS

# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from __future__ import annotations
from typing import List, Optional, Union, TYPE_CHECKING

from .base import Interaction

from ..components import ActionRow, Button, SelectMenu
from ..enums import ComponentType, try_enum
from ..utils import cached_slot_property
from ..message import Message

__all__ = (
    "MessageInteraction",
    "MessageInteractionData",
)

if TYPE_CHECKING:
    from ..types.interactions import (
        Interaction as InteractionPayload,
        ComponentInteractionData as ComponentInteractionDataPayload,
    )
    from ..state import ConnectionState


class MessageInteraction(Interaction):
    """Represents an interaction with a message component.

    Current examples are buttons and dropdowns.

    .. versionadded:: 2.1

    Attributes
    -----------
    id: :class:`int`
        The interaction's ID.
    guild_id: Optional[:class:`int`]
        The guild ID the interaction was sent from.
    channel_id: Optional[:class:`int`]
        The channel ID the interaction was sent from.
    application_id: :class:`int`
        The application ID that the interaction was for.
    author: Optional[Union[:class:`User`, :class:`Member`]]
        The user or member that sent the interaction.
    guild: Optional[:class:`Guild`]
        The guild the interaction was sent from.
    channel: Optional[Union[:class:`abc.GuildChannel`, :class:`PartialMessageable`, :class:`Thread`]]
        The channel the interaction was sent from.
    message: Optional[:class:`Message`]
        The message that sent this interaction.
    me: Union[:class:`.Member`, :class:`.ClientUser`]
        Similar to :attr:`.Guild.me`
    permissions: :class:`Permissions`
        The resolved permissions of the member in the channel, including overwrites.
    response: :class:`InteractionResponse`
        Returns an object responsible for handling responding to the interaction.
    followup: :class:`Webhook`
        Returns the follow up webhook for follow up interactions.
    type: :class:`InteractionType`
        The interaction type.
    token: :class:`str`
        The token to continue the interaction. These are valid
        for 15 minutes.
    data: :class:`MessageInteractionData`
        The wrapped interaction data.
    """

    target: Message

    def __init__(self, *, data: InteractionPayload, state: ConnectionState):
        super().__init__(data=data, state=state)
        self.data = MessageInteractionData(data=data.get("data", {}))
        self.message = Message(state=self._state, channel=self.channel, data=data["message"])  # type: ignore

    @property
    def values(self) -> Optional[List[str]]:
        """Optional[List[:class:`str`]]: The values the user selected"""
        return self.data.values

    @cached_slot_property("_cs_component")
    def component(self) -> Union[Button, SelectMenu]:
        """Union[:class:`Button`, :class:`SelectMenu`]: The component the user interacted with"""
        for action_row in self.message.components:
            if not isinstance(action_row, ActionRow):
                continue
            for component in action_row.children:
                if not isinstance(component, (Button, SelectMenu)):
                    continue

                if component.custom_id == self.data.custom_id:
                    return component

        raise Exception("MessageInteraction is malformed - no component found")


class MessageInteractionData:
    """Represents the data of an interaction with a message component.

    .. versionadded:: 2.1

    Attributes
    ----------
    custom_id: :class:`str`
        The custom ID of the component.
    component_type: :class:`ComponentType`
        The type of the component.
    values: Optional[List[:class:`str`]]
        The values the user has selected.
    """

    __slots__ = ("custom_id", "component_type", "values")

    def __init__(self, *, data: ComponentInteractionDataPayload):
        self.custom_id: str = data.get("custom_id")
        self.component_type: ComponentType = try_enum(ComponentType, data.get("component_type", 0))
        self.values: Optional[List[str]] = data.get("values")
