// Copyright (C) 1997-2010 Autodesk, Inc., and/or its licensors.
// All rights reserved.
//
// The coded instructions, statements, computer programs, and/or related
// material (collectively the "Data") in these files contain unpublished
// information proprietary to Autodesk, Inc. ("Autodesk") and/or its licensors,
// which is protected by U.S. and Canadian federal copyright law and by
// international treaties.
//
// The Data is provided for use exclusively by You. You have the right to use,
// modify, and incorporate this Data into other products for purposes authorized 
// by the Autodesk software license agreement, without fee.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. AUTODESK
// DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTIES
// INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF NON-INFRINGEMENT,
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR ARISING FROM A COURSE 
// OF DEALING, USAGE, OR TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS
// LICENSORS BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
// DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK AND/OR ITS
// LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY OR PROBABILITY OF SUCH DAMAGES.

//
//
//  Creation Date:  Feb. 6, 1997
//
//  Description:
//      Project Create/Setup window.
//
//  Input Arguments:
//		None
// 
//  Return Value:
//		None
//

global proc np_resetDefaults ( )
//
//	Description:
//		Fill in the project defaults.
//
{
	string	$fileRuleWidgets[] = np_getFileRuleWidgets("");

	int		$listSize;
	int		$index;

	$listSize = size($fileRuleWidgets);

	for ($index = 0; $index < $listSize; $index += 4)
	{
		string $name = formValidObjectName("np_FileLocation" + $fileRuleWidgets[$index + 1]);
		$name = substituteAllString($name, " ", "_");
		string $value = $fileRuleWidgets[$index + 2];

		textFieldGrp -e -fileName $value $name;
	}
}


global proc int np_createNewProjectCallback ( )
//
//	Description:
//		Create a new workspace and generate all of the
//		sub directories under it using the user defined defaults.
//
//	Returns true if the project was created OK.
//			false otherwise.
//
//	Notes:
//		This has the side effect of setting the workspace to the
//		newly created workspace.
//
{
	string $pathSep = ":";
	if (`about -nt`) {
		$pathSep = ";";
	}

	string $shortWSName; 
	string $wsLocation;
	string $wsRootDir;
	string $project;

	$shortWSName = `textFieldGrp -q -text ("np_wsName0")`;
	$wsLocation = `textFieldGrp -q -fileName ("wsLocation0")`;
//	$wsRootDir = `textFieldGrp -q -fileName ("wsRootDir0")`;

	int $locationSize = size($wsLocation);
	string $ok = (uiRes("m_projectSetup.kOk"));
	if ($shortWSName == "") {
		confirmDialog -message (uiRes("m_projectSetup.kNoProjectNameGiven")) 
			-button $ok -defaultButton $ok -parent newProjectWindow;
		return false;
	}	
	if ($locationSize <= 0) {
		confirmDialog -message (uiRes("m_projectSetup.kNoProjectLocationGiven")) 
			-button $ok -defaultButton $ok -parent newProjectWindow;
		return false;
	}	

	if (match("/$", $wsLocation) != "/") {
		$project = $wsLocation + "/" + $shortWSName;
	} else {
		$project = $wsLocation + $shortWSName;
	}

	string $message;
	string $displayMsg;
	// Verify that it doesn't exist.
	if (`file -q -ex ($project + "/workspace.mel")`) {
		// Then the project exists.
		$message = (uiRes("m_projectSetup.kProjectExist"));
		$displayMsg =`format -s $project $message`;
		confirmDialog -message $displayMsg 
			-button $ok -defaultButton $ok -parent newProjectWindow;
		return false;
	}

	workspace -cr $project;

	if (!(`file -q -ex $project`)) {
		$message = (uiRes("m_projectSetup.kProjectCreationFailed"));
		$displayMsg =`format -s $project $message`;
		confirmDialog -message $displayMsg 
			-button $ok -defaultButton $ok -parent newProjectWindow;
		return false;
	}	

	// Open the project
	//
	workspace -o $project;

	// Call the edit callback to set all the values and create
	// necessary directories
	//
	np_editProjectCallback();

	// Reset the prefs for the browser with project specific stuff.
	//
	np_resetBrowserPrefs;

	addRecentProject( $project );

	return true;
}

