from ..Qt import QtWidgets, QtCore
from .._py.smoothdialog import Ui_SmoothDialog


class QSmooth(QtWidgets.QDialog, Ui_SmoothDialog):
    newValues = QtCore.pyqtSignal(int, dict)

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setupUi(self)
        self.on_comboBox_currentIndexChanged(0)

    @QtCore.pyqtSlot(int, name='on_comboBox_currentIndexChanged')
    def change_mode(self, idx: int):
        if idx == 1:
            self.widget.show()
            self.widget_2.hide()
        elif idx == 2:
            self.widget.show()
            self.widget_2.show()
        else:
            self.widget.hide()
            self.widget_2.hide()

    def accept(self):
        para = {'logx': self.x_checkBox.isChecked(),
                'logy': self.y_checkBox.isChecked()}
        npoints = self.frac_spinBox.value()
        idx = self.comboBox.currentIndex()

        # this order must match the combobox
        para['mode'] = [
            'mean',
            'savgol',
            'loess',
            'median',
            'std',
            'var',
            'max',
            'min',
            'sum',
        ][idx]

        # Savitzky-Golay needs also polynomial degree
        if idx == 1:
            para['deg'] = self.polynom_spinBox.value()

        # LOESS needs also polynomial degree and number of iterations
        if idx == 2:
            para['deg'] = self.polynom_spinBox.value()
            para['it'] = self.iter_spinBox.value()

        self.newValues.emit(npoints, para)

        super().accept()

    def close(self):
        self.disconnect()
        super().close()
