class GogRewardsListController extends inkLogicController
{
	private editable var m_containerWidget : inkWidgetRef;
	private editable var m_scrollArea : inkWidgetRef;
	private editable var m_sizeRefWrapper : inkWidgetRef;
	private editable var m_scrollBarRequiredHeight : Int32;
	private var m_shouldUpdateLayout : Bool;

	public function UpdateRewardsList( const rewards : array< GogRewardEntryData > )
	{
		var i, limit : Int32;
		var groupWidget : weak< inkWidget >;
		var groupController : weak< GogRewardsGroupController >;
		var groupsNames : array< CName >;
		var groups : array< array< GogRewardEntryData > >;
		groupsNames.PushBack( 'default' );
		groupsNames.PushBack( 'ep1' );
		groupsNames.PushBack( 'twitch' );
		groupsNames.PushBack( 'amazon' );
		groups.Grow( groupsNames.Size() );
		for( i = 0, limit = rewards.Size(); i < limit; i += 1 )
		{
			groups[ GetGroupIndex( rewards[ i ].group ) ].PushBack( rewards[ i ] );
		}
		for( i = 0, limit = groups.Size(); i < limit; i += 1 )
		{
			if( groups[ i ].Size() > 0 )
			{
				groupWidget = SpawnFromLocal( inkWidgetRef.Get( m_containerWidget ), 'RewardGroupWidget' );
				groupController = ( ( GogRewardsGroupController )( groupWidget.GetController() ) );
				groupController.UpdateGroup( groupsNames[ i ], groups[ i ] );
			}
		}
		m_shouldUpdateLayout = true;
	}

	private function GetGroupIndex( const groupName : CName ) : Int32
	{
		if( groupName == 'ep1' )
		{
			return 1;
		}
		if( groupName == 'twitch' )
		{
			return 2;
		}
		if( groupName == 'amazon' )
		{
			return 3;
		}
		return 0;
	}

	protected event OnArrangeChildrenComplete()
	{
		if( m_shouldUpdateLayout )
		{
			QueueEvent( new DelayedUpdateLayoutEvent );
			m_shouldUpdateLayout = false;
		}
	}

	protected event OnDelayedUpdateLayoutEvent( evt : DelayedUpdateLayoutEvent )
	{
		var height : Float;
		height = inkWidgetRef.GetDesiredHeight( m_sizeRefWrapper );
		if( height < ( ( Float )( m_scrollBarRequiredHeight ) ) )
		{
			inkWidgetRef.SetHeight( m_scrollArea, height );
		}
		QueueEvent( new DelayedUpdateLayoutCompletedEvent );
	}

}

class DelayedUpdateLayoutEvent extends Event
{
}

class DelayedUpdateLayoutCompletedEvent extends Event
{
}

class GogRewardsGroupController extends inkLogicController
{
	private editable var m_label : inkTextRef;
	private editable var m_containerWidget : inkWidgetRef;

	public function UpdateGroup( const groupName : CName, const rewards : array< GogRewardEntryData > )
	{
		var i, limit : Int32;
		var entryWidget : weak< inkWidget >;
		var entryController : weak< GogRewardEntryController >;
		var state : CName;
		state = GroupNameToState( groupName );
		inkTextRef.SetText( m_label, GroupNameToLabelText( groupName ) );
		inkTextRef.SetState( m_label, state );
		for( i = 0, limit = rewards.Size(); i < limit; i += 1 )
		{
			entryWidget = SpawnFromLocal( inkWidgetRef.Get( m_containerWidget ), ( ( rewards[ i ].slotType == 'weapon' ) ? ( 'WideRewardEntry' ) : ( 'RewardEntry' ) ) );
			entryController = ( ( GogRewardEntryController )( entryWidget.GetController() ) );
			entryController.UpdateRewardDetails( rewards[ i ].icon, ( ( rewards[ i ].isUnlocked ) ? ( state ) : ( 'Airdrop' ) ), rewards[ i ].isUnlocked, rewards[ i ].slotType == 'outfit' );
			entryWidget.RegisterToCallback( 'OnHoverOver', this, 'OnEntryHoverOver' );
			entryWidget.RegisterToCallback( 'OnHoverOut', this, 'OnEntryHoverOut' );
		}
	}

	protected event OnEntryHoverOver( evt : inkPointerEvent )
	{
		var hoverEvent : GogRewardsEntryHoverOver;
		hoverEvent = new GogRewardsEntryHoverOver;
		hoverEvent.widget = evt.GetCurrentTarget();
		hoverEvent.controller = ( ( weak< weak< GogRewardEntryController > > )( hoverEvent.widget.GetController() ) );
		QueueEvent( hoverEvent );
	}

	protected event OnEntryHoverOut( evt : inkPointerEvent )
	{
		QueueEvent( new GogRewardsEntryHoverOut );
	}

	private function GroupNameToLabelText( const groupName : CName ) : String
	{
		if( groupName == 'ep1' )
		{
			return "LocKey#93674";
		}
		if( groupName == 'twitch' )
		{
			return "LocKey#94328";
		}
		if( groupName == 'amazon' )
		{
			return "LocKey#94947";
		}
		return "LocKey#15435";
	}

	private function GroupNameToState( const groupName : CName ) : CName
	{
		if( groupName == 'ep1' )
		{
			return 'EP1';
		}
		if( groupName == 'twitch' )
		{
			return 'Twitch';
		}
		if( groupName == 'amazon' )
		{
			return 'Amazon';
		}
		return 'Default';
	}

}

class GogRewardsEntryHoverOver extends Event
{
	var widget : weak< inkWidget >;
	var controller : weak< GogRewardEntryController >;
}

class GogRewardsEntryHoverOut extends Event
{
}

