import struct questPaymentConditionData
{
	import var isInverted : Bool;
	import var paymentItemId : ItemID;
	import var paymentAmount : Uint32;
}

abstract class ScriptConditionTypeBase extends IScriptable
{

	public quest const virtual function Evaluate( playerObject : GameObject ) : Bool
	{
		return false;
	}

	public quest const virtual function ToString() : String
	{
		return "";
	}

}

abstract class BluelineConditionTypeBase extends ScriptConditionTypeBase
{

	public const virtual function GetBluelinePart( playerObject : GameObject ) : BluelinePart
	{
		var part : BluelinePart;
		return part;
	}

	public virtual function ExecuteBluelineAction( playerObject : GameObject ) {}
}

class LifePath_ScriptConditionType extends BluelineConditionTypeBase
{
	[ customEditor = "TweakDBGroupInheritance;LifePath" ]
	instanceeditable var m_lifePathId : TweakDBID;
	instanceeditable var m_inverted : Bool;

	public export quest const override function Evaluate( playerObject : GameObject ) : Bool
	{
		var playerDevSystem : PlayerDevelopmentSystem;
		var playerLifePath : gamedataLifePath;
		var lifePath : gamedataLifePath;
		var playerControlledObject : GameObject;
		playerDevSystem = GetPlayerDevelopmentSystem();
		playerLifePath = playerDevSystem.GetLifePath( playerObject );
		lifePath = TweakDBInterface.GetLifePathRecord( m_lifePathId ).Type();
		if( !( playerObject ) )
		{
			return false;
		}
		playerControlledObject = GameInstance.GetPlayerSystem( playerObject.GetGame() ).GetLocalPlayerControlledGameObject();
		playerLifePath = playerDevSystem.GetLifePath( playerControlledObject );
		if( !( m_inverted ) )
		{
			return lifePath == playerLifePath;
		}
		else
		{
			return lifePath != playerLifePath;
		}
	}

	public const override function GetBluelinePart( playerObject : GameObject ) : BluelinePart
	{
		var part : LifePathBluelinePart;
		part = new LifePathBluelinePart;
		part.passed = Evaluate( playerObject );
		part.m_record = TweakDBInterface.GetLifePathRecord( m_lifePathId );
		part.captionIconRecordId = part.m_record.CaptionIcon().GetID();
		return part;
	}

	public export quest const override function ToString() : String
	{
		var outString : String;
		if( m_inverted )
		{
			outString += "NOT ";
		}
		outString += TDBID.ToStringDEBUG( m_lifePathId );
		return outString;
	}

	private const function GetPlayerDevelopmentSystem() : PlayerDevelopmentSystem
	{
		return ( ( PlayerDevelopmentSystem )( GameInstance.GetScriptableSystemsContainer( GetGameInstance() ).Get( 'PlayerDevelopmentSystem' ) ) );
	}

}

class Build_ScriptConditionType extends BluelineConditionTypeBase
{
	[ customEditor = "TweakDBGroupInheritance;ContentAssignment" ]
	instanceeditable var m_questAssignment : TweakDBID;
	[ customEditor = "TweakDBGroupInheritance;PlayerBuild" ]
	instanceeditable var m_buildId : TweakDBID;
	instanceeditable var m_difficulty : EGameplayChallengeLevel;
	instanceeditable var m_comparisonType : ECompareOp;

	public quest const override function Evaluate( playerObject : GameObject ) : Bool
	{
		var playerValue : Int32;
		var checkValue : Int32;
		playerValue = RPGManager.GetBuildScore( playerObject, TweakDBInterface.GetPlayerBuildRecord( m_buildId ) );
		checkValue = RPGManager.GetBluelineBuildCheckValue( playerObject, TweakDBInterface.GetContentAssignmentRecord( m_questAssignment ), m_difficulty );
		switch( m_comparisonType )
		{
			case ECompareOp.CO_Lesser:
				return playerValue < checkValue;
			case ECompareOp.CO_LesserEq:
				return playerValue <= checkValue;
			case ECompareOp.CO_Greater:
				return playerValue > checkValue;
			case ECompareOp.CO_GreaterEq:
				return playerValue >= checkValue;
			case ECompareOp.CO_Equal:
				return playerValue == checkValue;
			default:
				return playerValue != checkValue;
		}
	}

	public const override function GetBluelinePart( playerObject : GameObject ) : BluelinePart
	{
		var part : BuildBluelinePart;
		part = new BuildBluelinePart;
		part.m_lhsValue = RPGManager.GetBuildScore( playerObject, TweakDBInterface.GetPlayerBuildRecord( m_buildId ) );
		part.m_rhsValue = RPGManager.GetBluelineBuildCheckValue( playerObject, TweakDBInterface.GetContentAssignmentRecord( m_questAssignment ), m_difficulty );
		part.passed = Evaluate( playerObject );
		part.m_record = TweakDBInterface.GetPlayerBuildRecord( m_buildId );
		part.m_comparisonOperator = m_comparisonType;
		part.captionIconRecordId = part.m_record.CaptionIcon().GetID();
		return part;
	}

