from dataclasses import dataclass
from enum import Enum


@dataclass
class GameTitle:
    name: str
    description: str
    web_link: str
    image_name: str  # Name of the image in assets/img/games

class TournamentFormat(Enum):
    SINGLE_ELIMINATION_BO_1 = 1
    SINGLE_ELIMINATION_BO_3 = 2
    SINGLE_ELIMINATION_BO_5 = 3
    DOUBLE_ELIMINATION_BO_1 = 4
    DOUBLE_ELIMINATION_BO_3 = 5
    DOUBLE_ELIMINATION_BO_5 = 6
    FFA = 7

def tournament_format_to_display_texts(tournament_format: TournamentFormat) -> tuple[str, str]:
    """ Returns tuple where idx 0 is SE/DE/FFA string and idx 1 is match count """
    if tournament_format == TournamentFormat.SINGLE_ELIMINATION_BO_1:
        return "Single Elimination", "1"
    elif tournament_format == TournamentFormat.SINGLE_ELIMINATION_BO_3:
        return "Single Elimination", "3"
    elif tournament_format == TournamentFormat.SINGLE_ELIMINATION_BO_5:
        return "Single Elimination", "5"
    elif tournament_format == TournamentFormat.DOUBLE_ELIMINATION_BO_1:
        return "Double Elimination", "1"
    elif tournament_format == TournamentFormat.DOUBLE_ELIMINATION_BO_3:
        return "Double Elimination", "3"
    elif tournament_format == TournamentFormat.DOUBLE_ELIMINATION_BO_5:
        return "Double Elimination", "5"
    elif tournament_format == TournamentFormat.FFA:
        return "Free for All", "1"
    else:
        raise RuntimeError(f"Unknown tournament status: {str(tournament_format)}")


class TournamentStatus(Enum):
    CLOSED = 1
    OPEN = 2
    COMPLETED = 3
    CANCELED = 4
    INVITE_ONLY = 5  # For Show-matches
    ONGOING = 6

def tournament_status_to_display_text(tournament_status: TournamentStatus) -> str:
    if tournament_status == TournamentStatus.OPEN:
        return "Offen"
    elif tournament_status == TournamentStatus.CLOSED:
        return "Geschlossen"
    elif tournament_status == TournamentStatus.ONGOING:
        return "Läuft"
    elif tournament_status == TournamentStatus.COMPLETED:
        return "Abgeschlossen"
    elif tournament_status == TournamentStatus.CANCELED:
        return "Abgesagt"
    elif tournament_status == TournamentStatus.INVITE_ONLY:
        return "Invite-only"
    else:
        raise RuntimeError(f"Unknown tournament status: {str(tournament_status)}")


class TournamentError(Exception):
    def __init__(self, message: str) -> None:
        super().__init__(message)


class ParticipantType(Enum):
    PLAYER = 1
    TEAM = 2  # ToDo: Teams are not yet supported


class Bracket(Enum):
    UPPER = 1
    LOWER = 2
    FINAL = 3


class MatchStatus(Enum):
    WAITING = 1    # Participants incomplete
    PENDING = 2    # Match is ready to be played
    DELAYED = 3    # Same as PENDING, but with flag for UI
    COMPLETED = 4  # Match has been played
    CANCELED = 5   # Match got canceled, "bye" for followup