class ScannerSkillCheckLogicController extends inkLogicController
{
	private editable var m_ScannerSkillCheckItemName : CName;
	default m_ScannerSkillCheckItemName = 'SkillCheckItem';
	private var m_SkillCheckObjects : array< weak< inkWidget > >;
	var m_Root : weak< inkCompoundWidget >;

	protected event OnInitialize()
	{
		m_Root = ( ( inkCompoundWidget )( GetRootWidget() ) );
	}

	protected event OnUninitialize()
	{
		var i, limit : Int32;
		var currObject : weak< inkWidget >;
		for( i = 0, limit = m_SkillCheckObjects.Size(); i < limit; i += 1 )
		{
			currObject = m_SkillCheckObjects[ i ];
			m_Root.RemoveChild( currObject );
		}
		m_SkillCheckObjects.Clear();
	}

	public function Setup( const skillCheckInfo : ref< array< UIInteractionSkillCheck > > )
	{
		var i, limit, sizeSkill, sizeList : Int32;
		var currObject : weak< inkWidget >;
		var currLogic : ScannerSkillCheckItemLogicController;
		sizeSkill = skillCheckInfo.Size();
		sizeList = m_SkillCheckObjects.Size();
		for( i = 0, limit = Max( sizeSkill, sizeList ); i < limit; i += 1 )
		{
			if( i < sizeSkill )
			{
				if( i < sizeList )
				{
					currObject = m_SkillCheckObjects[ i ];
				}
				else
				{
					currObject = CreateSkillCheckObject();
				}
				currLogic = ( ( ScannerSkillCheckItemLogicController )( currObject.GetController() ) );
				if( currLogic )
				{
					currLogic.Setup( skillCheckInfo[ i ] );
				}
			}
			else
			{
				m_Root.RemoveChild( m_SkillCheckObjects.PopBack() );
			}
		}
		m_Root.SetVisible( m_SkillCheckObjects.Size() > 0 );
	}

	private function CreateSkillCheckObject() : weak< inkWidget >
	{
		var newObject : weak< inkWidget >;
		newObject = ( ( inkWidget )( SpawnFromLocal( m_Root, m_ScannerSkillCheckItemName ) ) );
		m_SkillCheckObjects.PushBack( newObject );
		return newObject;
	}

}

class ScannerSkillCheckItemLogicController extends inkLogicController
{
	private editable var m_NameRef : inkTextRef;
	private editable var m_ConditionDataListRef : inkCompoundRef;
	private var m_ConditionDataItems : array< weak< inkWidget > >;
	private editable var m_ConditionDataItemName : CName;
	default m_ConditionDataItemName = 'ConditionDataItem';
	private editable var m_PassedStateName : CName;
	default m_PassedStateName = 'Passed';
	private editable var m_FailedStateName : CName;
	default m_FailedStateName = 'Failed';

	protected event OnUninitialize()
	{
		var i, limit : Int32;
		var currObject : weak< inkWidget >;
		for( i = 0, limit = m_ConditionDataItems.Size(); i < limit; i += 1 )
		{
			currObject = m_ConditionDataItems[ i ];
			inkCompoundRef.RemoveChild( m_ConditionDataListRef, currObject );
		}
		m_ConditionDataItems.Clear();
	}

	public function Setup( const skillCheck : ref< UIInteractionSkillCheck > )
	{
		var i, limit, sizeSkill, sizeList : Int32;
		var currObject : weak< inkWidget >;
		var currLogic : ScannerSkillCheckConditionDataItemLogicController;
		ConstructName( skillCheck );
		GetRootWidget().SetState( ( ( skillCheck.isPassed ) ? ( m_PassedStateName ) : ( m_FailedStateName ) ) );
		sizeSkill = skillCheck.additionalRequirements.Size();
		sizeList = m_ConditionDataItems.Size();
		for( i = 0, limit = Max( sizeSkill, sizeList ); i < limit; i += 1 )
		{
			if( i < sizeSkill )
			{
				if( i < sizeList )
				{
					currObject = m_ConditionDataItems[ i ];
				}
				else
				{
					currObject = CreateConditionDataObject();
				}
				currLogic = ( ( ScannerSkillCheckConditionDataItemLogicController )( currObject.GetController() ) );
				if( currLogic )
				{
					currLogic.Setup( skillCheck.additionalRequirements[ i ], skillCheck.additionalReqOperator );
				}
			}
			else
			{
				inkCompoundRef.RemoveChild( m_ConditionDataListRef, m_ConditionDataItems.PopBack() );
			}
		}
	}

	private function CreateConditionDataObject() : weak< inkWidget >
	{
		var newObject : weak< inkWidget >;
		newObject = ( ( inkWidget )( SpawnFromLocal( inkCompoundRef.Get( m_ConditionDataListRef ), m_ConditionDataItemName ) ) );
		m_ConditionDataItems.PushBack( newObject );
		return newObject;
	}

