from parallels.core import messages
from parallels.core.actions.base.subscription_action import SubscriptionAction
from parallels.core.actions.hosting_settings.detect_scripting.utils import (
    upload_probe, list_domains_to_detect_scripting
)
from parallels.core.dump.data_model import Scripting
from parallels.core.logging import get_logger
from parallels.core.migrator_config import read_detect_scripting_settings_enabled
from parallels.core.utils.http_client import perform_simple_http_request

logger = get_logger(__name__)


class DetectASPAction(SubscriptionAction):
    def get_description(self):
        """Get short description of action as string

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

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

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :type subscription: parallels.core.migrated_subscription.MigratedSubscription
        :rtype: str | unicode
        """
        return messages.DETECT_ASP_ACTION_FAILURE

    def filter_subscription(self, global_context, subscription):
        """Check if we should run this action on given subscription or not

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :type subscription: parallels.core.migrated_subscription.MigratedSubscription
        :rtype: bool
        """
        return (
            read_detect_scripting_settings_enabled(global_context.config) and
            subscription.web_source_server.is_windows()
        )

    def is_critical(self):
        """If action is critical or not

        :rtype: bool
        """
        return False

    def run(self, global_context, subscription):
        """
        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :type subscription: parallels.core.migrated_subscription.MigratedSubscription
        """
        domains_to_detect_scripting = list_domains_to_detect_scripting(global_context, subscription)
        for domain, source_document_root, source_domain_ip in domains_to_detect_scripting:
            self._update_asp(subscription, domain, source_document_root, source_domain_ip)

    def _update_asp(self, subscription, domain, source_document_root, source_domain_ip):
        """Detect whether ASP is enabled, update dump

        :type subscription: parallels.core.migrated_subscription.MigratedSubscription
        :type domain: parallels.core.dump.data_model.Domain
        :type source_document_root: str | unicode
        :type source_domain_ip: str | unicode
        """
        if domain.scripting is None:
            return

        is_asp_enabled = self._detect_asp(subscription, domain, source_document_root, source_domain_ip)

        if is_asp_enabled:
            domain.scripting.enable_option(Scripting.OPTION_ASP)

    @staticmethod
    def _detect_asp(subscription, domain, source_document_root, source_domain_ip):
        """Detect whether ASP is enabled by uploading probe script

        :type subscription: parallels.core.migrated_subscription.MigratedSubscription
        :type domain: parallels.core.dump.data_model.Domain
        :type source_document_root: str | unicode
        :type source_domain_ip: str | unicode
        """
        asp_script_contents = '<% response.write("ASP is") %> enabled'

        with upload_probe(
            subscription.web_source_server, source_document_root, "asp", asp_script_contents
        ) as temp_script_name:
            response_body = perform_simple_http_request(source_domain_ip, domain.name_idn, temp_script_name)
            if 'ASP is enabled' not in response_body:
                return False
            else:
                logger.fdebug(messages.DETECTED_ASP_IS_ENABLED, site=domain.name)
                return True
