from parallels.core import get_logger
from parallels.core.actions.utils.logging_properties import LoggingProperties
from parallels.plesk.source.web import messages
from parallels.plesk.source.web.actions.base.site_migration import SiteMigrationAction

logger = get_logger(__name__)


class GetSourceIsWindows(SiteMigrationAction):
    """Get info about OS type on source web site, Windows or not

    """

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

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

    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.FAILED_ACTION_GET_SOURCE_IS_WINDOWS

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

        If action is critical and it failed for a subscription, migration tool
        won't run the next operations for the subscription.

        :rtype: bool
        """
        return False

    def get_logging_properties(self):
        """Get how action should be logged to migration tools end-user

        :rtype: parallels.core.actions.utils.logging_properties.LoggingProperties
        """
        return LoggingProperties(info_log=False, compound=False)

    def run(self, global_context):
        """
        :type global_context: parallels.plesk.source.web.global_context.WebGlobalMigrationContext
        """
        source = global_context.conn.get_source()
        web_rpc_runner = source.get_web_rpc_runner()
        is_windows = web_rpc_runner.is_windows

        if is_windows:
            logger.fdebug(messages.SOURCE_WINDOWS_DETECTED)
        else:
            logger.fdebug(messages.SOURCE_LINUX_DETECTED)

        global_context.site_info_storage.set_is_windows(is_windows)
