import sys

import termcolor

from anvil import env
from anvil import type_utils as tu

COLORS = termcolor.COLORS.keys()

LOG_COLOR = True
if 'LOG_COLOR' in env.get():
    LOG_COLOR = tu.make_bool(env.get_key('LOG_COLOR'))
if not sys.stdout.isatty():
    LOG_COLOR = False


def color_enabled():
    return LOG_COLOR


def quote(data, quote_color='green', **kargs):
    if not color_enabled():
        return "'%s'" % (data)
    else:
        text = str(data)
        if len(text) == 0:
            text = "''"
        return color(text, quote_color, **kargs)


def color(data, color_to_be, bold=False, underline=False, blink=False):
    text = str(data)
    text_attrs = list()
    if bold:
        text_attrs.append('bold')
    if underline:
        text_attrs.append('underline')
    if blink:
        text_attrs.append('blink')
    if color_enabled() and color_to_be in COLORS:
        return termcolor.colored(text, color_to_be, attrs=text_attrs)
    else:
        return text
