class SampleUITextSystemController extends inkGameController
{
	private editable var m_locKeyTextWidget : inkTextRef;
	private editable var m_localizedTextWidget : inkTextRef;
	private var m_textParams : inkTextParams;
	private editable var m_numberTextWidget : inkTextRef;
	private editable var m_numberIncreaseButton : inkWidgetRef;
	private editable var m_numberDecreaseButton : inkWidgetRef;
	private var m_numberToInject : Int32;
	default m_numberToInject = 1;
	private editable var m_stringTextInputWidget : inkTextInputRef;
	private var m_stringToInject : String;
	default m_stringToInject = "Dex";
	private editable var m_timeRefreshButton : inkWidgetRef;
	private const editable var m_measurementWidgets : array< inkWidgetRef >;
	private editable var m_metricSystemButton : inkWidgetRef;
	private editable var m_imperialSystemButton : inkWidgetRef;
	private editable var m_animateTextOffsetButton : inkWidgetRef;
	private editable var m_textOffsetWidget : inkTextRef;
	private editable var m_animateTextReplaceButton : inkWidgetRef;
	private editable var m_textReplaceWidget : inkTextRef;
	private editable var m_animateValueButton : inkWidgetRef;
	private editable var m_animateValueWidget : inkTextRef;

	protected event OnInitialize()
	{
		InitControls();
		InitTextParams();
	}

	private function InitControls()
	{
		inkTextRef.SetText( m_numberTextWidget, IntToString( m_numberToInject ) );
		inkWidgetRef.RegisterToCallback( m_numberIncreaseButton, 'OnRelease', this, 'OnIncreaseNumberToInject' );
		inkWidgetRef.RegisterToCallback( m_numberDecreaseButton, 'OnRelease', this, 'OnDecreaseNumberToInject' );
		inkTextInputRef.RegisterToCallback( m_stringTextInputWidget, 'OnTextChanged', this, 'OnChangeTextToInject' );
		inkWidgetRef.RegisterToCallback( m_timeRefreshButton, 'OnRelease', this, 'OnRefreshTime' );
		inkWidgetRef.RegisterToCallback( m_metricSystemButton, 'OnRelease', this, 'OnSwitchToMetricSystem' );
		inkWidgetRef.RegisterToCallback( m_imperialSystemButton, 'OnRelease', this, 'OnSwitchToImperialSystem' );
		inkWidgetRef.RegisterToCallback( m_animateTextOffsetButton, 'OnRelease', this, 'OnAnimateTextOffset' );
		inkWidgetRef.RegisterToCallback( m_animateTextReplaceButton, 'OnRelease', this, 'OnAnimateTextReplace' );
		inkWidgetRef.RegisterToCallback( m_animateValueButton, 'OnRelease', this, 'OnAnimateValue' );
	}

	private function InitTextParams()
	{
		var fakeLocKey : CName;
		fakeLocKey = 'My name is {player_name}, I am Level {player_level,number,integer}, and the time is {curr_date,time,short}';
		inkTextRef.SetLocalizedTextScript( m_locKeyTextWidget, fakeLocKey );
		m_textParams = new inkTextParams;
		m_textParams.AddNumber( "player_level", m_numberToInject );
		m_textParams.AddString( "player_name", m_stringToInject );
		m_textParams.AddCurrentDate( "curr_date" );
		inkTextRef.SetLocalizedTextScript( m_localizedTextWidget, fakeLocKey, m_textParams );
	}

	private function UpdateNumberParam( value : Int32 )
	{
		m_numberToInject = value;
		inkTextRef.SetText( m_numberTextWidget, IntToString( m_numberToInject ) );
		if( m_textParams )
		{
			m_textParams.UpdateNumber( "player_level", m_numberToInject );
		}
	}

	private function UpdateStringParam( const value : ref< String > )
	{
		m_stringToInject = value;
		if( m_textParams )
		{
			m_textParams.UpdateString( "player_name", m_stringToInject );
		}
	}

	private function UpdateTimeParam()
	{
		if( m_textParams )
		{
			m_textParams.UpdateCurrentDate( "curr_date" );
		}
	}

