"""
modeling memo for marking menu

    -preseve uvs
    -vertex color -> black
    -merge vertex
    -combine
    -separate
    -symmetry
    -scale on axis
    -snap
    -select from camera
    -invert selection
"""
import maya.cmds as cmds
import maya.mel as mel
import os

import dmptools.setup.items as items
reload(items)
from items import markingMenuItems as MARKINGMENU_ITEMS

MARKINGMENU_FILE = cmds.internalVar(userMarkingMenuDir=True)+'menu_dmptools.mel'

def getParentPanel():
    """Gets ant returns the parent panel"""

    upPanel = cmds.getPanel(up=True)
    if cmds.panel(upPanel, q=True, ex=True):
        upPanelLayout = cmds.layout(upPanel, q=True, p=True)
        while not cmds.paneLayout(upPanelLayout, q=True, ex=True):
            upPanelLayout = cmds.control(upPanelLayout, q=True, p=True)
        if cmds.paneLayout(upPanelLayout, q=True, ex=True):
            return upPanelLayout
    else:
        return "viewPanes"

def buildMenu(item):
    """Build the actual marking menu UI"""

    # fill the marking menu items
    name = item['name']
    subMenu = item['subMenu']
    position = item['position']
    # to be added to each item to correctly close the marking menu
    onCloseCommand = ';import dmptools.setup.markingMenu as markingMenu;markingMenu.deleteMarkingMenu()'
    # create item
    if position:
        command = item['command'].replace('python("', '').replace('");', '')
        cmds.menuItem(
            label=name,
            subMenu=subMenu,
            command=command+onCloseCommand,
            enable=True,
            data=0,
            boldFont=False,
            radialPosition=position,
            enableCommandRepeat=True,
            image="commandButton.png",
            echoCommand=1,
            sourceType="python",
            )
    else:
        if name == 'separator':
            cmds.menuItem(divider=True)
        else:
            command = item['command'].replace('python("', '').replace('");', '')
            cmds.menuItem(
                label=name,
                subMenu=subMenu,
                command=command+onCloseCommand,
                enable=True,
                data=0,
                boldFont=False,
                enableCommandRepeat=True,
                image="commandButton.png",
                echoCommand=1,
                sourceType="python",
                )

def showMarkingMenu():
    """Shows the marking menu when the hotkey is pressed"""

    if cmds.popupMenu('dmptoolsMarkingMenu', ex=True):
        cmds.deleteUI('dmptoolsMarkingMenu')
    # Create dmptools marking menu.
    dmptoolsMenu = cmds.popupMenu('dmptoolsMarkingMenu', b=1, mm=True, parent=getParentPanel())
    mel.eval('source \"'+MARKINGMENU_FILE+'"')

def deleteMarkingMenu():
    """Deletes the marking menu"""

    if cmds.popupMenu('dmptoolsMarkingMenu', ex=True):
        cmds.deleteUI('dmptoolsMarkingMenu')

def createMenu():
    """Creates the marking menu"""

    # remove the existing dmptools marking menu is exists
    if os.path.exists(MARKINGMENU_FILE):
        os.remove(MARKINGMENU_FILE)
    # creating marking menu
    dmptoolsMenu = cmds.popupMenu('dmptoolsMarkingMenu', b=1, mm=True, parent=getParentPanel())
    for item in MARKINGMENU_ITEMS:
        buildMenu(item)
    # Save the menu to a file.
    cmds.saveMenu(dmptoolsMenu, 'menu_dmptools')
    showMarkingMenu()
