import os, sys, re
# import hou
main = __import__('__main__')
hou = main.__dict__['hou']
import hqt
reload (hqt)
from managers.completeWidget import contextCompleterClass

path = os.path.join(os.path.dirname(__file__), 'houdini')

main = __import__('__main__')
ns = main.__dict__
for mod in [os.path.splitext(x)[0] for x in os.listdir(path)]:
    if not mod in ns:
        try:
            exec 'import ' + mod in ns
        except:
            pass

if not path in sys.path:
    sys.path.insert(0, path)

from pw_multiScriptEditor import scriptEditor
reload(scriptEditor)


def show(*args, **kwargs):
    hqt.show(scriptEditor.scriptEditorClass, *args, **kwargs)



# EXAMPLE SHELF BUTTON
# H13
# path = 'path/to/MultiScriptEditor_module'
# # example c:/houdini/python/lib
# if not path in sys.path:
#     sys.path.append(path)
# import pw_multiScriptEditor
# reload(pw_multiScriptEditor)
# pw_multiScriptEditor.showHoudini(ontop=1)

# H14
#import sys
# path = 'path/to/MultiScriptEditor_module'
# # example c:/houdini/python/lib
# if not path in sys.path:
#     sys.path.append(path)
# import pw_multiScriptEditor
# reload(pw_multiScriptEditor)
# pw_multiScriptEditor.showHoudini(name='Multi Script Editor',replacePyPanel=1, hideTitleMenu=0)




###################### CONTEXT FUNCTIONS

# def saveToNode(code, node):
#     definition = node.type().definition()
#     definition.sections()["PythonCook"].setContents(code)
# saveToNode('import hou',hou.selectedNodes()[0])

def getAllDifinitions():
    names = []
    for key in hou.nodeTypeCategories().keys():
        names += hou.nodeTypeCategories()[key].nodeTypes().keys()
    names = list(set(names))
    return names

roots = ['obj', 'shop', 'ch', 'vex', 'img', 'out']
nodes = list(set(getAllDifinitions()))

def completer(line):
    # node types
    func = ['createNode', 'createInputNode', 'createOutputNode']
    for f in func:
        p = r"\.%s\(['\"](\w*)$" % f
        m = re.search(p, line)
        if m:
            name = m.group(1)
            if name:
                auto = [x for x in nodes if x.lower().startswith(name.lower())]
            else:
                auto = nodes
            l = len(name)
            return [contextCompleterClass(x, x[l:], True) for x in auto], None
    # absolute path
    p = r"(?<=['\"]{1})(/[\w/]*)$"
    m = re.search(p, line)
    if m:
        name = m.group(0)
        auto, add = getChildrenFromPath(name)
        if auto or add:
            return auto, add
    return None, None

def getChildrenFromPath(path):
    sp = path.rsplit('/', 1)
    if not sp[0]: # rootOnly
        if sp[1]:
            nodes = [contextCompleterClass(x, x[len(sp[1]):]) for x in roots if x.startswith(sp[1])]
            return nodes, None
        else:
            nodes = [contextCompleterClass(x, x) for x in roots]
            return nodes, None
    # add parms
    else:
        node = hou.node(sp[0][1:])
        if node:
            nd = list(set([x.name() for x in node.children()]))
            nodes = [contextCompleterClass(x, x[len(sp[1]):]) for x in sorted(nd) if x.startswith(sp[1])]
            ch = list(set([x.name() for x in node.parms()] + [x.name() for x in node.parmTuples()]))
            channels = [contextCompleterClass(x, x[len(sp[1]):]) for x in sorted(ch) if x.startswith(sp[1])]
            return nodes, channels
    return None, None

def contextMenu(parent):
    m = houdiniMenuClass(parent)
    return m

class houdiniMenuClass(hqt.QMenu):
    def __init__(self, parent):
        super(houdiniMenuClass, self).__init__('Houdini', parent)
        self.par = parent
        self.addAction(hqt.QAction('Save To Node', parent, triggered=self.saveToNode))
        self.addAction(hqt.QAction('Read From Node', parent, triggered=self.readFromNode))


    def readFromNode(self):
        sel = hou.selectedNodes()
        if sel:
            res = self.getSectionsFromNode(sel[0])
            if not res:
                hou.ui.displayMessage('Sections not found1')
                return
            keys = res.keys()
            s = hou.ui.selectFromList(keys, exclusive=1)
            if s: