from parallels.core.hosting_repository.mail_account import MailAccountModel
from parallels.plesk.hosting_repository.mail_account import PleskMailAccountModel
from parallels.plesk_multi_server.hosting_repository.base import PleskMultiServerBaseModel


class PleskMultiServerMailAccountModel(MailAccountModel, PleskMultiServerBaseModel):
    def create_from_dump(self, subscription_name, mail_account_dump):
        """Create mail account in target panel

        :type subscription_name: str | unicode
        :type mail_account_dump: parallels.core.dump.entity.mail_account.MailAccount
        """
        server = self.get_subscription_service_server(subscription_name)
        return PleskMailAccountModel(server).create_from_dump(
            subscription_name, mail_account_dump
        )

    def create(self, mail_account, subscription_name):
        """Create mail account in target panel

        :type subscription_name: str | unicode
        :type mail_account: parallels.core.hosting_repository.mail_account.CreateMailAccountEntity
        """
        server = self.get_subscription_service_server(subscription_name)
        return PleskMailAccountModel(server).create(
            mail_account, subscription_name
        )

    def enable_mailbox(self, subscription_name, mail_user_name, domain_name, password):
        """Enable mailbox for specified mail account

        :type subscription_name: str | unicode
        :type mail_user_name: str | unicode
        :type domain_name: str | unicode
        :type password: str | unicode
        """
        server = self.get_subscription_service_server(subscription_name)
        return PleskMailAccountModel(server).enable_mailbox(
            subscription_name, mail_user_name, domain_name, password
        )

    def set_forwarding(self, subscription_name, mail_account_name, is_enable_forwarding, forwarding_addresses):
        """Set forwarding for given mail user in target panel

        :type subscription_name: str | unicode
        :type mail_account_name: str | unicode
        :type is_enable_forwarding: bool
        :type forwarding_addresses: list[str]
        """
        server = self.get_subscription_service_server(subscription_name)
        return PleskMailAccountModel(server).set_forwarding(
            subscription_name, mail_account_name, is_enable_forwarding, forwarding_addresses
        )

    def set_aliases(self, subscription_name, mail_account_name, aliases):
        """Set aliases for given mail account in target panel

        :type subscription_name: str | unicode
        :type mail_account_name: str | unicode
        :type aliases: list[str]
        """
        server = self.get_subscription_service_server(subscription_name)
        return PleskMailAccountModel(server).set_aliases(
            subscription_name, mail_account_name, aliases
        )

    def is_exists_by_subscription(self, subscription_name, mail_user_name, domain_name):
        """Check if mail user with given name exists in target panel for given domain

        :type subscription_name: str | unicode
        :type mail_user_name: str | unicode
        :type domain_name: str | unicode
        :rtype: bool
        """
        server = self.get_subscription_service_server(subscription_name)
        return PleskMailAccountModel(server).is_exists_by_subscription(
            subscription_name, mail_user_name, domain_name
        )

    def is_mailbox_enabled(self, subscription_name, mail_user_name, domain_name):
        """Check if mail account with given name exists in target panel and mailbox is enabled for it

        Returns:
        - True, if mail account exists and mailbox is enabled
        - False, if mail account exists and mailbox is disabled
        - None, if mail account does not exists

        :type subscription_name: str | unicode
        :type mail_user_name: str | unicode
        :type domain_name: str | unicode
        :rtype: bool | None
        """
        server = self.get_subscription_service_server(subscription_name)
        return PleskMailAccountModel(server).is_mailbox_enabled(
            subscription_name, mail_user_name, domain_name
        )

    def get_password(self, subscription_name, mail_user_name, domain_name):
        """Get password (encrypted) of mail account with given name

        Returns tuple (password, password type).

        :type subscription_name: str | unicode
        :type mail_user_name: str | unicode
        :type domain_name: str | unicode
        :rtype: tuple
        """
        server = self.get_subscription_service_server(subscription_name)
        return PleskMailAccountModel(server).get_password(
            subscription_name, mail_user_name, domain_name
        )

    def get_mailbox_effective_quota(self, subscription_name, mail_user_name, domain_name):
        """Get effective quota (according to mailbox limit and subscription limit) of mail account with given name

        Returns quota as string value.

        :type subscription_name: str | unicode
        :type mail_user_name: str | unicode
        :type domain_name: str | unicode
        :rtype: str
        """
        server = self.get_subscription_service_server(subscription_name)
        return PleskMailAccountModel(server).get_mailbox_effective_quota(
            subscription_name, mail_user_name, domain_name
        )

    def get_list_by_domain(self, subscription_name, domain_name):
        """Retrieve list of mail accounts in panel for given domain

        :type subscription_name: str | unicode
        :type domain_name: str | unicode
        :rtype: list[parallels.core.hosting_repository.mail_account.MailAccountEntity]
        """
        server = self.get_subscription_service_server(subscription_name)
        return PleskMailAccountModel(server).get_list_by_domain(
            subscription_name, domain_name
        )
