from plotly.graph_objects import Table, Scatter
from plotly.subplots import make_subplots
from numpy import array

class Subplots(object):
    
    __subtitles = ('12 месяцев', '9 месяцев', '6 месяцев', '3 месяца')
    __title = 'Временные ряды'
     
    def __init__(self, data, x_names, rows, cols, 
                 title=__title, row_heights=None):
        
        if not row_heights: row_heights = [1 / rows] * rows
            
        self.data, self.x_names = data, x_names
        self.title, self.subtitles = title, self.__subtitles
        self.rows, self.cols = rows, cols
        
        self.fig = make_subplots(
            rows=rows, 
            cols=cols,
            subplot_titles=self.__subtitles,
            row_heights=row_heights,
            horizontal_spacing=0.1, 
            shared_xaxes=False,
            shared_yaxes=False
        )
    
    def create_traces(self, y_pred, y_fact, row, col):
        
        colors = ['rgb(144, 144, 144)', 'rgb(190, 190, 190)']
        
        index = array(list(range(0, len(y_fact))))
        
        trace_1 = Scatter(
            x=index, 
            y=y_pred, 
            mode='lines',
            line=dict(color=colors[0]), 
            name='Прогноз', 
            showlegend=False
        )

        trace_2 = Scatter(
            x=index, 
            y=y_fact, 
            mode='lines',
            line=dict(color=colors[1]), 
            name='Факт', 
            showlegend=False
        )

        self.fig.add_trace(trace_1, row=row, col=col)
        self.fig.add_trace(trace_2, row=row, col=col)
        
        return self

    def add_traces(self):
            
        trace_1, trace_2 = self.data[0], self.data[1]
        trace_3, trace_4 = self.data[2], self.data[3]
        trace_5, trace_6 = self.data[4], self.data[5]
        trace_7, trace_8 = self.data[6], self.data[7]

        self.create_traces(trace_1, trace_2, row=1, col=1)
        self.create_traces(trace_3, trace_4, row=1, col=2)
        self.create_traces(trace_5, trace_6, row=2, col=1)
        self.create_traces(trace_7, trace_8, row=2, col=2)
            
        return self
            
    def update_layout(self):
        
        mae_12 = 'MAE: ' + str(self.x_names[0])
        mae_9 = 'MAE: ' + str(self.x_names[1])
        mae_6 = 'MAE: ' + str(self.x_names[2])
        mae_3 = 'MAE: ' + str(self.x_names[3])
        
        self.fig.update_xaxes(title_text=mae_12, row=1, col=1)
        self.fig.update_xaxes(title_text=mae_9, row=1, col=2)
        self.fig.update_xaxes(title_text=mae_6, row=2, col=1)
        self.fig.update_xaxes(title_text=mae_3, row=2, col=2)
        
        self.fig.data[0].showlegend, self.fig.data[1].showlegend = True, True
        
        self.fig.update_layout(
            title={'text': self.title, 'y': 0.9975, 'x': 0.05},
            legend_orientation="h", 
            legend=dict(x=-0.045, y=1.115),
            margin={'t': 60},
            height=520, 
            width=950
        )
        return self
            
    def plot(self, anaconda=False):
        
        self.add_traces()
        
        if not anaconda:
            self.update_layout()
        else:
            margin_bottom = 40
            self.update_layout()
            self.fig.layout.margin = {'t': 60, 'b': margin_bottom}
        
        config = {'displayModeBar': False}
        return self.fig.show(config=config)
    
    def to_json(self):
        
        self.add_traces()
        self.update_layout()
        
        return self.fig.to_json()
from plotly.graph_objects import Table, Scatter
from plotly.subplots import make_subplots
from numpy import array

class Subplots(object):
    
    __subtitles = ('12 месяцев', '9 месяцев', '6 месяцев', '3 месяца')
    __title = 'Временные ряды'
     
    def __init__(self, data, x_names, rows, cols, 
                 title=__title, row_heights=None):
        
        if not row_heights: row_heights = [1 / rows] * rows
            
        self.data, self.x_names = data, x_names
        self.title, self.subtitles = title, self.__subtitles
        self.rows, self.cols = rows, cols
        
        self.fig = make_subplots(
            rows=rows, 
            cols=cols,
            subplot_titles=self.__subtitles,
            row_heights=row_heights,
            horizontal_spacing=0.1, 
            shared_xaxes=False,
            shared_yaxes=False
        )
    
    def create_traces(self, y_pred, y_fact, row, col):
        
        colors = ['rgb(144, 144, 144)', 'rgb(190, 190, 190)']
        
        index = array(list(range(0, len(y_fact))))
        
        trace_1 = Scatter(
            x=index, 
            y=y_pred, 
            mode='lines',
            line=dict(color=colors[0]), 
            name='Прогноз', 
            showlegend=False
        )

        trace_2 = Scatter(
            x=index, 
            y=y_fact, 
            mode='lines',
            line=dict(color=colors[1]), 
            name='Факт', 
            showlegend=False
        )

        self.fig.add_trace(trace_1, row=row, col=col)
        self.fig.add_trace(trace_2, row=row, col=col)
        
        return self

    def add_traces(self):
            
        trace_1, trace_2 = self.data[0], self.data[1]
        trace_3, trace_4 = self.data[2], self.data[3]
        trace_5, trace_6 = self.data[4], self.data[5]
        trace_7, trace_8 = self.data[6], self.data[7]

        self.create_traces(trace_1, trace_2, row=1, col=1)
        self.create_traces(trace_3, trace_4, row=1, col=2)
        self.create_traces(trace_5, trace_6, row=2, col=1)
        self.create_traces(trace_7, trace_8, row=2, col=2)
            
        return self
            
    def update_layout(self):
        
        mae_12 = 'MAE: ' + str(self.x_names[0])
        mae_9 = 'MAE: ' + str(self.x_names[1])
        mae_6 = 'MAE: ' + str(self.x_names[2])
        mae_3 = 'MAE: ' + str(self.x_names[3])
        
        self.fig.update_xaxes(title_text=mae_12, row=1, col=1)
        self.fig.update_xaxes(title_text=mae_9, row=1, col=2)
        self.fig.update_xaxes(title_text=mae_6, row=2, col=1)
        self.fig.update_xaxes(title_text=mae_3, row=2, col=2)
        
        self.fig.data[0].showlegend, self.fig.data[1].showlegend = True, True
        
        self.fig.update_layout(
            title={'text': self.title, 'y': 0.9975, 'x': 0.05},
            legend_orientation="h", 
            legend=dict(x=-0.045, y=1.115),
            margin={'t': 60},
            height=520, 
            width=950
        )
        return self
            
    def plot(self, anaconda=False):
        
        self.add_traces()
        
        if not anaconda:
            self.update_layout()
        else:
            margin_bottom = 40
            self.update_layout()
            self.fig.layout.margin = {'t': 60, 'b': margin_bottom}
        
        config = {'displayModeBar': False}
        return self.fig.show(config=config)
    
    def to_json(self):
        
        self.add_traces()
        self.update_layout()
        
        return self.fig.to_json()