from __future__ import annotations

from ..Qt import QtWidgets, QtCore
from .._py.guidelinewidget import Ui_Form
from .draw_inputs import *
from .draw_objects import *


class DrawingsWidget(QtWidgets.QWidget, Ui_Form):

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

        self.setupUi(self)

        self.widgets = {
            'line': LineWidget(self),
            'multipts': MultiPointWidget(self),
            'text': TextWidget(self),
            'rectangle': RectangleWidget(self),
            'ellipse': EllipseWidget(self),
        }
        for w in self.widgets.values():
            self.stackedWidget.addWidget(w)

        self.graphs = None
        self._editmode = False
        self.frame.setEnabled(False)

    def update_tree(self):
        for gid, windows in self.graphs.items():
            self.graph_combobox.addItem(windows.title, userData=gid)

    @QtCore.pyqtSlot(int, name='on_graph_combobox_currentIndexChanged')
    def look_for_drawing(self, idx: int):
        self.listWidget.clear()

        graph_id = self.graph_combobox.itemData(idx)

        for k, v in self.graphs[graph_id].drawings.items():
            child = QtWidgets.QListWidgetItem(str(v))
            child.setData(QtCore.Qt.UserRole, k)
            self.listWidget.addItem(child)


    @QtCore.pyqtSlot(name='on_newButton_clicked')
    @QtCore.pyqtSlot(name='on_editButton_clicked')
    def start_drawing(self):
        self._editmode = self.sender() == self.editButton
        if self._editmode:

            graph_id, obj = self.get_item()
            arguments = obj.get_values()
            obj_type = arguments.pop('type')
            self.widgets[obj_type].set_args(**arguments)

        self.listWidget.setEnabled(False)
        self.graph_combobox.setEnabled(False)
        self.frame.setEnabled(True)

    def clear(self):
        self.graph_combobox.clear()

    @QtCore.pyqtSlot(int, name='on_mode_comboBox_currentIndexChanged')
    def change_draw_type(self, idx: int):
        self.stackedWidget.setCurrentIndex(idx)

    @QtCore.pyqtSlot(name='on_createButton_clicked')
    def make_drawing(self):
        dic = self.stackedWidget.currentWidget().collect_args()
        if dic is None:
            QtWidgets.QMessageBox.information(self, 'Not working', 'Something is missing to create this object')
            return

        graph_id, obj = self.get_item()

        if self._editmode:
            obj.set_values(**dic)

        else:
            new_obj = [LineObject, MultipointObject, TextObject, RectangleObject, EllipseObject][self.mode_comboBox.currentIndex()](**dic)

            child = QtWidgets.QListWidgetItem(str(new_obj))
            child.setData(QtCore.Qt.UserRole, new_obj.id)
            self.listWidget.addItem(child)

            self.graphs[graph_id].addDrawing(new_obj)

        self.graph_combobox.setEnabled(True)
        self.listWidget.setEnabled(True)
        self.frame.setEnabled(False)

    @QtCore.pyqtSlot(name='on_cancelbutton_clicked')
    def cancel_edit(self):
        self.graph_combobox.setEnabled(True)
        self.listWidget.setEnabled(True)
        self.frame.setEnabled(False)

    def get_item(self):
        edit_item = self.listWidget.currentItem()
        graph_id = self.graph_combobox.currentData()
        if edit_item is None:
            return graph_id, None

        object_id = edit_item.data(QtCore.Qt.UserRole)

        return graph_id, self.graphs[graph_id].drawings[object_id]
