import matplotlib.pyplot as plt


class Chart:
    """
    Chart class to create and format a basic pyplot figure
    """
    def __init__(self, title=None, xlabel=None, ylabel=None, figsize=None):

        self.title = title if title else "Unnamed Chart"
        self.xlabel = xlabel if xlabel else "X-Axis"
        self.ylabel = ylabel if ylabel else "Y-Axis"
        self.figsize = figsize if figsize else (10, 8)

        # Create figure
        self.figure, self.ax = self.create_figure(self.figsize)

        # Format
        self.format_labels()
        self.format_axes()

    def create_figure(self, figsize):
        """
        Create plplot figure and axes objects and assign to Chart
        :param figsize:
        :return:
        """
        self.figure, self.ax = plt.subplots(1, 1, figsize=figsize)

        return self.figure, self.ax

    def format_labels(self, color="black"):
        """
        Format title, x label, and y label
        :return:
        """
        self.ax.set_title(self.title, color=color, fontsize=14)
        self.ax.set_xlabel(self.xlabel, color=color)
        self.ax.set_ylabel(self.ylabel, color=color)

    def format_axes(self, color="darkgray"):
        """
        Format axes to my preference.  Remove top/right spines and set colors on
        left/bottom spines, ticks, and tick labels
        :param color:
        :return:
        """

        # Turn off top / right spines
        self.ax.spines["top"].set_visible(False)
        self.ax.spines["right"].set_visible(False)

        # Format left / bottom spines
        self.ax.spines["left"].set_color(color)
        self.ax.spines["bottom"].set_color(color)

        # Format ticks
        self.ax.tick_params(axis="x", colors=color)
        self.ax.tick_params(axis="y", colors=color)

        # Format tick labels
        self.ax.xaxis.label.set_color(color)
        self.ax.yaxis.label.set_color(color)

    @staticmethod
    def show():
        """
        Show chart
        :return:
        """
        plt.show()

    @staticmethod
    def save_figure(*args, **kwargs):
        """
        Save figure to file
        :param args:
        :param kwargs:
        :return:
        """
        plt.savefig(*args, **kwargs)