global proc int np_editProjectCallback ( )
//
//  Description:
//      Update the project setup using the user supplied information.
//
//  Note:
//      Old directories won't be removed because of the update however
//      they may become unused.
//
{
global string $gProjectSetupWorkspace;

	string $pathSep = ":";
	if (`about -nt`) {
		$pathSep = ";";
	}

	// Make sure we go to the project root directory before
	// creating any new directories.
	//
	string	$origDir = `workspace -q -dir`;
	workspace -dir `workspace -q -rd`;

	string	$fileRuleWidgets[];
	string	$fileRuleMap[];

	$fileRuleWidgets = np_getFileRuleWidgets($gProjectSetupWorkspace);
	$fileRuleMap     = np_getTranslatorWidgetMap();

    int     $index;
    int     $listSize;
	string	$locations[];

	$listSize = size($fileRuleWidgets);

	for ($index = 0; $index < $listSize; $index += 4)
	{
		string $frtype = $fileRuleWidgets[$index + 1];
		string $name  = formValidObjectName("np_FileLocation" + $frtype);
		string $value;
		int $j, $found;

		$value = `textFieldGrp -q -fileName $name`;

		$found = 0;
		for ($j = 0; $j < size($fileRuleMap); $j += 2)
		{
			if ($frtype == $fileRuleMap[$j + 1])
			{
				$found = 1;
				workspace -fr $fileRuleMap[$j] $value;
			}
		}

		if (!$found)
		{
			workspace -fr $frtype $value;
		}

		if ($value != "")
		{
			if (tokenize($value, $pathSep, $locations) > 0)
			{
				workspace -cr $locations[0];
			}
		}
	}

	workspace -u;
    workspace -s;

	// Restore the original directory
	//
	workspace -dir $origDir;

	// If the browser is up then we must reset it.
	//

	if (`window -ex projectViewerWindow`) {
		pv_resetWorkspace;	// Force the layout to be redone.
	}

	return true;
}

proc np_showSetup ( string $wsFullName, string $parent, int $bUseDefaults, int $bEditable ) 
//
//	Description:
//		Display the setup information for either the specified workspace
//	 	or the preference defaults.
//
//	Parameters:
//		$wsFullName		- the full name of the workspace.
//		$parent			- the parent controlLayout.
//		$bUseDefaults	- Fill with defaults.
//		$bEditable		- if true then make the fields editable.
//
{
	string $np_wsRuleList = ("np_wsRuleList");
	string $itemList[];
	int $listSize;
	int $listSize2;
	int $index;
	int $index2;
	string $curTypeRules[];
	string $curObjectRules[];
	string $curRenderRules[];
	string $tmpString;			// Multi use string.
	int $bFound;
	string $curLocation;
	int $justFill;
	string $tfgName;
	int	$useWorkspace = $wsFullName != "";

	string $fileRuleWidgets[] = np_getFileRuleWidgets($wsFullName);

	if (`columnLayout -ex $np_wsRuleList`) {
		// The window is already filled in so just replace contents.
		$justFill = 1;
	} else {
		$justFill = 0;
	}
	
	setParent $parent;
	
	if( !$justFill ) {
		columnLayout -adj true $np_wsRuleList;
	}
	// All project directories
	
	if (!$justFill) {
		frameLayout -borderVisible true -borderStyle "etchedIn"
			-collapsable true -collapse false 
			-label (uiRes("m_projectSetup.kShowSetupProjLocLbl"))
			("ProjectDataLocations_Defaults_F");
		columnLayout ("ProjectDataLocations_Defaults_L");
	}

	$listSize = size($fileRuleWidgets);
	for ($index = 0; $index < $listSize; $index += 4)
	{
		string $label = $fileRuleWidgets[$index];
		string $name  = formValidObjectName(substituteAllString("np_FileLocation" + $fileRuleWidgets[$index + 1]," ", "_"));
//		$name = substituteAllString($name, " ", "_");
		string $value = $fileRuleWidgets[$index + 3];

		if ($justFill) {
			textFieldGrp -e -fileName $value $name;
		} else {
print("Create ===== bEditable "+$bEditable+" value ["+$value+"] name ["+$name+"]\n");
/* Modify for 	MAYA2011 NNExporter
			textFieldGrp -editable $bEditable -fileName $value -label $label $name;
***/
			textFieldGrp -editable $bEditable -fileName $value -label $label -cw2 250 250 $name;
print("Finish\n");
		}
	}
print("np_wsRuleList "+$np_wsRuleList+"\n");
	setParent $np_wsRuleList;
}

global proc int np_setNewLocation( string $path, string $type )
{
	string $wsLocationCtl = ("wsLocation0");

	// All we do on a successful browse is set the control in the
	// 'New Project' window - if user accepts this choice all the
	// workspace stuff will be set up at that point
	if ( `textFieldButtonGrp -ex $wsLocationCtl` )
	{
		textFieldButtonGrp -e -fi $path $wsLocationCtl;
		return true;
	}

	// If we got here, then the user may have either accepted the
	// new project settings from the New Project window, or cancelled
	// the New Project window. In either case, the following should
	// be valid:
	string $wsDir = `workspace -q -fn`;
	if ( `file -q -ex $wsDir` )
		workspace -dir $wsDir;

	return false;
}