	private function UpdateMeasurementSystem( system : EMeasurementSystem )
	{
		var controller : SampleUIMeasurementController;
		var i, total : Int32;
		total = m_measurementWidgets.Size();
		for( i = 0; i < total; i += 1 )
		{
			controller = ( ( SampleUIMeasurementController )( inkWidgetRef.GetController( m_measurementWidgets[ i ] ) ) );
			controller.SetMeasurementSystem( system );
		}
	}

	protected event OnIncreaseNumberToInject( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			UpdateNumberParam( m_numberToInject + 1 );
		}
	}

	protected event OnDecreaseNumberToInject( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			UpdateNumberParam( m_numberToInject - 1 );
		}
	}

	protected event OnChangeTextToInject( str : String )
	{
		UpdateStringParam( str );
	}

	protected event OnRefreshTime( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			UpdateTimeParam();
		}
	}

	protected event OnSwitchToMetricSystem( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			UpdateMeasurementSystem( EMeasurementSystem.Metric );
		}
	}

	protected event OnSwitchToImperialSystem( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			UpdateMeasurementSystem( EMeasurementSystem.Imperial );
		}
	}

	protected event OnAnimateTextOffset( e : inkPointerEvent )
	{
		var controller : inkTextOffsetController;
		if( e.IsAction( 'click' ) )
		{
			controller = ( ( inkTextOffsetController )( inkTextRef.GetController( m_textOffsetWidget ) ) );
			controller.PlaySetAnimation();
		}
	}

	protected event OnAnimateTextReplace( e : inkPointerEvent )
	{
		var controller : inkTextReplaceController;
		if( e.IsAction( 'click' ) )
		{
			controller = ( ( inkTextReplaceController )( inkTextRef.GetController( m_textReplaceWidget ) ) );
			controller.PlaySetAnimation();
		}
	}

	protected event OnAnimateValue( e : inkPointerEvent )
	{
		var controller : inkTextValueProgressController;
		if( e.IsAction( 'click' ) )
		{
			controller = ( ( inkTextValueProgressController )( inkTextRef.GetController( m_animateValueWidget ) ) );
			controller.PlaySetAnimation();
		}
	}

}

class SampleUIMeasurementController extends inkLogicController
{
	private editable var m_value : Float;
	private editable var m_unit : EMeasurementUnit;
	private editable var m_valueText : inkTextRef;
	private editable var m_unitText : inkTextRef;
	private editable var m_valueIncreaseButton : inkWidgetRef;
	private editable var m_valueDecreaseButton : inkWidgetRef;

	protected event OnInitialize()
	{
		inkWidgetRef.RegisterToCallback( m_valueIncreaseButton, 'OnRelease', this, 'OnIncreaseValue' );
		inkWidgetRef.RegisterToCallback( m_valueDecreaseButton, 'OnRelease', this, 'OnDecreaseValue' );
		UpdateTextWidgets();
	}

	public function SetMeasurementSystem( system : EMeasurementSystem )
	{
		m_value = MeasurementUtils.ValueToSystem( m_value, m_unit, system );
		m_unit = MeasurementUtils.UnitToSystem( m_unit, system );
		UpdateTextWidgets();
	}

	private function UpdateTextWidgets()
	{
		inkTextRef.SetText( m_valueText, FormatValue( m_value ) );
		inkTextRef.SetLocalizedTextScript( m_unitText, MeasurementUtils.GetUnitLocalizationKey( m_unit ) );
	}

	private function FormatValue( value : Float ) : String
	{
		var valueStr : String;
		var endIdx : Int32;
		valueStr = FloatToString( value );
		endIdx = StrFindLast( valueStr, "." ) + 3;
		if( endIdx < StrLen( valueStr ) )
		{
			return StrLeft( valueStr, endIdx );
		}
		return valueStr;
	}

	protected event OnIncreaseValue( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			m_value += 1.0;
			UpdateTextWidgets();
		}
	}

	protected event OnDecreaseValue( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			m_value -= 1.0;
			UpdateTextWidgets();
		}
	}

}

