from parallels.core.utils.common import cached
from parallels.plesk_multi_server.connections.service_plesk_target_server import get_service_node_by_subscription_name
from parallels.plesk_multi_server.connections.target_connections import PleskMultiServerTargetConnections
from parallels.plesk_multi_server.hosting_repository.model import PleskMultiServerHostingRepositoryModel
from parallels.plesk.panel import Panel as PleskPanel


class Panel(PleskPanel):
    def has_resellers(self, global_context):
        """Whether panel has resellers

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :rtype: bool
        """
        return False

    @cached
    def get_connections(self, global_context):
        """Get target connections for Plesk Multi Server

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        """
        return PleskMultiServerTargetConnections(global_context.config)

    def get_hosting_repository(self, global_context):
        """Retrieve hosting repository of target Plesk Multi Server

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :rtype: parallels.plesk.hosting_repository.model.PleskHostingRepositoryModel
        """
        return PleskMultiServerHostingRepositoryModel(
            self.get_connections(global_context).plesk_multi_server_management_server
        )

    def get_subscription_plesk_node(self, global_context, subscription_name):
        return get_service_node_by_subscription_name(
            self.get_connections(global_context).plesk_multi_server_management_server,
            subscription_name
        )
