from parallels.core import safe_format
from parallels.plesk.source.web import messages
from parallels.plesk.source.web.actions.base.site_migration import SiteMigrationAction
from parallels.plesk.source.web.utils.site_info_storage import DomainType
from parallels.plesk.source.web.utils.site_migration_exception import SiteMigrationError


class RetrieveTargetSiteInfo(SiteMigrationAction):
    def get_description(self):
        """Get short description of action as string

        :rtype: str | unicode
        """
        return messages.ACTION_RETRIEVE_TARGET_SITE_INFO_DESCRIPTION

    def get_failure_message(self, global_context):
        """Get message for situation when action failed

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :rtype: str | unicode
        """
        return messages.ACTION_RETRIEVE_TARGET_SITE_INFO_FAILURE

    def run(self, global_context):
        """
        :type global_context: parallels.plesk.source.web.global_context.WebGlobalMigrationContext
        """
        hosting_repo = global_context.hosting_repository

        domain_name = global_context.conn.get_source_config().target_domain
        if hosting_repo.subscription.is_exists(domain_name):
            subscription = hosting_repo.subscription.get_by_name(domain_name)
            subscription_name = domain_name
            domain_type = DomainType.SUBSCRIPTION
            domain_id = subscription.domain_id
        else:
            domain = hosting_repo.domain.get_by_name(domain_name)
            if domain is not None:
                subscription_name = domain.subscription_name
                domain_type = DomainType.ADDON
                domain_id = domain.domain_id
            else:
                subdomain = hosting_repo.subdomain.get_by_name(domain_name)
                if subdomain is not None:
                    subscription_name = subdomain.subscription_name
                    domain_type = DomainType.SUBDOMAIN
                    domain_id = subdomain.subdomain_id
                else:
                    raise SiteMigrationError(safe_format(
                        messages.NO_DOMAIN_ON_TARGET, domain_name=domain_name
                    ))

        global_context.site_info_storage.set_target_subscription(subscription_name)
        global_context.site_info_storage.set_target_domain_type(domain_type)
        global_context.site_info_storage.set_target_domain_id(domain_id)
        global_context.site_info_storage.set_target_domain_name(domain_name)

        virtual_hosting_info = hosting_repo.virtual_hosting_settings.get_virtual_hosting_settings(
            subscription_name, domain_name
        )

        if virtual_hosting_info is None:
            raise SiteMigrationError(safe_format(
                messages.NO_VIRTUAL_HOSTING, domain=domain_name
            ))

        global_context.site_info_storage.set_target_document_root(
            virtual_hosting_info.document_root
        )
        global_context.site_info_storage.set_target_sysuser_login(
            virtual_hosting_info.sysuser_login
        )
        global_context.site_info_storage.set_target_ip_addresses(
            virtual_hosting_info.ip_addresses
        )
        global_context.site_info_storage.set_target_hosting_ssl(
            virtual_hosting_info.hosting_ssl
        )
        global_context.site_info_storage.set_target_domain_display_name(
            virtual_hosting_info.domain_display_name
        )
