global proc usdTranslatorImport_SetCheckbox(string $arg, string $widget) {
    if ($arg == "0") {
        checkBox -e -v false $widget;
    } else {
        checkBox -e -v true $widget;
    }
}

global proc usdTranslatorImport_SetOptionMenuByAnnotation(
        string $ann, string $widget) {
    int $index = 1; // 1-based indexing.
    for ($i in `optionMenuGrp -q -itemListLong $widget`) {
        if (`menuItem -q -ann $i` == $ann) {
            optionMenuGrp -e -select $index $widget;
            return;
        }

        $index++;
    }
}

global proc string usdTranslatorImport_AppendFromCheckbox(string $currentOptions, string $arg, string $widget) {
    if (`checkBox -q -v $widget` == 1) {
        return $currentOptions + ";" + $arg + "=1";
    } else {
        return $currentOptions + ";" + $arg + "=0";
    }
}

global proc string usdTranslatorImport_AppendFromPopup(string $currentOptions, string $arg, string $widget) {
    int $index = `optionMenuGrp -q -select $widget` - 1;
    string $allItems[] = `optionMenuGrp -q -itemListLong $widget`;
    string $item = $allItems[$index];
    string $annotation = `menuItem -q -ann $item`;
    return $currentOptions + ";" + $arg + "=" + $annotation;
}

global proc string usdTranslatorImport_AppendFromIntField(string $currentOptions, string $arg, string $widget) {
    string $value = `intFieldGrp -q -value1 $widget`;
    return $currentOptions + ";" + $arg + "=" + $value;
}

global proc usdTranslatorImport_AnimationCB() {
    if (`checkBox -q -v readAnimDataCheckBox` == 1) {
        checkBox -e -vis true customFrameRangeCheckBox;
        int $vis = (`checkBox -q -v customFrameRangeCheckBox` == 1);
        intFieldGrp -e -vis $vis startTimeField;
        intFieldGrp -e -vis $vis endTimeField;
        checkBox -e -vis true useAsAnimationCacheCheckBox;
    } else {
        checkBox -e -vis false customFrameRangeCheckBox;
        intFieldGrp -e -vis false startTimeField;
        intFieldGrp -e -vis false endTimeField;
        checkBox -e -vis false useAsAnimationCacheCheckBox;
    }
}

global proc int usdTranslatorImport (string $parent,
                                 string $action,
                                 string $initialSettings,
                                 string $resultCallback )
//
//  Description:
//    This script posts the USDImport file translator options.
//    The optionsString is of the form:
//    varName1=value1;varName2=value2;...
//
//  Parameters:
//    $parent - the elf parent layout for this options layout. It is
//            always a scrollLayout.
//    $action - the action that is to be performed with this invokation of this proc.
//        Valid options are:
//        "query" - construct the options string and pass it to the resultCallback.
//        "post" - post all the elf controls.
//    $initialSettings - the current options string in effect at the time this script is invoked.
//    $resultCallback - This is the proc to be called with the result string. 
//            resultCallback ( string $optionsString )
//
//    Returns:
//        1 if successfull.
//        0 otherwise.
//
{
    int $bResult;
    string $currentOptions;
    string $optionList[];
    string $optionBreakDown[];
    int $index;
        
    if ($action == "post") {
        setParent $parent;

        columnLayout -adj true usdOptsCol;

        string $importers[] = `usdListShadingModes -import`;
        optionMenuGrp -l "Shading Mode:" shadingModePopup;
        for ($modeName in $importers) {
            string $niceName = $modeName;
            if ($modeName == "displayColor") {
                $niceName = "Material Colors";
            } else if ($modeName == "pxrRis") {
                $niceName = "RfM Shaders";
            }
            menuItem -l $niceName -ann $modeName;
        }

        checkBox -l "Read Animation Data" -cc ("usdTranslatorImport_AnimationCB") readAnimDataCheckBox;

        checkBox -l "Custom Frame Range" -cc ("usdTranslatorImport_AnimationCB") customFrameRangeCheckBox;

        columnLayout -width 100 animOptsCol;
            intFieldGrp -l "Start Time:" startTimeField;
            intFieldGrp -l "End Time:" endTimeField;

        checkBox -l "Use As Animation Cache" -cc ("usdTranslatorImport_AnimationCB") useAsAnimationCacheCheckBox;

        optionMenuGrp -l "Assembly Representation:" assemblyRepPopup;
            menuItem -l "Collapsed" -ann "Collapsed";
            menuItem -l "Full" -ann "Full";
            menuItem -l "Import" -ann "Import";
        
        setParent $parent;
        
        // Now set to current settings.
        $currentOptions = $initialSettings;
        if (size($currentOptions) > 0) {
            tokenize($currentOptions, ";", $optionList);
            for ($index = 0; $index < size($optionList); $index++) {
                tokenize($optionList[$index], "=", $optionBreakDown);
                if ($optionBreakDown[0] == "shadingMode") {
                    usdTranslatorImport_SetOptionMenuByAnnotation($optionBreakDown[1], "shadingModePopup");
                } else if ($optionBreakDown[0] == "readAnimData") {
                    usdTranslatorImport_SetCheckbox($optionBreakDown[1], "readAnimDataCheckBox");
                } else if ($optionBreakDown[0] == "useAsAnimationCache") {
                    usdTranslatorImport_SetCheckbox($optionBreakDown[1], "useAsAnimationCacheCheckBox");
                } else if ($optionBreakDown[0] == "assemblyRep") {
                    usdTranslatorImport_SetOptionMenuByAnnotation($optionBreakDown[1], "assemblyRepPopup");
                } else if ($optionBreakDown[0] == "startTime") {
                    int $startTime = $optionBreakDown[1];
                    intFieldGrp -e -v1 $startTime startTimeField;
                } else if ($optionBreakDown[0] == "endTime") {
                    int $endTime = $optionBreakDown[1];
                    intFieldGrp -e -v1 $endTime endTimeField;
                } else if ($optionBreakDown[0] == "useCustomFrameRange") {
                    usdTranslatorImport_SetCheckbox($optionBreakDown[1], "customFrameRangeCheckBox");
                }
            }
        }
                
        $bResult = 1;
    
    } else if ($action == "query") {
    
        $currentOptions = usdTranslatorImport_AppendFromPopup($currentOptions, "shadingMode", "shadingModePopup");
        $currentOptions = usdTranslatorImport_AppendFromCheckbox($currentOptions, "readAnimData", "readAnimDataCheckBox");
        $currentOptions = usdTranslatorImport_AppendFromCheckbox($currentOptions, "useAsAnimationCache", "useAsAnimationCacheCheckBox");
        $currentOptions = usdTranslatorImport_AppendFromPopup($currentOptions, "assemblyRep", "assemblyRepPopup");
        $currentOptions = usdTranslatorImport_AppendFromIntField($currentOptions, "startTime", "startTimeField");
        $currentOptions = usdTranslatorImport_AppendFromIntField($currentOptions, "endTime", "endTimeField");
        $currentOptions = usdTranslatorImport_AppendFromCheckbox($currentOptions, "useCustomFrameRange", "customFrameRangeCheckBox");

        eval($resultCallback+" \""+$currentOptions+"\"");
        $bResult = 1;

    } else {
        $bResult = 0;
    }

    usdTranslatorImport_AnimationCB();
    return $bResult;
}
