from parallels.core.actions.utils.logging_properties import LoggingProperties
from parallels.core.actions.utils.repeat_properties import RepeatProperties
from parallels.core.logging import get_logger
from parallels.core.utils.config_utils import global_section
from parallels.plesk.source.web import messages
from parallels.plesk.source.web.actions.base.site_migration import SiteMigrationAction

logger = get_logger(__name__)


class RemoveWebRPCAgent(SiteMigrationAction):
    """Remove web agent(s) from the source servers"""

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

        :rtype: str | unicode
        """
        return messages.ACTION_REMOVE_WEB_RPC_AGENT_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_REMOVE_WEB_RPC_AGENT_DESCRIPTION_FAILURE

    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(compound=False)

    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 filter_action(self, global_context):
        """Check whether we should run this action or not. By default True - action should be executed.

        Arguments:
        - global_context - registry with different objects that reused among different actions

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :rtype: bool
        """
        if global_section(global_context.config).getboolean('do-not-remove-web-agent', False):
            return False

        source = global_context.conn.get_source()
        return source.is_web_rpc_agent_deployed()

    def get_repeat_properties(self, global_context):
        """Get repeat properties - whether and how we should try to repeat action in case of a failure

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :rtype: parallels.core.actions.utils.repeat_properties.RepeatProperties
        """
        return RepeatProperties(
            # There could be problems removing the agent on Windows with antivirus,
            # which tries to make checks when we're removing agent files.
            # In that case agent files are not removed: we get error like:
            #   The process cannot access the file because it is being used by another process.
            # So, simply try to repeat removing of the agent 3 times with 10 seconds between attempts.
            repeat_count=3, repeat_timeout=10,
            repeat_message=messages.ACTION_REMOVE_WEB_AGENT_REPEAT,
            success_message=messages.ACTION_REMOVE_WEB_AGENT_REPEAT_SUCCESS
        )

    def run(self, global_context):
        """Run action

        :type global_context: parallels.plesk.source.web.global_context.WebGlobalMigrationContext
        :rtype: None
        """
        source = global_context.conn.get_source()
        source.web_rpc_agent.shutdown()