	private function ConstructName( const skillCheck : ref< UIInteractionSkillCheck > )
	{
		var skillName : String;
		var hasActionName : Bool;
		hasActionName = StrLen( skillCheck.actionDisplayName ) > 0;
		skillName = "";
		if( hasActionName )
		{
			skillName += ( skillCheck.actionDisplayName + " (" );
		}
		skillName += ( ( GetLocalizedText( skillCheck.skillName ) + " " ) + ( ( String )( skillCheck.requiredSkill ) ) );
		if( hasActionName )
		{
			skillName += ")";
		}
		inkTextRef.SetLetterCase( m_NameRef, textLetterCase.UpperCase );
		inkTextRef.SetText( m_NameRef, skillName );
	}

}

class ScannerSkillCheckConditionDataItemLogicController extends inkLogicController
{
	private editable var m_ConditionDataDescriptionName : CName;
	default m_ConditionDataDescriptionName = 'ConditionDataDescription';
	private editable var m_ParentConditionTextPath : inkWidgetPath;
	private editable var m_OwnConditionTextPath : inkWidgetPath;
	private editable var m_ConditionDescriptionListPath : inkWidgetPath;
	private var m_ConditionDescriptions : array< weak< inkWidget > >;
	private var m_ParentConditionText : weak< inkText >;
	private var m_OwnConditionText : weak< inkText >;
	private var m_ConditionDescriptionList : weak< inkCompoundWidget >;

	protected event OnInitialize()
	{
		m_ParentConditionText = ( ( inkText )( GetWidget( m_ParentConditionTextPath ) ) );
		m_OwnConditionText = ( ( inkText )( GetWidget( m_OwnConditionTextPath ) ) );
		m_ConditionDescriptionList = ( ( inkCompoundWidget )( GetWidget( m_ConditionDescriptionListPath ) ) );
	}

	protected event OnUninitialize()
	{
		var i, limit : Int32;
		var currObject : weak< inkWidget >;
		for( i = 0, limit = m_ConditionDescriptions.Size(); i < limit; i += 1 )
		{
			currObject = m_ConditionDescriptions[ i ];
			m_ConditionDescriptionList.RemoveChild( currObject );
		}
		m_ConditionDescriptions.Clear();
	}

	public function Setup( const conditionData : ref< ConditionData >, parentOperator : ELogicOperator )
	{
		var i, limit, sizeSkill, sizeList, numPassed : Int32;
		var currObject : weak< inkWidget >;
		var currLogic : ScannerSkillCheckConditionDescriptionLogicController;
		sizeSkill = conditionData.requirementList.Size();
		sizeList = m_ConditionDescriptions.Size();
		numPassed = 0;
		for( i = 0, limit = Max( sizeSkill, sizeList ); i < limit; i += 1 )
		{
			if( i < sizeSkill )
			{
				if( i < sizeList )
				{
					currObject = m_ConditionDescriptions[ i ];
				}
				else
				{
					currObject = CreateConditionDescriptionObject();
				}
				currLogic = ( ( ScannerSkillCheckConditionDescriptionLogicController )( currObject.GetController() ) );
				if( currLogic )
				{
					currLogic.Setup( conditionData.requirementList[ i ] );
					if( conditionData.requirementList[ i ].passed )
					{
						numPassed += 1;
					}
				}
			}
			else
			{
				m_ConditionDescriptionList.RemoveChild( m_ConditionDescriptions.PopBack() );
			}
		}
		ConstructTexts( conditionData.conditionOperator, parentOperator, numPassed, sizeSkill );
	}

	private function CreateConditionDescriptionObject() : weak< inkWidget >
	{
		var newObject : weak< inkWidget >;
		newObject = ( ( inkWidget )( SpawnFromLocal( m_ConditionDescriptionList, m_ConditionDataDescriptionName ) ) );
		m_ConditionDescriptions.PushBack( newObject );
		return newObject;
	}

	private function ConstructTexts( ownOperator : ELogicOperator, parentOperator : ELogicOperator, passed : Int32, total : Int32 )
	{
		var conditionText : String;
		m_ParentConditionText.SetLetterCase( textLetterCase.UpperCase );
		m_ParentConditionText.SetText( ( ( String )( parentOperator ) ) );
		conditionText = "(any 1 needed)";
		if( ownOperator == ELogicOperator.AND )
		{
			conditionText = ( ( ( "(" + passed ) + "/" ) + total ) + ")";
		}
		m_OwnConditionText.SetLetterCase( textLetterCase.UpperCase );
		m_OwnConditionText.SetText( conditionText );
	}

}

class ScannerSkillCheckConditionDescriptionLogicController extends inkLogicController
{
	private editable var m_NameRef : inkTextRef;
	private editable var m_PassedStateName : CName;
	default m_PassedStateName = 'Passed';
	private editable var m_FailedStateName : CName;
	default m_FailedStateName = 'Failed';

	public function Setup( const condition : ref< Condition > )
	{
		GetRootWidget().SetState( ( ( condition.passed ) ? ( m_PassedStateName ) : ( m_FailedStateName ) ) );
		inkTextRef.SetLocalizedTextScript( m_NameRef, condition.description );
	}

}

