from parallels.core import safe_format
from parallels.core.utils.common import is_empty
from parallels.plesk.source.web import messages
from parallels.plesk.source.web.migrated_database_user import WebMigratedDatabaseUser


class WebMigratedDatabase(object):
    def __init__(self, global_context, subscription, name, host, port, username, password, prefix=None):
        """
        :type global_context: parallels.plesk.source.web.global_context.WebGlobalMigrationContext
        :type subscription: parallels.core.migrated_subscription.MigratedSubscription
        """
        self._global_context = global_context
        self._subscription = subscription

        self._source_name = name
        self._source_host = host

        if not is_empty(port):
            self._source_port = port
        else:
            self._source_port = 3306

        self._source_username = username
        self._source_password = password

        self._migrated_user = WebMigratedDatabaseUser(self, username, password)

        self._target_name = self._source_name

        self._source_prefix = prefix
        self._target_prefix = self._source_prefix

    def description(self):
        """
        :rtype: str | unicode
        """
        return safe_format(
            messages.DATABASE_DESCRIPTION, name=self.source_name
        )

    @property
    def source_name(self):
        """
        :rtype: str | unicode
        """
        return self._source_name

    @property
    def source_host(self):
        """
        :rtype: str | unicode
        """
        return self._source_host

    @property
    def source_port(self):
        """
        :rtype: str | unicode
        """
        return self._source_port

    @property
    def database_user(self):
        """
        :rtype: parallels.plesk.source.web.migrated_database_user.WebMigratedDatabaseUser
        """
        return self._migrated_user

    @property
    def target_server(self):
        """
        :rtype: parallels.core.connections.database_servers.base.DatabaseServer | None
        """
        return self._global_context.target_panel_obj.get_subscription_nodes(
            self._global_context, self.target_subscription_name
        ).database.get('mysql')

    @property
    def target_subscription_name(self):
        """
        :rtype: str | unicode
        """
        return self._subscription.name

    @property
    def target_name(self):
        """
        :rtype: str | unicode
        """
        return self._target_name

    def set_target_name(self, name):
        """
        :type name: str | unicode
        :rtype: None
        """
        self._target_name = name

    @property
    def source_prefix(self):
        """
        :rtype: str | unicode | None
        """
        return self._source_prefix

    @property
    def target_prefix(self):
        """
        :rtype: str | unicode | None
        """
        return self._target_prefix

    @target_prefix.setter
    def target_prefix(self, value):
        """
        :value: str | unicode
        """
        self._target_prefix = value

    def is_valid(self):
        return (
            not is_empty(self.source_host)
            and not is_empty(self.source_name)
            and not is_empty(self.database_user.source_username)
            and not is_empty(self.database_user.source_password)
        )
