from parallels.core.migrator import Migrator as CommonMigrator
from parallels.core.utils.common import cached
from parallels.core.utils.locks.session_file_lock import SessionFileLock
from parallels.plesk.source.web.connections import WebConnections
from parallels.plesk.source.web.global_context import WebGlobalMigrationContext
from parallels.plesk.source.web.migrated_subscription import WebMigratedSubscription
from parallels.plesk.source.web.session_files import WebSessionFiles


class Migrator(CommonMigrator):
    """
    :type global_context: parallels.plesk.source.web.global_context.WebGlobalMigrationContext
    """
    def __init__(self, config, options):
        self.global_context = None
        super(Migrator, self).__init__(config, options)
        self.global_context.site_migration_lock = SessionFileLock('site-migration.lock')

    def _load_connections_configuration(self, global_context, target_panel_type):
        return WebConnections(global_context, self._get_target_panel_by_name(target_panel_type))

    def _create_global_context(self):
        """Create global context object

        :rtype: parallels.plesk.source.web.global_context.WebGlobalMigrationContext
        """
        return WebGlobalMigrationContext()

    def _create_session_files(self):
        """Create session files object

        :rtype: parallels.plesk.source.web.session_files.WebSessionFiles
        """
        return WebSessionFiles(self.global_context.conn, self._get_migrator_server())

    @cached
    def _create_migrated_subscription(self, name):
        return WebMigratedSubscription(self, name)
