#-------------------------------------------------------------------------------
# Name:        YJ_Arm_Rig
# Purpose:     Rig Arms
#
# Author:      yangjie
#
# Created:     13/07/2014
# Copyright:   (c) yangjie 2014
# Licence:     <your licence>
#-------------------------------------------------------------------------------
from YJ_Rig_Pub import*
from YJ_FK_Rig import*
from YJ_IK_Rig import*



L_Arm_Jnt_List = YJ_Load_List('L_Arm_Jnt_List.pkl')
R_Arm_Jnt_List = YJ_GetMir_List(L_Arm_Jnt_List)
Arm_IKFK_Type = radioCollection('Arm_radioCollection',q=1,sl=1)

# NamePre = 'L_'
def YJ_Arm_Rig():

    L_ArmAll_Grp = CreatGRpToTarg("L_ArmAll_Grp",L_Arm_Jnt_List[0])
    R_ArmAll_Grp = CreatGRpToTarg("R_ArmAll_Grp",R_Arm_Jnt_List[0])

    YJ_Try_Parent(L_Arm_Jnt_List[0],L_ArmAll_Grp)
    YJ_Try_Parent(R_Arm_Jnt_List[0],R_ArmAll_Grp)

    L_ArmFk_List,L_ArmFkConstrnList,L_FK_Grp,R_ArmFk_List,R_ArmFkConstrnList,R_FK_Grp= YJ_Arm_FK_Rig(Arm_IKFK_Type,L_Arm_Jnt_List,R_Arm_Jnt_List,L_ArmAll_Grp,R_ArmAll_Grp)
    L_ArmIk_List,L_ArmIkCtrl_List,L_IK_All_Grp,R_ArmIk_List,R_IK_All_Grp,R_ArmIkCtrl_List = YJ_Arm_IK_Rig(L_Arm_Jnt_List,L_ArmAll_Grp,R_Arm_Jnt_List,R_ArmAll_Grp)

    L_IKFKCtrl,R_IKFKCtrl = YJ_Jud_Cre_ArmIKFKCtrl(L_ArmAll_Grp,R_ArmAll_Grp,L_ArmFkConstrnList,R_ArmFkConstrnList,L_FK_Grp,L_IK_All_Grp,R_FK_Grp,R_IK_All_Grp)


def YJ_Jud_Cre_ArmIKFKCtrl(L_ArmAll_Grp,R_ArmAll_Grp,L_ArmFkConstrnList,R_ArmFkConstrnList,L_FK_Grp,L_IK_Grp,R_FK_Grp,R_IK_Grp):
    if Arm_IKFK_Type == 'Arm_IKFK':
        L_IKFKCtrl = YJ_Cre_ArmIKFKCtrl(L_Arm_Jnt_List[-2],'L_Arm_IKFK_Anim',L_ArmAll_Grp,L_ArmFkConstrnList,L_FK_Grp,L_IK_Grp)
        R_IKFKCtrl = YJ_Cre_ArmIKFKCtrl(R_Arm_Jnt_List[-2],'R_Arm_IKFK_Anim',R_ArmAll_Grp,R_ArmFkConstrnList,R_FK_Grp,R_IK_Grp)
    else:
        L_IKFKCtrl,R_IKFKCtrl = '',''
    return  L_IKFKCtrl,R_IKFKCtrl
# TarJnt,CtrlName,ArmAll_Grp,ParConLis = L_Arm_Jnt_List[-1],'L_Arm_IKFK_Anim',L_ArmAll_Grp,L_ArmFkConstrnList
def YJ_Cre_ArmIKFKCtrl(TarJnt,CtrlName,ArmAll_Grp,ParConLis,FK_Grp,IK_Grp):
    YJ_Creat_Ctrl('Cur12',CtrlName)
    YJ_Scale_Shape([CtrlName],0.1)
    YJ_MoveOrRotateToTargetWd(TarJnt,CtrlName,'t','t')
    YJ_Try_Parent(CtrlName,ArmAll_Grp)
    AttrLis = YJ_AddIKFKCtrlAttr(CtrlName)
    IKFKAttr = CtrlName + '.' +AttrLis[1][1]
    YJ_ConnectIKFKAttr('FK','IK',IKFKAttr,ParConLis,FK_Grp,IK_Grp)
    return CtrlName

def YJ_AddIKFKCtrlAttr(Ctrl):
    AttrList=[['Kind','Basic'],['Bl','IKFK']]
    YJ_AddAttr(Ctrl,AttrList)
    return AttrList

