from src.nmreval.math import apodization
from src.nmreval.lib.importer import find_models
from src.nmreval.utils.text import convert

from ...Qt import QtCore, QtWidgets, QtGui
from ...lib.forms import FormWidget
from ..._py.editsignalwidget import Ui_Form


class EditSignalWidget(QtWidgets.QWidget, Ui_Form):
    do_something = QtCore.pyqtSignal(str, tuple)
    get_values = QtCore.pyqtSignal()
    preview_triggered = QtCore.pyqtSignal(str)

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

        self.apodlist = find_models(apodization)

        self.lineEdit.hide()
        self.lineEdit.setValidator(QtGui.QDoubleValidator())

        for ap in self.apodlist:
            self.apodcombobox.addItem(str(ap().name))
            self.change_apodization(0)

        self.baselinebutton.clicked.connect(lambda: self.apply_changes('bl'))
        self.zfbutton.clicked.connect(lambda: self.apply_changes('zf'))
        self.phasebutton.clicked.connect(lambda: self.apply_changes('ph'))
        self.apodbutton.clicked.connect(lambda: self.apply_changes('ap'))
        self.leftshiftbutton.clicked.connect(lambda: self.apply_changes('ls'))
        self.fourierutton.clicked.connect(lambda: self.apply_changes('ft'))

        self.pushButton.clicked.connect(lambda: self.preview_triggered.emit('ap'))
        self.pushButton_2.clicked.connect(lambda: self.preview_triggered.emit('ph'))

    @QtCore.pyqtSlot(str)
    def apply_changes(self, sender):
        if sender in ['bl', 'zf', 'ft']:
            self.do_something.emit(sender, tuple())

        elif sender == 'ls':
            if self.comboBox.currentIndex() == 0:
                _nop = int(self.lsspinBox.text())
                stype = 'pts'
            else:
                try:
                    _nop = float(self.lineEdit.text())
                except ValueError:
                    _nop = 0.0
                stype = 'time'
            self.do_something.emit(sender, (_nop, stype))

        elif sender == 'ap':
            apodmodel = self.apodlist[self.apodcombobox.currentIndex()]
            p = [float(x.text()) for x in self.groupBox_3.findChildren(QtWidgets.QLineEdit)]
            self.do_something.emit(sender, (p, apodmodel))

        elif sender == 'ph':
            ph0 = float(self.ph0slider.value())
            ph1 = float(self.ph1slider.value())
            pvt = float(self.pivot_lineedit.text())
            self.do_something.emit(sender, (ph0, ph1, pvt))

        else:
            print('You should never reach this by accident.')

    @QtCore.pyqtSlot(int, name='on_apodcombobox_currentIndexChanged')
    def change_apodization(self, index):
        apod_func = self.apodlist[index]
        self.label_2.setText(convert(apod_func.equation))

        while self.verticalLayout_8.count():
            item = self.verticalLayout_8.takeAt(0)
            try:
                item.widget().deleteLater()
            except AttributeError:
                pass

        for k, v in enumerate(apod_func.params):
            widgt = FormWidget(name=v)
            self.verticalLayout_8.addWidget(widgt)

    @QtCore.pyqtSlot(int, name='on_comboBox_currentIndexChanged')
    def change_ls(self, idx):
        if idx:
            self.lineEdit.show()
            self.lsspinBox.hide()
        else:
            self.lineEdit.hide()
            self.lsspinBox.show()
