from __future__ import print_function
from configparser import ConfigParser
from ansible_bundle import worker
import sys
import yaml

from os.path import join, isfile
from os import getcwd, getenv

QUIET = 0
VERBOSE = 1
DEBUG = 2
DRY = False
CLEAN = False
RUN = False
OK = 0
ERROR = 1

DOTS = '\n-------------------'

HOME = getenv('HOME')
WORKDIR = getcwd()
DEFAULT_SCM_URL = 'https://github.com'

# Color + decoration for messages printed on terminal


COLORS = {
    'red': '\033[91m',
    'green': '\033[92m',
    'yellow': '\033[93m',
    'blue': '\033[94m',
    'magenta' : '\033[95m',
}

DECORATIONS = {
    'bold': '\033[1m',
    'underline': '\033[4m',
}

MESSAGES = {
    'none': (None, None),
    'ok': ('green', 'bold'),
    'info': ('blue', None),
    'warning': ('yellow', None),
    'error': ('red', 'bold'),
    'debug': ('magenta', 'bold')
}

END = '\033[0m'

def load_cfg(filename):
    try:
        config = ConfigParser()
        config.read(filename)
        return config
    except:
        sys.exit('Cannot load %s' %filename)


def text(s, **kwargs):
    msg = ''
    color = kwargs.get('color', None)
    decoration = kwargs.get('decoration', None)
    if decoration is not None:
        msg += DECORATIONS[decoration]
    if color is not None:
        msg += COLORS[color]
    msg += s
    if color or decoration:
        msg += END
    return (msg)


class Config:
    SCM = 'git'
    SCM_VERSION = 'master'
    scm_prefix = DEFAULT_SCM_URL
    scm_roles = ''
    scm_modules = ''
    verbosity = QUIET
    dry = False
    colorize = True
    workers = 1
    pool = None

    def __init__(self):
        pass

    def initialize(self, **kwargs):
        cfg = self.load()
        if cfg and cfg.has_section('bundle'):
            self.setvalues(cfg)

        for name, value in kwargs.items():
            if value: setattr(self, name, value)

        for string in ('workers', 'verbosity'):
            setattr(self, string, int(getattr(self, string)))


    def load(self):
        LOAD_ORDER = (
            join(WORKDIR, 'ansible.cfg'),
            join(HOME, '.ansible.cfg'),
            join('etc', 'ansible', 'ansible.cfg')
        )
        if getenv('ANSIBLE_CONFIG') is not None:
            return load_cfg(os.getenv('ANSIBLE_CONFIG'))
        else:
            for filename in LOAD_ORDER:
                if isfile(filename):
                    return load_cfg(filename)
        return None


    def setvalues(self, cfg):
        for key, value in cfg.items('bundle'):
            setattr(self, key, value)

        if self.scm_prefix:
            self.scm_roles = self.scm_prefix + self.scm_roles
            self.scm_modules = self.scm_prefix + self.scm_modules
        self.pool = worker.ThreadPool(int(self.workers))