##

def YJ_Arm_FK_Rig(IKFK_Type,L_ArmJnt_List,R_ArmJnt_List,L_ArmAll_Grp,R_ArmAll_Grp):
    if Arm_IKFK_Type != 'Arm_IK':
        L_ArmFk_List,L_ArmFkConstrnList,L_FK_Grp = YJ_Jud_Arm_Fk_Rig(L_ArmJnt_List,'L',L_ArmAll_Grp)
        R_ArmFk_List,R_ArmFkConstrnList,R_FK_Grp = YJ_Jud_Arm_Fk_Rig(R_ArmJnt_List,'R',R_ArmAll_Grp)
    else:
        L_ArmFk_List,L_ArmFkConstrnList,L_FK_Grp,R_ArmFk_List,R_ArmFkConstrnList,R_FK_Grp = [],[],[],[],[],[]

    return L_ArmFk_List,L_ArmFkConstrnList,L_FK_Grp,R_ArmFk_List,R_ArmFkConstrnList,R_FK_Grp



# ArmList =L_Arm_Jnt_List,NamePre ='L',ArmAll_Grp =L_ArmAll_Grp
def YJ_Jud_Arm_Fk_Rig(ArmList,NamePre,ArmAll_Grp):
    ArmFk_List = YJ_DuplicateJnt(ArmList,'jnt','FK')
    ZeroCreate = YJRigZeroGrp([ArmFk_List[0]])
    parent(ZeroCreate,w=True )
    FK_Grp = rename(ZeroCreate[0], NamePre+'_Fk_Grp' )
    YJ_CreatFkRig(ArmFk_List[0],ArmFk_List[-1],1,0)
    ConstrnList = YJ_SetParentConstraint(ArmFk_List,ArmList)
    YJ_Scale_Shape(ArmFk_List[:-1],0.2)
    YJ_Try_Parent(FK_Grp,ArmAll_Grp)
    return ArmFk_List[:-1],ConstrnList,FK_Grp

#ArmList = L_ArmJnt_List,NamePre = 'L'

def YJ_Arm_IK_Rig(L_ArmJnt_List,L_ArmAll_Grp,R_ArmJnt_List,R_ArmAll_Grp):
    if Arm_IKFK_Type != 'Arm_FK':
        L_ArmIk_List,L_ArmIkCtrl_List,L_IK_All_Grp = YJ_Jud_Arm_Ik_Rig(L_ArmJnt_List,'L',L_ArmAll_Grp)
        R_ArmIk_List,R_ArmIkCtrl_List,R_IK_All_Grp= YJ_Jud_Arm_Ik_Rig(R_ArmJnt_List,'R',R_ArmAll_Grp)
    else:
        L_ArmIk_List,L_ArmIkCtrl_List,L_IK_All_Grp,R_ArmIk_List,R_IK_All_Grp,R_ArmIkCtrl_List = [],[],[],[],[],[]

    return L_ArmIk_List,L_ArmIkCtrl_List,L_IK_All_Grp,R_ArmIk_List,R_IK_All_Grp,R_ArmIkCtrl_List
#ArmList,NamePre,L_ArmAll_Grp = L_Arm_Jnt_List,'L',L_ArmAll_Grp
def YJ_Jud_Arm_Ik_Rig(ArmList,NamePre,L_ArmAll_Grp):
    ArmIk_List = YJ_DuplicateJnt(ArmList,'jnt','IK')
    ZeroCreate = YJRigZeroGrp([ArmIk_List[0]])
    parent(ZeroCreate,w=True )
    IK_All_Grp = rename(ZeroCreate[0], NamePre+'_Ik_Grp' )
    IKGrp,ArmIkCtrl_List,Arm_IKHandle= YJ_CreateIkRig(ArmIk_List[0],ArmIk_List[2],1,'rz','+')
    YJ_Try_Parent(IKGrp,IK_All_Grp)
    ConstrnList = YJ_SetParentConstraint(ArmIk_List,ArmList)
    YJ_Scale_Shape(ArmIkCtrl_List,0.2)
    YJ_Try_Parent(IK_All_Grp,L_ArmAll_Grp)
    RcIK = YJ_Creat_IK(ArmIk_List[-2],ArmIk_List[-1],'ikSCsolver' )
    YJ_Try_Parent(RcIK,ArmIkCtrl_List[0])

    return ArmIk_List,ArmIkCtrl_List,IK_All_Grp