import maya.cmds as cmds
import maya.mel as mel
import os

import dmptools.setup.items as items
reload(items)
from items import hotkeyItems as HOTKEY_ITEMS
from dmptools.output import defaultPrint, successPrint, errorPrint

def setHotkey(hotkey):
    """Creates a hotkey from a given hotkey dict"""

    # get hotkey dict data
    name = hotkey['name']
    key = hotkey['key']
    alt = hotkey['alt']
    ctl = hotkey['ctrl']
    release = hotkey['release']
    command = hotkey['command']
    if release:
        releaseName = name+"Release"
        releaseCommand = hotkey['releaseCommand']

    # check if this is not a separator
    if not 'separator' in command:
        #create hotkey command
        cmds.nameCommand(name, sourceType="mel", annotation=name, command=command)
        if release:
            cmds.nameCommand(releaseName, sourceType="mel", annotation=releaseName, command=releaseCommand)
        
        # set hotkey
        cmds.hotkey(k=key, alt=alt, ctl=ctl, name=name)    
        if release:
            cmds.hotkey(k=key, alt=alt, ctl=ctl, releaseName=releaseName)

def main():
    """Creates the dmptools hotkeys"""

    defaultPrint('creating hotkeys...')

    for hotKey in HOTKEY_ITEMS:
        setHotkey(hotKey)
    # save hotkeys pref files
    cmds.savePrefs(hotkeys=True)

if __name__ == '__main__':
    main()
