from __future__ import absolute_import
from glm.Qtpy import Qt
from glm.Qtpy.Qt import QtGui, QtCore, QtWidgets
#from . import nodz_main

class QtPopupLineEditWidget(QtWidgets.QLineEdit):

    @staticmethod
    def defaultNodeCreator(nodzInst, nodeName, pos):
        nodzInst.createNode(name=nodeName, position=pos)

    def __init__(self, nodzInst, nodeList=[], nodeCreator=None):
        """
        Initialize the graphics view.

        """
        super(QtPopupLineEditWidget, self).__init__(nodzInst)
        self.nodzInst = nodzInst
        self.nodeList = nodeList
        if nodeCreator is None:
            self.nodeCreator = self.defaultNodeCreator
        else:
            self.nodeCreator = nodeCreator
        self.returnPressed.connect(self.onReturnPressedSlot)
        #hide by default
        self.hide()
        self.clear()
        self.parentWidget().setFocus()

    def popup(self):
        position = self.parentWidget().mapFromGlobal(QtGui.QCursor.pos())
        self.move(position)
        self.clear()
        self.show()
        self.setFocus()
        self.setNodesList(self.nodeList)
        self.completer.complete()

    def popdown(self):
        self.hide()
        self.clear()
        self.parentWidget().setFocus()

    def setNodesList(self, nodeList):
        self.nodeList = nodeList
        self.completer = QtWidgets.QCompleter(self.nodeList, self)
        self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.setCompleter(self.completer)
        self.completer.activated.connect(self.onCompleterActivatedSlot)

        fontMetrics = QtGui.QFontMetrics(self.font())
        maxSize = self.size()
        for nodeName in self.nodeList:
            boundingSize = fontMetrics.boundingRect(nodeName).size()
            maxSize.setWidth(max(maxSize.width(), boundingSize.width()+30))  #30 is for margin
        self.resize(maxSize.width(), self.size().height())

    def focusOutEvent(self, QFocusEvent):
        self.popdown()

    def onCompleterActivatedSlot(self, text):
        pos=QtCore.QPointF(self.nodzInst.mapToScene(self.pos()))
        self.popdown()
        newNode = self.nodeCreator(self.nodzInst, text, pos)
        if newNode is not None:
            self.nodzInst.signal_UndoRedoAddNode.emit(self.nodzInst, newNode.userData)

    def onReturnPressedSlot(self):
        name = self.text()
        pos = QtCore.QPointF(self.nodzInst.mapToScene(self.pos()))
        self.completer.activated.disconnect(self.onCompleterActivatedSlot)
        self.popdown()
        newNode = self.nodeCreator(self.nodzInst, name, pos)
        if newNode is not None:
            self.nodzInst.signal_UndoRedoAddNode.emit(self.nodzInst, newNode.userData)
