class Quick1DPlot(QuickDataPlot):

    def __init__(self, ap, **kwargs):

        super().__init__(ap, **kwargs)
        self.nplots = 0

        self.table.setColumnCount(5)
        self.table.setRowCount(1)
        self.table.setColumnWidth(0, 200)
        self.table.setColumnWidth(1, 200)
        self.table.setColumnWidth(2, 30)
        self.table.setColumnWidth(3, 30)
        self.table.setColumnWidth(4, 40)
        self.table.setHorizontalHeaderLabels(['xvalue', 'yvalue', 'color', 'show', 'scatter'])

        self.combos = []
        self.curves = []
        self.show_cbs = []
        self.scatter_cbs = []

        self.mk_buttons()

    def mk_buttons(self):
        self.bt_add_plot = QPushButton('Add plot', self)
        self.bt_add_plot.clicked.connect(self.add_plot)
        self.table.setCellWidget(self.nplots, 0, self.bt_add_plot)

        self.bt_update = QPushButton('Update', self)
        self.bt_update.clicked.connect(self.update_from_h5)
        self.table.setCellWidget(self.nplots, 1, self.bt_update)

    def add_plot(self):
        self.nplots += 1
        self.table.setRowCount(self.nplots + 1)
        combox = NumericDataCombo(self.ap.df)
        comboy = NumericDataCombo(self.ap.df)

        self.combos += [[combox, comboy]]
        self.table.setCellWidget(self.nplots - 1, 0, combox)
        self.table.setCellWidget(self.nplots - 1, 1, comboy)

        self.table.setItem(self.nplots - 1, 2, QtGui.QTableWidgetItem())
        self.table.item(self.nplots - 1, 2).setBackground(QtGui.QColor(*color_palette[self.nplots - 1]))

        # self.table.setFixedSize(self.table.sizeHint())

        self.curves += [
            self.plot.plot(pen=pg.mkPen(color=color_palette[self.nplots - 1], width=1.5), symbol='x', symbolPen=None,
                           symbolBrush=None)]

        self.show_cbs += [QCheckBox()]
        self.show_cbs[self.nplots - 1].setChecked(True)
        self.table.setCellWidget(self.nplots - 1, 3, self.show_cbs[self.nplots - 1])
        self.show_cbs[self.nplots - 1].stateChanged.connect(self.update_shows)

        self.scatter_cbs += [QCheckBox()]
        self.scatter_cbs[self.nplots - 1].setChecked(False)
        self.table.setCellWidget(self.nplots - 1, 4, self.scatter_cbs[self.nplots - 1])
        self.scatter_cbs[self.nplots - 1].stateChanged.connect(self.update_scatters)

        self.mk_buttons()

    def update_shows(self):
        for k, cb in enumerate(self.show_cbs):
            if cb.isChecked():
                self.curves[k].show()
            else:
                self.curves[k].hide()

    def update_scatters(self):
        for k, cb in enumerate(self.scatter_cbs):
            pen = pg.mkPen(color=color_palette[k], width=1.5)
            brush = pg.mkBrush(color=color_palette[k])
            if cb.isChecked():
                self.curves[k].setSymbolBrush(brush)
                self.curves[k].setPen(None)
            else:
                self.curves[k].setSymbolBrush(None)
                self.curves[k].setPen(pen)

    def update_data_extractor(self):

        idxxs = [combo[0].get_idx() for combo in self.combos]
        idxys = [combo[1].get_idx() for combo in self.combos]

        for idxx in idxxs:
            if idxx not in self.data_extractor.data_extractors:
                if idxx[0] == 'shot number':
                    self.data_extractor[idxx] = EmptyDataExtractor()
                else:
                    self.data_extractor[idxx] = SingleDataExtractor(idxx)

        for idxy in idxys:
            if idxy not in self.data_extractor.data_extractors:
                if idxy[0] == 'shot number':
                    self.data_extractor[idxy] = EmptyDataExtractor()
                else:
                    self.data_extractor[idxy] = SingleDataExtractor(idxy)

        self.data_extractor.clean_children(idxxs + idxys)
        self.data_extractor.clean_memory(self.ap.h5_paths)

    def update(self, data=None):

        Xs = np.zeros((self.nplots, len(self.ap.h5_paths_selected)))
        Ys = np.zeros((self.nplots, len(self.ap.h5_paths_selected)))

        for i, h5_path in enumerate(self.ap.h5_paths_selected):

            data = self.data_extractor.get_data(h5_path)[0]

            for k in range(self.nplots):
                idxx = self.combos[k][0].get_idx()
                idxy = self.combos[k][1].get_idx()

                if idxx[0] == 'shot number':
                    Xs[k, i] = self.ap.shotselector.get_selected_indices()[0][i]
                else:
                    Xs[k, i] = data[idxx]

                if idxy[0] == 'shot number':
                    Ys[k, i] = self.ap.shotselector.get_selected_indices()[0][i]
                else:
                    Ys[k, i] = data[idxy]

        for k in range(self.nplots):
            self.curves[k].setData(Xs[k], Ys[k])