"""
Module containing the ServerConnectionSettings
"""

from dataclasses import dataclass


@dataclass
class ServerConnectionSettings:
    """
    Represents the settings needed to connect to a server with a specific weight.

    Args:
        hostname (str): The hostname of the server.
        port (int): The port number to connect to the server on.
        weight (int, optional): The weight of the connection to this server. Defaults to 1.
    """

    hostname: str
    port: int
    weight: int = 1

    @staticmethod
    def from_string(connection_string: str):
        """
        parse a connection string into a list of ServerConnectionSettings objects.

        Args:
            connection_string (str): A string containing server connection settings separated by '#'.
            Each setting is in the format 'hostname:port:weight'.
        """
        servers: list[ServerConnectionSettings] = []
        for server_string in connection_string.split("#"):
            server_parts = server_string.split(":")
            if len(server_parts) != 3:
                raise ValueError(
                    f"""Connection String invalid, server '{connection_string}' must be formatted as hostname:port:weight.
                    For example: server1:4000:1 for one Server or server1:4000:1#server2:4000:1 for multiple servers"""
                )
            servers.append(ServerConnectionSettings(server_parts[0], int(server_parts[1]), int(server_parts[2])))

        return servers
