# coding: utf-8
import pandas as pd

from fireant import utils


class WidgetGroupManager(object):
    def __init__(self, widget_group):
        self.widget_group = widget_group

    def render(self, dimensions=None, metric_filters=None, dimension_filters=None, references=None, operations=None):
        combined_dimensions = utils.filter_duplicates(self.widget_group.dimensions + (dimensions or []))
        combined_references = utils.filter_duplicates(self.widget_group.references + (references or []))
        combined_operations = utils.filter_duplicates(self.widget_group.operations + (operations or []))

        dataframe = self.widget_group.slicer.manager.data(
            metrics=[metric
                     for widget in self.widget_group.widgets
                     for metric in widget.metrics],
            dimensions=combined_dimensions,
            metric_filters=metric_filters or [],
            dimension_filters=self.widget_group.dimension_filters + (dimension_filters or []),
            references=combined_references,
            operations=combined_operations,
        )

        return list(self._transform_widgets(self.widget_group.widgets, dataframe,
                                            combined_dimensions, combined_references))

    def _transform_widgets(self, widgets, dataframe, dimensions, references):
        for widget in widgets:
            display_schema = self.widget_group.slicer.manager.display_schema(
                metrics=widget.metrics,
                dimensions=dimensions,
                references=references,
            )

            if references:
                # This escapes a pandas bug where a data frame subset of columns still returns the columns of the
                # original data frame
                reference_keys = [''] + [ref.key for ref in references]
                subset_columns = pd.MultiIndex.from_product([reference_keys, widget.metrics])
                subset = pd.DataFrame(dataframe[subset_columns], columns=subset_columns)

            else:
                subset = dataframe[widget.metrics]

            yield widget.transformer.transform(subset, display_schema)

@@ -785,24 +785,38 @@
 r metric in 
+utils.flatten(
 widget.metri
@@ -817,16 +817,17 @@
 .metrics
+)
 %5D,%0A     
@@ -1926,16 +1926,30 @@
 e_keys, 
+utils.flatten(
 widget.m
@@ -1954,16 +1954,17 @@
 .metrics
+)
 %5D)%0A     
@@ -2101,16 +2101,30 @@
 taframe%5B
+utils.flatten(
 widget.m
@@ -2129,16 +2129,17 @@
 .metrics
+)
 %5D%0A%0A     

Fix exceptions in WidgetGroupManager due to 2d metrics list