dx11ShaderCmd::dx11ShaderCmd()
{}

dx11ShaderCmd::~dx11ShaderCmd()
{}

MStatus dx11ShaderCmd::doIt( const MArgList& args )
{
	MStatus status;

	// Parse the shader node
	//
	MArgParser parser( syntax(), args );
	MString nodeName;
	parser.getCommandArgument( 0, nodeName );


	MSelectionList list;
	status = list.add( nodeName );
	MObject shaderNode;
	status = list.getDependNode( 0, shaderNode );
	if( status != MS::kSuccess )
	{
		MStringArray args;
		args.append(nodeName);

		MString msg = dx11ShaderStrings::getString( dx11ShaderStrings::kInvalidDx11ShaderNode, args );
		displayError( msg );
		return MS::kFailure;
	}

	MArgDatabase argData( syntax(), args, &status );
	if ( !status )
		return status;

	bool fIsQuery = argData.isQuery();

	MFnDependencyNode depFn( shaderNode );
	if( depFn.typeId() != dx11ShaderNode::typeId() )
	{
		MString msg = dx11ShaderStrings::getString( dx11ShaderStrings::kInvalidDx11ShaderNode, nodeName );
		displayError( msg );
		return MS::kFailure;
	}

	dx11ShaderNode* shader = (dx11ShaderNode*) depFn.userNode();
	if( !shader )
	{
		MString msg = dx11ShaderStrings::getString( dx11ShaderStrings::kInvalidDx11ShaderNode, nodeName );
		displayError( msg );
		return MS::kFailure;
	}

	if ( fIsQuery ) 
	{
		if( parser.isFlagSet(kFXFileFlag) )
		{
			MString path = shader->effectName();
			setResult( path );
			return MS::kSuccess;
		}
		else if( parser.isFlagSet( kListTechniquesFlag ) )
		{
			setResult( shader->techniques() );
			return MS::kSuccess;
		}
		else if( parser.isFlagSet( kListLightInformationFlag ) )
		{
			setResult( shader->lightInfoDescription() );
			return MS::kSuccess;
		}
		else if( parser.isFlagSet( kListUIGroupInformationFlag ) )
		{
			setResult( shader->getUIGroups() );
			return MS::kSuccess;
		}
	}
	else
	{
		// Process flag arguments
		//
		if( parser.isFlagSet(kReloadFlag) )
		{
			dx11ShaderNode::reloadAll( shader->effectName() );
			//shader->reload( );
		}
		else if( parser.isFlagSet(kClearParametersFlag))
		{
			shader->clearParameters();
		}
		else if( parser.isFlagSet(kConnectLightFlag))
		{
			MString connectableLightName;
			argData.getFlagArgument(kConnectLightFlag, 0, connectableLightName);
			int lightIndex = shader->getIndexForLightName(connectableLightName);
			if (lightIndex < 0)
			{
				MString msg = dx11ShaderStrings::getString( dx11ShaderStrings::kUnknownConnectableLight, connectableLightName );
				displayError( msg );
				return MS::kFailure;
			}
			MString lightName;
			argData.getFlagArgument(kConnectLightFlag, 1, lightName);
			MDagPath lightPath;
			list.clear();
			list.add( lightName );
			status = list.getDagPath(0, lightPath);
			if( status != MS::kSuccess )
			{
				MString msg = dx11ShaderStrings::getString( dx11ShaderStrings::kUnknownSceneObject, lightName );
				displayError( msg );
				return MS::kFailure;
			}
			// Make sure it is a light:
			MDagPath lightShapeDagPath;
			unsigned int numShapes = 0;
			lightPath.numberOfShapesDirectlyBelow(numShapes);
			for (unsigned int i = 0; i < numShapes; ++i)
			{
				MDagPath shapePath(lightPath);
				status = shapePath.extendToShapeDirectlyBelow(i);
				if( status == MS::kSuccess && shapePath.hasFn(MFn::kLight))
				{
					lightShapeDagPath = shapePath;
					break;
				}
			}

			if (!lightShapeDagPath.isValid())
			{
				MString msg = dx11ShaderStrings::getString( dx11ShaderStrings::kNotALight, lightName );
				displayError( msg );
				return MS::kFailure;
			}

			shader->disconnectLight(lightIndex);
			shader->connectLight(lightIndex,lightShapeDagPath);
			return MS::kSuccess;
		}
		else if( parser.isFlagSet(kLightConnectionStatusFlag))
		{
			MString connectableLightName;
			argData.getFlagArgument(kLightConnectionStatusFlag, 0, connectableLightName);
			int lightIndex = shader->getIndexForLightName(connectableLightName);
			if (lightIndex < 0)
			{
				MString msg = dx11ShaderStrings::getString( dx11ShaderStrings::kUnknownConnectableLight, connectableLightName );
				displayError( msg );
				return MS::kFailure;
			}
			setResult( shader->getLightConnectionInfo(lightIndex));
			return MS::kSuccess;
		}
		else if(parser.isFlagSet(kListLightParametersFlag))
		{
			MString connectableLightName;
			argData.getFlagArgument(kListLightParametersFlag, 0, connectableLightName);
			int lightIndex = shader->getIndexForLightName(connectableLightName);
			if (lightIndex < 0)
			{
				MString msg = dx11ShaderStrings::getString( dx11ShaderStrings::kUnknownConnectableLight, connectableLightName );
				displayError( msg );
				return MS::kFailure;
			}
			setResult( shader->getLightableParameters(lightIndex, parser.isFlagSet(kListLightParameterSemanticsFlag) ));
			return MS::kSuccess;
		}
		else if(parser.isFlagSet(kListUIGroupParametersFlag))
		{
			MString connectableUIGroupName;
			argData.getFlagArgument(kListUIGroupParametersFlag, 0, connectableUIGroupName);
			int uiGroupIndex = shader->getIndexForUIGroupName(connectableUIGroupName);
			if (uiGroupIndex < 0)
			{
				MString msg = dx11ShaderStrings::getString( dx11ShaderStrings::kUnknownUIGroup, connectableUIGroupName );
				displayError( msg );
				return MS::kFailure;
			}
			setResult( shader->getUIGroupParameters(uiGroupIndex) );
			return MS::kSuccess;
		}
		else if( parser.isFlagSet(kDisconnectLightFlag))
		{
			MString lightName;
			argData.getFlagArgument(kDisconnectLightFlag, 0, lightName);
			int lightIndex = shader->getIndexForLightName(lightName);
			if (lightIndex < 0)
			{
				MString msg = dx11ShaderStrings::getString( dx11ShaderStrings::kUnknownConnectableLight, lightName );
				displayError( msg );
				return MS::kFailure;
			}
			shader->disconnectLight(lightIndex);
			return MS::kSuccess;
		}
	}
	

	return MS::kSuccess;
}

