from ..Qt import QtWidgets, QtCore, QtGui
from .._py.integratederive_dialog import Ui_Dialog


class QDeriveIntegrate(QtWidgets.QDialog, Ui_Dialog):
    newValues = QtCore.pyqtSignal(int, dict)

    def __init__(self, mode: str, parent=None):
        super().__init__(parent=parent)
        self.setupUi(self)

        self.mode = mode.lower()[0]

        if self.mode == 'i':
            self.setWindowTitle('Integration dialog')
            self.start_lineedit.setValidator(QtGui.QDoubleValidator())
            self.stop_lineedit.setValidator(QtGui.QDoubleValidator())
            self.ft_comboBox.hide()
            self.freq_box.hide()

        elif self.mode == 'd':
            self.setWindowTitle('Differentiation dialog')
            self.widget.hide()
            self.ft_comboBox.hide()
            self.freq_box.hide()

        elif self.mode == 'l':
            self.setWindowTitle('Logarithmic FT dialog')
            self.log_checkbox.hide()
            self.widget.hide()
            self.freq_box.show()

        else:
            raise ValueError(f'Unknown mode {mode}, use "d", "i", or "l".')

    def add_graphs(self, graphs: list):
        self.graph_combobox.clear()

        for (graph_id, graph_name) in graphs:
            self.graph_combobox.addItem(graph_name, userData=graph_id)

    def set_sets(self, sets: list):
        self.listWidget.clear()
        for (set_id, set_name) in sets:
            item = QtWidgets.QListWidgetItem(set_name)
            item.setData(QtCore.Qt.UserRole, set_id)
            item.setCheckState(QtCore.Qt.Checked)
            self.listWidget.addItem(item)

    @QtCore.pyqtSlot(int, name='on_checkBox_2_stateChanged')
    def enable_graph(self, state: int):
        self.graph_combobox.setEnabled(state != QtCore.Qt.Checked)

    @QtCore.pyqtSlot(int, name='on_range_checkbox_stateChanged')
    def enable_limits(self, full_range: int):
        self.start_lineedit.setEnabled(full_range != QtCore.Qt.Checked)
        self.stop_lineedit.setEnabled(full_range != QtCore.Qt.Checked)

    def get_options(self):
        opts = {
            'graph': '' if self.newgraph_checkbox.isChecked() else self.graph_combobox.currentData(),
            'mode': self.mode, 'sets': []
        }

        if self.mode == 'i':
            start = None
            stop = None

            if not self.range_checkbox.isChecked():
                start = self.start_lineedit.text()
                stop = self.stop_lineedit.text()

            if start:
                start = float(start)

            if stop:
                stop = float(stop)

            opts['limits'] = (start, stop)

        if self.mode == 'l':
            opts['ft_mode'] = ['cos', 'sin', 'complex'][self.ft_comboBox.currentIndex()]
            opts['return_f'] = self.freq_box.isChecked()
        else:
            opts['log'] = self.log_checkbox.isChecked()

        for i in range(self.listWidget.count()):
            item = self.listWidget.item(i)
            if item.checkState() == QtCore.Qt.Checked:
                opts['sets'].append(item.data(QtCore.Qt.UserRole))

        return opts
