source MCPXutils;

global string $MCPXLastPoseUuid;

global string $MCPX_pose_ctr_name;

global proc AEMCPXPoseTemplate( string $nodeName ){
    
    editorTemplate -beginScrollLayout;

	editorTemplate -beginLayout "Pose Weight" -collapse false;

	   editorTemplate -callCustom "MCPXPoseCommonCreateView" "MCPXPoseCommonUpdateView" "weight" "weight_min" "weight_max";

	editorTemplate -endLayout;

	editorTemplate -beginLayout "Attributes" -collapse false;

	   editorTemplate -callCustom "MCPXPoseCreateAttrs" "MCPXPoseReplaceAttrs" "attrs";

	editorTemplate -endLayout;

    AEdependNodeTemplate $nodeName;

    editorTemplate -suppress "mute";

	editorTemplate -addExtraControls;

	editorTemplate -endScrollLayout;
}

global proc MCPXPoseCommonCreateView(string $weight, string $weight_min, string $weight_max){
    python("from mocapx.ui.ae.pose import pose_create_view");
    string $par = `setParent -q`;

    python("pose_create_view('" + $par + "', '" + $weight + "', '" + $weight_min + "', '" + $weight_max + "')");

    global string $MCPXLastPoseUuid;
    $MCPXLastPoseUuid = MCPXGetNodeUUID($weight);
}


global proc MCPXPoseCommonUpdateView(string $weight, string $weight_min, string $weight_max){
    python("from mocapx.ui.ae.pose import pose_update_view");
    string $par = `setParent -q`;

    global string $MCPXLastPoseUuid;

    python("pose_update_view('" + $par + "', '" + $weight + "', '" + $weight_min + "', '" + $weight_max +  "', '" + $MCPXLastPoseUuid + "')");

    $MCPXLastPoseUuid = MCPXGetNodeUUID($weight);
}



global proc MCPXPoseCreateAttrs(string $attrName){
    global string $MCPX_pose_ctr_name;
    $MCPX_pose_ctr_name = MCPXPoseBuildAttrsSheet($attrName);
}

global proc MCPXPoseReplaceAttrs( string $attrName ){
    global string $MCPX_pose_ctr_name;
    deleteUI -control $MCPX_pose_ctr_name;
    $MCPX_pose_ctr_name = MCPXPoseBuildAttrsSheet($attrName);
}

global proc string MCPXPoseBuildAttrsSheet(string $plugName){
    setUITemplate -pst attributeEditorTemplate;
        string $createdControl = "";
        string $buffer[];
        tokenize($plugName, ".", $buffer);
        string $node = $buffer[0];
        
        int $index;
        int $indexes[] = `getAttr -multiIndices $plugName`;
        int $numChildren = size($indexes);
        
        string $indexedPlug, $attrPlug, $ctrlName, $field_pose, $field_rest;
        string $ctrlNames[], $collectionNode[];
        
        $createdControl = `columnLayout`;
        if ($numChildren > 0){
            for($i=0; $i<$numChildren; $i++){
                $index = $indexes[$i];
                $indexedPlug = ($plugName + "[" + $index + "]");
                $attrPlug = ($indexedPlug + ".rest");
                $collectionNode = `listConnections -p on -d off -s on $attrPlug`;

                if (size($collectionNode) > 0){
                    $sourceAttrPlug = `substitute "\.restValue" $collectionNode[0] ".plug"`;
                    $ctrlNames = `listConnections -plugs on -destination off -scn on $sourceAttrPlug`;
                    $ctrlNames = `ls -shortNames $ctrlNames`;
                    $ctrlName = $ctrlNames[0];
                } else{
                    $ctrlName = ("custom[" + $index + "]");
                }
                $field_pose = "floatFieldPose_" + $index;
                $field_rest = "floatFieldRest_" + $index;

                rowLayout -nc 4 -cw4 233 60 60 20;
                    text -label $ctrlName;
                    floatField $field_rest;
                    floatField $field_pose;
                    symbolButton -image "smallTrash.png" -command ("removeMultiInstance -break true \"" + $indexedPlug + "\";");
                    connectControl $field_rest ($indexedPlug + ".rest");
                    connectControl $field_pose ($indexedPlug + ".pose");
                setParent ..;
            }

        } else {
            text -label "No attributes";
        }

    rowLayout -nc 3 -cw3 233 122 20;
        text "";
        button -label "Add Item" -command ("MCPXCreateNonNumericMultiAddNewItem(\"" +  $node + "\", \"attrs\");");
    setParent ..;
    
    setUITemplate -popTemplate;
    return $createdControl;
}