global proc np_browseForNewLocation()
{
	string $wsDir;
	string $wsLocationCtl = ("wsLocation0");

	if ( `textFieldButtonGrp -ex $wsLocationCtl` )
	{
		$wsDir = `textFieldButtonGrp -q -fi $wsLocationCtl`;
		// Have to set the workspace dir so the fileBrowser knows
		// where to start looking
		if ( `file -q -ex $wsDir` )
			workspace -dir $wsDir;
	}

	string $selectLocation = (uiRes("m_projectSetup.kSelectLocation"));
	fileBrowser "np_setNewLocation" $selectLocation "" 4;
}

global proc np_setupNewInfo ( )
{
	string  $parent = "wsSetupInfo";

	string $wsLocationCtl = ("wsLocation0");
	string $np_wsNameCtl = ("np_wsName0");
	string $wsScrollCtl = ("wsScroll0");
	int $enabled = 1;
	
	button -e -enable $enabled acceptSetup;
	
	if (!`textFieldButtonGrp -ex $np_wsNameCtl`) {
		string $currentDir = "";
		// Check for environment variable first
		$currentDir = getenv("MAYA_PROJECTS_DIR");
		if(size($currentDir) == 0) {
			// Environment var is not set, so check for option var now
			if(`optionVar -ex "ProjectsDir"`) {
				$currentDir = `optionVar -q "ProjectsDir"`;
			}
			if(size($currentDir) == 0) {
				// Option var is not set too. Get the root directory of the current workspace
				$currentDir = dirname( `workspace -q -fn` );
			}
		}
		setParent $parent;
			textFieldButtonGrp -editable $enabled 
				-label (uiRes("m_projectSetup.kSetupNewInfoNameLbl"))
				-text (uiRes("m_projectSetup.kSetupNewInfoNameTxt"))
				-buttonLabel (uiRes("m_projectSetup.kSetupNewInfoHelpLbl"))
				-buttonCommand "showHelp NewProject"
				$np_wsNameCtl;
			textFieldButtonGrp -editable $enabled 
				-label (uiRes("m_projectSetup.kSetupNewInfoLocLbl"))
				-fileName $currentDir 
				-bl (uiRes("m_projectSetup.kSetupNewInfoBrowseLbl"))
				-bc np_browseForNewLocation $wsLocationCtl;
			scrollLayout -cr true $wsScrollCtl;
				np_showSetup ( "", $wsScrollCtl, 0, $enabled);

/* Modify for MAYA2011 NNExporter
		formLayout -e 
			-af $wsLocationCtl left 0 
			-af $wsLocationCtl right 0 
			-ac $wsLocationCtl top 0 $np_wsNameCtl
			-af $np_wsNameCtl left 0 
			-af $np_wsNameCtl right 0 
			-af $np_wsNameCtl top 5  
			-af $wsScrollCtl left 0 
			-af $wsScrollCtl right 0 
			-af $wsScrollCtl bottom 0 
			-ac $wsScrollCtl top 3 $wsLocationCtl  
				$parent;
*/
		formLayout -e
			-af $wsLocationCtl left 15
			-af $wsLocationCtl right 0
			-ac $wsLocationCtl top 0 $np_wsNameCtl
			-af $np_wsNameCtl left 15
			-af $np_wsNameCtl right 0
			-af $np_wsNameCtl top 5
			-af $wsScrollCtl left 0
			-af $wsScrollCtl right 0
			-af $wsScrollCtl bottom 0
			-ac $wsScrollCtl top 3 $wsLocationCtl
				$parent;
	}
}

global proc np_setupEditInfo ( )
{
	string  $parent = "wsSetupInfo";

	string $wsLocationCtl = ("wsLocation0");
	string $np_wsNameCtl = ("np_wsName0");
	string $wsScrollCtl = ("wsScroll0");
	
	button -e -enable 1 acceptSetup;
	
	if (!`textFieldGrp -ex $np_wsNameCtl`) {
		string $workspace = `workspace -q -fn`;		// Get the current WS name.
		string $wsLocation = dirname( $workspace );
		string $wsName = basename( $workspace, "" );

		setParent $parent;
			textFieldButtonGrp -label (uiRes("m_projectSetup.kSetupEditInfoNameLbl"))
				-editable false
				-text $wsName
				-buttonLabel (uiRes("m_projectSetup.kSetupEditInfoHelpLbl"))
				-buttonCommand "showHelp EditProject"
				$np_wsNameCtl;
			textFieldGrp -label (uiRes("m_projectSetup.kSetupEditInfoLocLbl"))
				-editable false 
				-fileName $wsLocation 
				$wsLocationCtl;

			scrollLayout -cr true $wsScrollCtl;
				np_showSetup ( $workspace, $wsScrollCtl, 0, 1);
		
		formLayout -e 
			-af $wsLocationCtl left 0 
			-af $wsLocationCtl right 0 
			-ac $wsLocationCtl top 0 $np_wsNameCtl
			-af $np_wsNameCtl left 0 
			-af $np_wsNameCtl right 0 
			-af $np_wsNameCtl top 5 
			-af $wsScrollCtl left 0 
			-af $wsScrollCtl right 0 
			-af $wsScrollCtl bottom 0 
			-ac $wsScrollCtl top 3 $wsLocationCtl
			$parent;
	}
}


