'''
*   This script was created by
*       Trevor Sommer
*           trevor@trevorsommer.com
*
*   Module containing wrapper functions for users to call via shell or from shelf, and gui buttons
'''

import maya.cmds as cmds
from . import rigUtil as rUtil
from . import rigLib as rLib
from . import rigPieces as rPcs

def arm_basicSetupSelect(distroUp= 1, distroDown= 1):
    '''
    Creates a simple arm setup, via selecing base joint (shoulder Joint of arm on binding Joint chain) and running proc
    '''
    selection = cmds.ls(selection= True)

    if not selection or len(selection) > 1:
        cmds.error('arm_basicSetupSelect requires that you select a single shoulder joint in your scene') 
    if cmds.nodeType(selection[0]) != "joint":
        cmds.error('Improper Input passed into function arm_basicSetupRun. baseJoint must be of type \"joint\"')   
    if distroUp and not isinstance(distroUp, int):
        cmds.error('Improper Input passed into function arm_basicSetupRun. distroUp must be of type \"int\"')  
    if distroDown and not isinstance(distroUp, int):
        cmds.error('Improper Input passed into function arm_basicSetupRun. distroDown must be of type \"int\"')

    chainMembers = cmds.listRelatives(selection[0], allDescendents= True, type= "joint")
    if not chainMembers or (len(chainMembers) + 1) != 4:
        cmds.error('Improper Input passed into function arm_basicSetupSelect. Joint chain must have a length of 4 joints in order to setup an arm')   

    #get charPfx if any exists
    chainMembers.append(selection[0])
    charPfx = rUtil.get_charPfx(chainMembers)
    #if no charPfx is found skip setup
    if not charPfx:
        cmds.error('No charPfx could be obtained from input, unable to designate character, canceling arm_basicSetupRun operation')

    #get the side of the character     
    sidePfx = selection[0].split("_")[1] 

    rPcs.arm_basicSetupProc(selection[0], chainMembers[0], chainMembers[1], chainMembers[2], charPfx, sidePfx, distroUp, distroUp)
    
def arm_basicSetupRun(baseJoint, distroUp= 1, distroDown= 1):
    '''
    Creates a simple arm setup, via passing the base joint (shoulder Joint of arm) and running proc
    '''
    if cmds.nodeType(baseJoint) != "joint":
        cmds.error('Improper Input passed into function arm_basicSetupRun. baseJoint must be of type \"joint\"')   
    if distroUp and not isinstance(distroUp, int):
        cmds.error('Improper Input passed into function arm_basicSetupRun. distroUp must be of type \"int\"')  
    if distroDown and not isinstance(distroUp, int):
        cmds.error('Improper Input passed into function arm_basicSetupRun. distroDown must be of type \"int\"')

    chainMembers = cmds.listRelatives(baseJoint, allDescendents= True, type= "joint")
    if (len(chainMembers) + 1) != 4:
        cmds.error('Improper Input passed into function arm_basicSetupRun. Joint chain must have a length of 4 joints in order to setup an arm')   

    #get charPfx if any exists
    chainMembers.append(baseJoint)
    charPfx = rUtil.get_charPfx(chainMembers)
    #if no charPfx is found skip setup
    if not charPfx:
        cmds.error('No charPfx could be obtained from input, unable to designate character, canceling arm_basicSetupRun operation')

    #get the side of the character     
    sidePfx = baseJoint.split("_")[1] 

    rPcs.arm_basicSetupProc(baseJoint, chainMembers[0], chainMembers[1], chainMembers[2], charPfx, sidePfx, distroUp, distroUp)
    