from parallels.plesk.source.web.utils.status_storage import StatusStorage


def set_status_for_all_migration_list_objects(site_migration_list, status_storage, status, status_type=None):
    """
    :type site_migration_list: parallels.plesk.source.web.site_migration_list.SiteMigrationList
    :type status_storage: parallels.plesk.source.web.utils.status_storage.StatusStorage
    :type status: str | unicode
    :type status_type: str | unicode | None
    :rtype: None
    """
    applications = site_migration_list.filter_applications
    if applications is not None:
        for application in applications:
            status_storage.set_application_status(
                application.application_type, application.source_path, application.target_path,
                status, status_type
            )

    files = site_migration_list.include_files
    if files is not None:
        for file_info in files:
            status_storage.set_files_status(
                file_info.source_path, file_info.target_path, status
            )

    databases = site_migration_list.include_databases
    if databases is not None:
        for database in databases:
            status_storage.set_database_status(
                database.name, database.host, database.port,
                database.username, database.password, status
            )


def get_status_value(global_context, object_safe):
    """
    :type global_context: parallels.plesk.source.web.global_context.WebGlobalMigrationContext
    :type object_safe: parallels.core.utils.safe_object.SafeObject
    :rtype: str | unicode
    """
    if global_context.stop_mark.is_set():
        return StatusStorage.STATUS_CANCELLED
    elif object_safe.has_errors:
        return StatusStorage.STATUS_FAILURE
    elif object_safe.has_warnings:
        return StatusStorage.STATUS_WARNING
    else:
        return StatusStorage.STATUS_SUCCESS