global proc string np_displayWSSetup ( string $parent, int $mode )
//
//	Parameters:
//		$parent		- the parent control layout.
//		$mode		- 1 == new project
//					  2 == edit project.
//
{
	setParent $parent;		

	string $callback;

	if ($mode == 1) {
		$callback = "if (np_createNewProjectCallback()) deleteUI newProjectWindow;";
	} else if ($mode == 2) {
		$callback = "np_editProjectCallback;deleteUI newProjectWindow";
	}

	int $numDivisions = 100;
	formLayout -nd $numDivisions WSsetupForm;
		formLayout setupControlArea;
			button	-label (uiRes("m_projectSetup.kDisplayWSAcceptLbl"))
					-width 110 -c $callback acceptSetup;
			if ($mode == 1) {
				button	-label (uiRes("m_projectSetup.kDisplayWSDefaultsLbl"))
						-width 110 -c "np_resetDefaults" defaultSetup;
			}
			button	-label (uiRes("m_projectSetup.kDisplayWSCancelLbl"))
					-width 110 -c "deleteUI newProjectWindow;" cancelSetup;
			
			formLayout -e 
				-af acceptSetup "left" 5
				-af acceptSetup "bottom" 5
				-an acceptSetup "top"
				-af cancelSetup "bottom" 5
				-af cancelSetup "right" 5
				-an cancelSetup "top"
				setupControlArea;

			// Only have the "Use Defaults" button in "new" mode since the
			// user shouldn't be making radical changes to a created project.
			//
			if ($mode == 1) {
				formLayout -e 
					-ap acceptSetup "right" 0 33
					-an defaultSetup top 
					-ac defaultSetup left 5 acceptSetup
					-ap defaultSetup right 0 66
					-af defaultSetup bottom 5
					-ac cancelSetup "left" 5 defaultSetup
					setupControlArea;
			} else {
				formLayout -e 
					-ap acceptSetup "right" 3 50
					-ac cancelSetup "left" 5 acceptSetup
					setupControlArea;
			}

							
		setParent WSsetupForm;
		
		formLayout wsSetupInfo;
	
		formLayout -e -af wsSetupInfo top 0 -af wsSetupInfo left 0
						-af wsSetupInfo right 0
						-ac wsSetupInfo bottom 5 setupControlArea
						-af setupControlArea left 0 -af setupControlArea right 0
						-af setupControlArea bottom 0
						-an setupControlArea top
						WSsetupForm;
	
	if ($mode == 1) {
		np_setupNewInfo;	// Draw the first tab.
	} if ($mode == 2) {
		np_setupEditInfo;
	}
	
	return "WSsetupForm";
}

global proc projectSetup ( int $mode )
//
//	Description:
//		Edit a new project if $mode = 2.
//		Display a new project setup window if $mode = 1.
//		Display the defaults if $mode = 0.  - defunct
//
{
global string $gProjectSetupWorkspace;

	if (!`window -ex newProjectWindow`) {
		window	-iconName (uiRes("m_projectSetup.kProjSetupWkSpcProjectLbl"))
				-wh 450 510 newProjectWindow;
			tabLayout -tv false windowLayout;
				columnLayout dummyTab;
	} else {
		// remove the children 
		string $childList[] = `tabLayout -q -ca windowLayout`;
		int $numChildren = size($childList);
		int $child;

		for ($child = 0; $child < $numChildren; $child++) {
			deleteUI $childList[$child];
		}

		setParent windowLayout;
		columnLayout dummyTab;	// Make the dummy display tab.
	}

	if ($mode == 2) {
		// remember the current workspace (we need it for editing)
		//
		$gProjectSetupWorkspace = `workspace -query -fullName`;
		window -e -title (uiRes("m_projectSetup.kProjSetupWkSpcEditProjectLbl")) 
				newProjectWindow;
		tabLayout -e -st dummyTab windowLayout;
		string $newTab = `np_displayWSSetup windowLayout $mode`;
		tabLayout -e -st $newTab windowLayout;
	} else if ($mode == 1) {
		// set the "current" workspace to nothing since there is none
		//
		$gProjectSetupWorkspace = "";
		window -e -title (uiRes("m_projectSetup.kProjSetupWkSpcNewProjectLbl")) 
				newProjectWindow;
		tabLayout -e -st dummyTab windowLayout;
		string $newTab = `np_displayWSSetup windowLayout $mode`;
		tabLayout -e -st $newTab windowLayout;
	}

	showWindow newProjectWindow;
}
