from numpy import linspace, logspace, log10

from ..Qt import QtWidgets, QtCore
from .._py.interpol_dialog import Ui_Dialog


class QInterpol(QtWidgets.QDialog, Ui_Dialog):
    ready = QtCore.pyqtSignal(dict)

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

        self.setupUi(self)

        self.on_comboBox_2_currentIndexChanged(0)

    @QtCore.pyqtSlot(int)
    def on_comboBox_2_currentIndexChanged(self, idx: int):
        if idx == 0:
            self.frame.hide()
            self.frame_2.show()
        else:
            self.frame.show()
            self.frame_2.hide()

    def accept(self):
        ret_dic = {'kind': self.comboBox.currentIndex(), 'ylog': self.checkBox_2.isChecked()}
        if self.comboBox_2.currentIndex() == 0:
            ret_dic['xaxis'] = int(self.lineEdit.text())
        else:
            if self.checkBox.isChecked():
                ret_dic['xaxis'] = logspace(log10(float(self.lineEdit_2.text())),
                                            log10(float(self.lineEdit_3.text())),
                                            num=int(self.lineEdit_4.text()))
            else:
                ret_dic['xaxis'] = linspace(float(self.lineEdit_2.text()),
                                            float(self.lineEdit_3.text()),
                                            num=int(self.lineEdit_4.text()))
        ret_dic['idx'] = [i.row() for i in self.listWidget.selectedIndexes()]
        self.ready.emit(ret_dic)
        self.close()
