# Copyright (C) 2014 SignalFuse, Inc.
# Copyright (C) 2015 SignalFx, Inc.
#
# Docker container orchestration utility.

import getpass
import json
import requests

from . import exceptions
from .version import name as maestro_name


class BaseAuditor:
    """Base class for auditors that can save or notify about orchestration
    plays being executed."""

    def _format_what(self, what):
        if type(what) == list or type(what) == tuple:
            return ', '.join(what)
        return what

    def _format_who(self, who=None):
        return who or getpass.getuser()

    def _format_action(self, what, action=None, who=None):
        what = self._format_what(what)
        who = self._format_who(who)

        if action:
            action = action if action is not 'stop' else 'stopp'
            return '{} is {}ing {}.'.format(who, action, what)
        return '{} is acting on {}.'.format(who, what)

    def _format_success(self, what, action=None):
        what = self._format_what(what)

        if action:
            return '{} of {} succeeded.'.format(action.title(), what)
        return 'Action on {} succeeded.'.format(what)

    def _format_error(self, what, action=None, message=None):
        what = self._format_what(what)

        if action:
            s = 'Failed to {} {}!'.format(action, what)
        else:
            s = 'Failed action on {}!'.format(what)

        if message:
            s = '{} (message: {})'.format(s, message)
        return s

    def action(self, what, action=None, who=None):