from parallels.core.logging import get_logger
from parallels.core.utils.data_storage import SessionFileDataStorage, InnerDataStorage
from parallels.plesk.source.web import messages
from parallels.plesk.source.web.actions.base.site_migration import SiteMigrationAction
from parallels.plesk.source.web.utils.safe import Safe
from parallels.plesk.source.web.utils.site_info_storage import SiteInfoStorage
from parallels.plesk.source.web.utils.status_storage import StatusStorage

logger = get_logger(__name__)


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

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

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

        global_context.site_info_storage = SiteInfoStorage(
            InnerDataStorage(
                SessionFileDataStorage(
                    global_context.session_files.get_site_info_file()
                ),
                source.config.host
            )
        )
        global_context.site_info_storage.set_site_name(source.config.host)

        global_context.status_storage = StatusStorage(
            global_context.session_files.get_status_file(), source.config.host
        )
        global_context.site_objects_safe = Safe(global_context.status_storage)
