class PhotoModeGridButton extends inkToggleController
{
	private editable var m_FrameImg : inkImageRef;
	private editable var m_DynamicImg : inkImageRef;
	private editable var m_BgWidget : inkWidgetRef;
	private editable var m_HoverWidget : inkWidgetRef;
	private editable var m_PlusImg : inkImageRef;
	private var m_currentImagePart : CName;
	private var m_atlasRef : ResRef;
	private var m_buttonData : Int32;
	private var m_parentGrid : weak< PhotoModeGridList >;
	private var m_index : Int32;
	private var m_visibleOnGrid : Bool;
	private var m_imageScalingSpeed : Float;
	private var m_opacityScalingSpeed : Float;

	public function Setup( grid : PhotoModeGridList, index : Int32 )
	{
		m_parentGrid = grid;
		m_index = index;
	}

	protected event OnInitialize()
	{
		m_buttonData = -1;
		m_visibleOnGrid = false;
		RegisterToCallback( 'OnRelease', this, 'OnToggleClick' );
		RegisterToCallback( 'OnHoverOver', this, 'OnHovered' );
		RegisterToCallback( 'OnHoverOut', this, 'OnHoverOut' );
		ButtonStateChanged( false );
		m_imageScalingSpeed = 50.0;
		m_opacityScalingSpeed = 3.0;
	}

	protected event OnUninitialize()
	{
		m_parentGrid = NULL;
		UnregisterFromCallback( 'OnRelease', this, 'OnToggleClick' );
		UnregisterFromCallback( 'OnHoverOver', this, 'OnHovered' );
		UnregisterFromCallback( 'OnHoverOut', this, 'OnHoverOut' );
	}

	protected event OnToggleClick( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			m_parentGrid.OnGridButtonAction( m_index );
		}
	}

	protected event OnHovered( e : inkPointerEvent )
	{
		if( !( IsToggledVisually() ) )
		{
			inkWidgetRef.SetVisible( m_HoverWidget, true );
		}
	}

	protected event OnHoverOut( e : inkPointerEvent )
	{
		inkWidgetRef.SetVisible( m_HoverWidget, false );
	}

	public function SetImage( atlasPath : ResRef, imagePart : CName )
	{
		if( imagePart == '' || !( ResRef.IsValid( atlasPath ) ) )
		{
			inkImageRef.SetVisible( m_DynamicImg, false );
			inkImageRef.SetVisible( m_PlusImg, true );
			inkImageRef.SetVisible( m_FrameImg, true );
		}
		else
		{
			inkImageRef.SetVisible( m_DynamicImg, true );
			inkImageRef.SetVisible( m_PlusImg, false );
			inkImageRef.SetVisible( m_FrameImg, false );
		}
		if( m_visibleOnGrid )
		{
			inkImageRef.SetAtlasResource( m_DynamicImg, atlasPath );
			inkImageRef.SetTexturePart( m_DynamicImg, imagePart );
		}
		inkImageRef.SetScale( m_DynamicImg, Vector2( 0.0, 0.0 ) );
		inkImageRef.SetOpacity( m_DynamicImg, 0.1 );
		m_atlasRef = atlasPath;
		m_currentImagePart = imagePart;
	}

	public function SetData( buttonData : Int32 )
	{
		m_buttonData = buttonData;
	}

	public function GetData() : Int32
	{
		return m_buttonData;
	}

	public function IsToggledVisually() : Bool
	{
		return inkWidgetRef.IsVisible( m_BgWidget );
	}

	public function ButtonStateChanged( selected : Bool )
	{
		if( selected )
		{
			inkWidgetRef.SetVisible( m_BgWidget, true );
		}
		else
		{
			inkWidgetRef.SetVisible( m_BgWidget, false );
		}
	}

	public function OnVisibilityOnGridChanged( visible : Bool )
	{
		if( m_visibleOnGrid == visible )
		{
			return;
		}
		if( visible )
		{
			inkImageRef.SetAtlasResource( m_DynamicImg, m_atlasRef );
			inkImageRef.SetTexturePart( m_DynamicImg, m_currentImagePart );
			inkImageRef.SetVisible( m_DynamicImg, true );
		}
		else
		{
			inkImageRef.SetScale( m_DynamicImg, Vector2( 0.0, 0.0 ) );
			inkImageRef.SetOpacity( m_DynamicImg, 0.1 );
			inkImageRef.SetVisible( m_DynamicImg, false );
		}
		m_visibleOnGrid = visible;
	}

	public function UpdateSize( timeDelta : Float )
	{
		var imageSize : Vector2;
		var currentScale : Vector2;
		var scaleX : Float;
		var scaleY : Float;
		var t : Float;
		if( ( m_visibleOnGrid && m_currentImagePart != '' ) && inkImageRef.IsTexturePartExist( m_DynamicImg, m_currentImagePart ) )
		{
			currentScale = inkImageRef.GetScale( m_DynamicImg );
			imageSize = inkImageRef.GetDesiredSize( m_DynamicImg );
			scaleX = imageSize.X / MaxF( imageSize.X, imageSize.Y );
			scaleY = imageSize.Y / MaxF( imageSize.X, imageSize.Y );
			t = ClampF( m_imageScalingSpeed * timeDelta, 0.0, 1.0 );
			inkImageRef.SetScale( m_DynamicImg, Vector2( LerpF( t, currentScale.X, scaleX ), LerpF( t, currentScale.Y, scaleY ) ) );
			inkImageRef.SetOpacity( m_DynamicImg, ClampF( inkImageRef.GetOpacity( m_DynamicImg ) + ( m_opacityScalingSpeed * timeDelta ), 0.0, 1.0 ) );
		}
	}

}

