"""
Module for formatting standard output text.

Useful specifically for making things stand out when
printing errors and information to the console.

Note
----
This is a utility that is not to be used directly by the user and is
instead meant to contain functions that are called programmatically.

Returns
-------
Returns formatted text, printing it to the console.

Examples
--------
Here is a simple example of usage::

    from thesis.utils.textformatter import TextFormatter

    TextFormatter.print_title("THIS IS A TITLE")

"""
from __future__ import absolute_import, division, print_function
from termcolor import colored

CHECK = u'\u2713'
X_MARK = u'\u2717'

class TextFormatter(object):
    """
    Text formatter for displaying different kinds of formatted text.
    """

    @staticmethod
    def color_text(text, color, text_attributes=None):
        """
        Returns text with defined color and text_attributes.
        """
        if text_attributes is None:
            return colored(text, color, attrs=[])
        else:
            return colored(text, color, attrs=text_attributes)

    @staticmethod
    def print_title(title_text):
        """
        Prints a blue, bold title.
        """
        title = colored(title_text, "blue", attrs=['bold'])
        TextFormatter.print_new_line()
        TextFormatter.print_box(title, "=")

    @staticmethod
    def print_heading(heading_text):
        """
        Prints a green, bold heading.
        """

        heading = colored(heading_text, "green", attrs=['bold'])
        TextFormatter.print_new_line()
        TextFormatter.print_box(heading)
        TextFormatter.print_new_line()

    @staticmethod
    def print_error(msg):
        """
        Prints error message.
        """
        error_msg = colored("[ERROR] %s" % msg, "red", attrs=['bold'])
        print(error_msg)

    @staticmethod
    def print_status(msg):
        """
        Prints status message.
        """
        status_msg = colored(msg, "green", attrs=['concealed'])
        print(status_msg)

    @staticmethod
    def print_info(msg):
        """
        Prints information message.
        """
        info_msg = colored("[INFO] %s" % msg, "white", attrs=['bold'])
        print(info_msg)

    @staticmethod
    def print_option(num, msg):
        """
        Prints option message.
        """
        prompt = "{0}) {1}".format(num, msg)
        formatted_option = colored(prompt, "white", attrs=['bold'])
        print(formatted_option)

    @staticmethod
    def print_pair(key, val):
        """
        Prints a key value pair.
        """
        adjusted_key = ("{0}:".format(key)).ljust(15)
        formatted_key = colored(adjusted_key, "cyan", attrs=['bold'])
        formatted_val = colored(val, "magenta", attrs=['bold'])
        print("{0}{1}".format(formatted_key, formatted_val))

    @staticmethod
    def print_spacer(length=30, character_type="-"):
        """
        Prints separator line.
        """
        print(character_type * length)

    @staticmethod
    def print_new_line():
        """
        Prints new line.
        """
        print("")

    @staticmethod
    def print_box(msg, character_type="-"):
        """
        Prints box with defined msg in the middle of the box.
        """
        msg_length = len(msg) - 9
        TextFormatter.print_spacer(msg_length, character_type)
        print("* %s *" % msg)
        TextFormatter.print_spacer(msg_length, character_type)

    @staticmethod
    def get_check():
        """
        Returns check-mark symbol.
        """
        return colored(CHECK.encode('utf-8'), "green", attrs=['bold'])

    @staticmethod
    def get_xmark():
        """
        Returns x-mark symbol.
        """
        return colored(X_MARK.encode('utf-8'), "red", attrs=['bold'])

    @staticmethod
    def get_input_msg(msg):
        """
        Returns message prompt for user input.
        """
        return colored("\n%s" % msg, "yellow", attrs=['bold'])
