from parallels.core.actions.utils.logging_properties import LoggingProperties
from parallels.plesk.source.web.statistics import StatisticsGetSiteInfo
from parallels.core.utils.migrator_utils import get_version
from parallels.plesk.source.web import messages
from parallels.plesk.source.web.actions.base.site_migration import SiteMigrationAction


class StartGetSiteInfoStatistics(SiteMigrationAction):
    """Initially write statistics once 'get-site-info' command is started
    """

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

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

    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_START_GET_SITE_INFO_STATISTICS

    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
        """
        conn_source_config = global_context.conn.get_source().config
        global_context.statistics_get_site_info = StatisticsGetSiteInfo(
            start_time=int(global_context.start_time),
            domain_id=global_context.site_info_storage.get_target_domain_id(),
            migrator_version=get_version(),
            advanced_screen_base_url=conn_source_config.base_url_explicit,
            advanced_screen_document_root=conn_source_config.document_root_explicit
        )
        global_context.statistics_get_site_info.write()
