from parallels.core import safe_format
from parallels.plesk.source.web import messages
from parallels.plesk.source.web.utils.status_storage import StatusStorage


class Tasks(object):
    """Available tasks for migration list
    IMPORT - import and sync operations
    CHECK - check applications
    """
    IMPORT = 'import'
    CHECK = 'check'

    TASKS = [IMPORT, CHECK]

    @staticmethod
    def get_command(task):
        """Returns migration cli command action for task defined im migration list

        :type task: str | unicode | None
        :rtype: str | unicode
        """
        if task == Tasks.IMPORT:
            return 'transfer-site'
        if task == Tasks.CHECK:
            return 'check-apps'

        # Task was not defined in migration list and there was only 'transfer-site' command
        # in old Site Import versions. To keep compatibility return 'transfer-site' if no task.
        return 'transfer-site'

    @staticmethod
    def get_storage_status_type(task):
        """Returns field key in status storage for task defined im migration list
        We store statuses for different tasks in different fields in status storage.

        :type task: str | unicode | None
        :rtype: str | unicode
        """
        if task == Tasks.IMPORT:
            return StatusStorage.STORAGE_STATUS_TYPE_COMMON_STATUS
        if task == Tasks.CHECK:
            return StatusStorage.STORAGE_STATUS_TYPE_CHECK_STATUS

        # Task was not defined in migration list and there was only import status
        # in old Site Import versions. To keep compatibility return import if no task.
        return StatusStorage.STORAGE_STATUS_TYPE_COMMON_STATUS

    @staticmethod
    def check_migration_list_task(task):
        """Validate task defined in migration list

        :type task: str | unicode | None
        :rtype:
        """
        if task in Tasks.TASKS:
            return task

        if task is None:
            return Tasks.IMPORT

        raise Exception(safe_format(
            messages.UNKNOWN_TASK_IN_MIGRATION_LIST,
            task=task, available_tasks=', '.join(Tasks.TASKS)
        ))
