# $Revision$ on $Date$
# The above revision line needs to be the first line for all techtk files
# Copyright (c) 2006 Ansys Inc
namespace eval ::AIEnv::GUIManagement {}

namespace eval ::AIEnv::GUIManagement::NodeMover {
	variable Commands
	variable Options
	variable MoverData
	
	set Commands {
		set
		get
		move
	}
	
	set Options {
		invertFlag
		type
	}
}

proc ::AIEnv::GUIManagement::NodeMover::RouterProc {Name Cmd args} {

	if {[catch {eval [list RouterProcImpl $Name $Cmd] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	}
}

proc ::AIEnv::GUIManagement::NodeMover::RouterProcImpl {Name Cmd args} {
	# Check the command to see if it is valid
	variable Commands
	if { [lsearch $Commands $Cmd] != -1 } {
		return [eval [list ::AIEnv::GUIManagement::NodeMover::_$Cmd $Name] $args]
	} else {
		set Optlist [join $Commands ", "]
		set Optlist [linsert $Optlist "end-1" "or"]
		error "Unknown command: \"Cmd\" given to picker named: \"$Name\".  Valid commands are: \"$Optlist\"."
	}	
}

proc ::AIEnv::GUIManagement::NodeMover::_set {Name args} {
	if {[catch {eval [list SetImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result		
	}
}

proc ::AIEnv::GUIManagement::NodeMover::SetImpl {Name args} {
	variable Options
	variable MoverData
	variable SelectionTypes
	if { [llength $args] == 3 } {
		set Flag [lindex $args 0]
		set Key [lindex $args 1]
		set Value [lindex $args 2]
		if { $Flag == "-key" } {
			if { [lsearch $Options $Key] != -1 } {
				if { $Key == "type" } {
					set Value [string tolower $Value]
					if { [lsearch $SelectionTypes $Value] == -1 } {
						set Optlist [join $SelectionTypes ", "]
						set Optlist [linsert $Optlist "end-1" "or"]
						error "Invalid selection type: \"$Value\" given for picker: \"$Name\"\n. Valid selection types are: $Optlist."
					}
				}
				set MoverData($Name,$Key) $Value
			} else {
				set Optlist [join $Options ", "]
				set Optlist [linsert $Optlist "end-1" "or"]
				error "Invalid option: \"$Key\" for picker: \"$Name\". Valid options are $Optlist."
			}
		} else {
			error "Invalid flag: \"$Flag\" for picker: \"$Name\". Must be -key"
		}
	} else {
		error "Wrong number of arguments: Should be \"$Name set -key key value\""
	}

}


proc ::AIEnv::GUIManagement::NodeMover::_get {Name args} {
	
	if {[catch {eval [list GetImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	}
}

proc ::AIEnv::GUIManagement::NodeMover::GetImpl {Name args} {
	variable Options
	variable MoverData
	if { [llength $args] == 2 } {
		set Flag [lindex $args 0]
		set Key [lindex $args 1]
		if { $Flag == "-key" } {
			if { [lsearch $Options $Key] != -1 } {
				return $MoverData($Name,$Key)
			} else {
				set Optlist [join $Options ", "]
				set Optlist [linsert $Optlist "end-1" "or"]
				error "Invalid option: \"$Key\" for picker: \"$Name\". Valid options are $Optlist."
			}
		} else {
			error "Invalid flag: \"$Flag\" for picker: \"$Name\". Must be -key"
		}
	} else {
		error "Wrong number of arguments: Should be \"$Name get -key key\""
	}
}


proc ::AIEnv::GUIManagement::NodeMover::_move {Name args} {
	if {[catch {eval [list MoveImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	}	
}

proc ::AIEnv::GUIManagement::NodeMover::MoveImpl {Name args} {
	variable MoverData
	if {[array names MoverData $Name,invertFlag] != ""} {
		set FlagVar $MoverData($Name,invertFlag)
	} else {
		error "The multipleFlag variable was not set for the mover: \"$Name\"."
	}
	
	global $FlagVar
	if { [info exists $FlagVar] == 1 } {
		set InvertFlag [subst $[subst $FlagVar]]
	} else {
		set InvertFlag 0
	}
	unstruct_move_node $InvertFlag
}