	public quest const override function ToString() : String
	{
		var outString : String;
		outString = ( ( ( TDBID.ToStringDEBUG( m_buildId ) + " " ) + ( ( String )( m_comparisonType ) ) ) + " " ) + ( ( String )( m_difficulty ) );
		return outString;
	}

}

class PaymentConditionTypeBase extends BluelineConditionTypeBase
{
	instanceeditable var m_inverted : Bool;
	instanceeditable var m_payWhenSucceded : Bool;
	default m_payWhenSucceded = true;

	public quest const function IsInverted() : Bool
	{
		return m_inverted;
	}

	public quest const function IsPaidWhenSucceeded() : Bool
	{
		return m_payWhenSucceded;
	}

	public quest const virtual function GetPaymentAmount( playerObject : GameObject ) : Uint32
	{
		return 0;
	}

	public quest const function GetPaymentMoneyItemId() : ItemID
	{
		return MarketSystem.Money();
	}

	public quest const override final function Evaluate( playerObject : GameObject ) : Bool
	{
		var playerMoney : Int32;
		var paymentMoney : Int32;
		playerMoney = GameInstance.GetTransactionSystem( playerObject.GetGame() ).GetItemQuantity( playerObject, GetPaymentMoneyItemId() );
		paymentMoney = ( ( Int32 )( GetPaymentAmount( playerObject ) ) );
		return ( ( m_inverted ) ? ( playerMoney < paymentMoney ) : ( playerMoney >= paymentMoney ) );
	}

	public override function ExecuteBluelineAction( playerObject : GameObject )
	{
		var paymentAmount : Int32;
		if( !( IsInverted() ) )
		{
			if( IsPaidWhenSucceeded() )
			{
				paymentAmount = ( ( Int32 )( GetPaymentAmount( playerObject ) ) );
				GameInstance.GetTransactionSystem( GetGameInstance() ).RemoveItem( playerObject, GetPaymentMoneyItemId(), paymentAmount );
			}
		}
	}

	protected function GetPaymentData( playerObject : GameObject ) : questPaymentConditionData
	{
		var ret : questPaymentConditionData;
		ret.isInverted = m_inverted;
		ret.paymentItemId = GetPaymentMoneyItemId();
		ret.paymentAmount = GetPaymentAmount( playerObject );
		return ret;
	}

}

class PaymentBalanced_ScriptConditionType extends PaymentConditionTypeBase
{
	[ customEditor = "TweakDBGroupInheritance;ContentAssignment" ]
	instanceeditable var m_questAssignment : TweakDBID;
	instanceeditable var m_difficulty : EGameplayChallengeLevel;

	public quest const override function GetPaymentAmount( playerObject : GameObject ) : Uint32
	{
		return ( ( Uint32 )( RPGManager.GetBluelinePaymentValue( playerObject, TweakDBInterface.GetContentAssignmentRecord( m_questAssignment ), m_difficulty ) ) );
	}

	public const override function GetBluelinePart( playerObject : GameObject ) : BluelinePart
	{
		var part : PaymentBluelinePart;
		part = new PaymentBluelinePart;
		part.m_playerMoney = GameInstance.GetTransactionSystem( playerObject.GetGame() ).GetItemQuantity( playerObject, GetPaymentMoneyItemId() );
		part.m_paymentMoney = RPGManager.GetBluelinePaymentValue( playerObject, TweakDBInterface.GetContentAssignmentRecord( m_questAssignment ), m_difficulty );
		part.passed = Evaluate( playerObject );
		part.captionIconRecordId = T"ChoiceCaptionParts.OpenVendorIcon";
		return part;
	}

	public quest const override function ToString() : String
	{
		var outString : String;
		outString = ( ( String )( m_difficulty ) );
		return outString;
	}

}

class PaymentFixedAmount_ScriptConditionType extends PaymentConditionTypeBase
{
	instanceeditable var m_payAmount : Uint32;

	public quest function SetPayAmountValue( value : Uint32 )
	{
		m_payAmount = value;
	}

	public quest const override function GetPaymentAmount( playerObject : GameObject ) : Uint32
	{
		return m_payAmount;
	}

	public const override function GetBluelinePart( playerObject : GameObject ) : BluelinePart
	{
		var part : PaymentBluelinePart;
		part = new PaymentBluelinePart;
		part.m_playerMoney = GameInstance.GetTransactionSystem( playerObject.GetGame() ).GetItemQuantity( playerObject, MarketSystem.Money() );
		part.m_paymentMoney = ( ( Int32 )( m_payAmount ) );
		part.passed = Evaluate( playerObject );
		part.captionIconRecordId = T"ChoiceCaptionParts.OpenVendorIcon";
		return part;
	}

	public quest const override function ToString() : String
	{
		var outString : String;
		outString = IntToString( ( ( Int32 )( m_payAmount ) ) );
		return outString;
	}

}

