from parallels.core.migrator import Migrator as CommonMigrator
from parallels.core.utils.migrator_utils import get_optional_option_value
from parallels.plesk.source.mail.connections import MailConnections
from parallels.plesk.source.mail.global_context import MailGlobalMigrationContext
from parallels.plesk.source.mail.session_files import MailSessionFiles
from parallels.plesk.source.mail.settings_storage import SettingsStorage
from parallels.plesk.source.mail.status_storage import StatusStorage


class Migrator(CommonMigrator):
    """
    :type global_context: parallels.plesk.source.mail.global_context.MailGlobalMigrationContext
    """
    def __init__(self, config, options):
        self.global_context = None
        super(Migrator, self).__init__(config, options)

        task_status_file_path = get_optional_option_value(
            self.global_context.options, 'task_status', None
        )
        self.global_context.status_storage = StatusStorage(
            self.global_context.session_files.get_global_status_file().path,
            self.global_context.session_files.get_global_status_file_lock().path,
            task_status_file_path
        )
        self.global_context.settings_storage = SettingsStorage(
            self.global_context.session_files.get_settings_file(),
            self.global_context.session_files.get_settings_file_lock()
        )

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

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

        :rtype: parallels.plesk.source.mail.global_context.MailGlobalMigrationContext
        """
        return MailGlobalMigrationContext()

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

        :rtype: parallels.plesk.source.mail.session_files.MailSessionFiles
        """
        return MailSessionFiles(
            self.global_context.conn, self._get_migrator_server(),
            task_id=get_optional_option_value(
                self.global_context.options, 'task_id', None
            )
        )
