from parallels.core.hosting_repository.database_user import DatabaseUserModel
from parallels.plesk.hosting_repository.base import PleskBaseModel
from parallels.plesk.hosting_repository.utils.cli.database import DatabaseUserCreateCli, DatabaseUserRegisterCli


class PleskDatabaseUserModel(DatabaseUserModel, PleskBaseModel):
    def create_from_dump(self, database_user_dump, subscription_name, is_assimilate=False):
        """Create database user in target Plesk

        :type database_user_dump: parallels.core.dump.data_model.DatabaseUser
        :type subscription_name: str
        :type is_assimilate: bool
        """
        if is_assimilate:
            command = DatabaseUserRegisterCli(
                runner=self.plesk_cli_runner,
                database_user_name=database_user_dump.name,
                database_user_password=database_user_dump.password.password_text,
                database_user_password_type=database_user_dump.password.password_type,
                database_name=database_user_dump.database_name,
                database_type=database_user_dump.dbtype,
                database_host=database_user_dump.host,
                database_port=database_user_dump.port,
                subscription_name=subscription_name
            )
        else:
            command = DatabaseUserCreateCli(
                runner=self.plesk_cli_runner,
                database_user_name=database_user_dump.name,
                database_user_password=database_user_dump.password.password_text,
                database_user_password_type=database_user_dump.password.password_type,
                database_name=database_user_dump.database_name,
                database_type=database_user_dump.dbtype,
                database_host=database_user_dump.host,
                database_port=database_user_dump.port,
                subscription_name=subscription_name
            )
        command.run()

    def create(self, database_user, subscription_name):
        """Create database user in target panel

        :type database_user: parallels.core.hosting_repository.database_user.CreateDatabaseUserEntity
        :type subscription_name: str | unicode
        :return:
        """
        command = DatabaseUserCreateCli(
            runner=self.plesk_cli_runner,
            database_user_name=database_user.name,
            database_user_password=database_user.password,
            database_user_password_type=database_user.password_type,
            database_name=database_user.database_name,
            database_type=database_user.dbtype,
            database_host=database_user.host,
            database_port=database_user.port,
            subscription_name=subscription_name
        )
        command.run()

    def is_exists_by_subscription(self, subscription_name, database_user_name):
        """Check if database user with given name exists in target Plesk

        :type subscription_name: subscription_name
        :type database_user_name: str
        :rtype: bool
        """
        result = self.plesk_server.query_panel_db(
            'SELECT id FROM db_users WHERE login = %(database_user_name)s', dict(
                database_user_name=database_user_name
            )
        )
        return len(result) > 0