MSyntax dx11ShaderCmd::newSyntax()
{
	MSyntax syntax;
	syntax.enableQuery();
	syntax.addFlag( kReloadFlag, kReloadFlagLong);
	syntax.addFlag( kFXFileFlag, kFXFileFlagLong, MSyntax::kString );
	syntax.addFlag( kListTechniquesFlag, kListTechniquesFlagLong);
	syntax.addFlag( kClearParametersFlag, kClearParametersFlagLong);
	syntax.addFlag( kListLightInformationFlag, kListLightInformationFlagLong);
	syntax.addFlag( kConnectLightFlag, kConnectLightFlagLong, MSyntax::kString, MSyntax::kString );
	syntax.addFlag( kLightConnectionStatusFlag, kLightConnectionStatusFlagLong, MSyntax::kString );
	syntax.addFlag( kListLightParametersFlag, kListLightParametersFlagLong, MSyntax::kString );
	syntax.addFlag( kListLightParameterSemanticsFlag, kListLightParameterSemanticsFlagLong );
	syntax.addFlag( kListUIGroupInformationFlag, kListUIGroupInformationFlagLong);
	syntax.addFlag( kListUIGroupParametersFlag, kListUIGroupParametersFlagLong, MSyntax::kString );
	syntax.addFlag( kDisconnectLightFlag, kDisconnectLightFlagLong, MSyntax::kString);
	syntax.addArg( MSyntax::kString );	
	return syntax;
}

void* dx11ShaderCmd::creator()
{
	return new dx11ShaderCmd;
}

