// ****************************************************************************
//
// Copyright 2014 Autodesk, Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// *       Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// *       Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// *       Neither the name of Sony Pictures Imageworks, nor
// Industrial Light & Magic, nor the names of their contributors may be used
// to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ****************************************************************************

//
//  Description:
//      Insert a sub menu into Pipeline Cache menu.
//      This procedure ensures menu ordering.
//

global proc insertPipelineSubMenu(string $subMenu, string $label, string $postMenuCommand)
{
    global string $gMainWindow;  // defined by Maya

    // Check Pipeline Cache menu
    setParent $gMainWindow;
    if (!`menu -q -exists mainPipelineCacheMenu`) {
        // Pipeline Cache menu does not exist, create one
        menu -label (uiRes("m_insertPipelineSubMenu.kPipelineCache")) -tearOff true mainPipelineCacheMenu;
    }

    // Check the sub menu
    setParent -menu mainPipelineCacheMenu;

    if (`menuItem -q -exists $subMenu`) {
        return;
    }
    else {
        string $existingMenuName[] = `menu -q -itemArray mainPipelineCacheMenu`;

        int $numItems = size($existingMenuName);
        if ($numItems == 0 || strcmp($existingMenuName[$numItems-1], $subMenu) < 0) {
            // Adding an item that should come last alphabetically,
            // so appending is sufficient

            // Create sub menu
            string $menu = `menuItem -label $label
                                     -tearOff true
                                     -subMenu true
                                     $subMenu`;
            menuItem -e -postMenuCommand ($postMenuCommand + " " + $menu)
                        -postMenuCommandOnce true
                        $menu;
            setParent -menu ..;
        }
        else {
            // Backup and delete all existing menus
            string $existingMenuLabel[];
            int    $existingMenuTearOff[];
            int    $existingMenuSubMenu[];
            string $existingMenuPostMenuCommand[];
            int    $existingMenuPostMenuCommandOnce[];
            
            int $i;
            for ($i = 0; $i < size($existingMenuName); $i++) {
                $existingMenuLabel[$i] = `menuItem -q -label $existingMenuName[$i]`;
                $existingMenuTearOff[$i] = `menuItem -q -tearOff $existingMenuName[$i]`;
                $existingMenuSubMenu[$i] = `menuItem -q -subMenu $existingMenuName[$i]`;
                $existingMenuPostMenuCommand[$i] = `menuItem -q -postMenuCommand $existingMenuName[$i]`;
                $existingMenuPostMenuCommandOnce[$i] = `menuItem -q -postMenuCommandOnce $existingMenuName[$i]`;
            }

            menu -e -deleteAllItems mainPipelineCacheMenu;

            // Restore the menus before sub menu
            for ($i = 0; $i < size($existingMenuName) && strcmp($existingMenuName[$i], $subMenu) < 0; $i++) {
                menuItem -label   $existingMenuLabel[$i]
                         -tearOff $existingMenuTearOff[$i]
                         -subMenu $existingMenuSubMenu[$i]
                         -postMenuCommand     $existingMenuPostMenuCommand[$i]
                         -postMenuCommandOnce $existingMenuPostMenuCommandOnce[$i]
                         $existingMenuName[$i];
                if ($existingMenuSubMenu[$i]) {
                    setParent -menu ..;
                }
            }

            // Create sub menu
            string $menu = `menuItem -label $label
                                     -tearOff true
                                     -subMenu true
                                     $subMenu`;
            menuItem -e -postMenuCommand ($postMenuCommand + " " + $menu)
                        -postMenuCommandOnce true
                        $menu;
            setParent -menu ..;

            // Restore the menus after sub menu
            for (; $i < size($existingMenuName); $i++) {
                menuItem -label   $existingMenuLabel[$i]
                         -tearOff $existingMenuTearOff[$i]
                         -subMenu $existingMenuSubMenu[$i]
                         -postMenuCommand     $existingMenuPostMenuCommand[$i]
                         -postMenuCommandOnce $existingMenuPostMenuCommandOnce[$i]
                         $existingMenuName[$i];
                if ($existingMenuSubMenu[$i]) {
                    setParent -menu ..;
                }
            }
        }
    }
}


//
//  Description:
//      Delete a sub menu from Pipeline Cache menu.
//

global proc deletePipelineSubMenu(string $subMenu)
{
    global string $gMainWindow;  // defined by Maya

    // Check Pipeline Cache menu
    setParent $gMainWindow;
    if (!`menu -q -exists mainPipelineCacheMenu`) {
        // Pipeline Cache menu does not exist
        return;
    }

    // Check the sub menu
    setParent -menu mainPipelineCacheMenu;
    if (`menuItem -q -exists $subMenu`) {
        // Delete the sub menu
        deleteUI -menuItem $subMenu;
    }

    // Delete Pipeline Cache menu if there are no sub menus
    if (`menu -q -numberOfItems mainPipelineCacheMenu` == 0) {
        deleteUI -menu mainPipelineCacheMenu;
    }
}
