class RadialMenuItemController extends inkLogicController
{
	var m_menuData : MenuData;
	private editable var m_label : inkTextRef;
	private editable var m_icon : inkImageRef;
	private editable var m_frameHovered : inkWidgetRef;
	private editable var m_hoverPanel : inkWidgetRef;
	private editable var m_background : inkWidgetRef;
	private editable var m_levelFlag : inkWidgetRef;
	private editable var m_attrFlag : inkWidgetRef;
	private editable var m_attrText : inkTextRef;
	private editable var m_perkFlag : inkWidgetRef;
	private editable var m_perkText : inkTextRef;
	private editable var m_queueHoverEvents : Bool;
	private editable var m_disableClick : Bool;
	private editable var m_applyHoverState : Bool;
	private var m_itemHovered : Bool;
	private var m_panelHovered : Bool;
	private var m_panelTransitionProxy : inkAnimProxy;
	private var m_buttonTransitionProxy : inkAnimProxy;
	private var m_isPanelShown : Bool;
	private var m_isDimmed : Bool;
	private var m_isHyperlink : Bool;

	protected event OnInitialize()
	{
		RegisterToCallback( 'OnRelease', this, 'OnMenuChangeRelease' );
		RegisterToCallback( 'OnHoverOver', this, 'OnItemHoverOver' );
		RegisterToCallback( 'OnHoverOut', this, 'OnItemHoverOut' );
		inkWidgetRef.SetOpacity( m_frameHovered, 0.0 );
	}

	protected event OnUninitialize()
	{
		UnregisterFromCallback( 'OnRelease', this, 'OnMenuChangeRelease' );
		UnregisterFromCallback( 'OnHoverOver', this, 'OnItemHoverOver' );
		UnregisterFromCallback( 'OnHoverOut', this, 'OnItemHoverOut' );
		if( inkWidgetRef.IsValid( m_hoverPanel ) )
		{
			UnregisterFromCallback( 'OnHoverOver', this, 'OnHoverPanelOver' );
			UnregisterFromCallback( 'OnHoverOut', this, 'OnHoverPanelOut' );
		}
	}

	public function Init( const menuData : ref< MenuData > )
	{
		m_menuData = menuData;
		inkTextRef.SetText( m_label, menuData.label );
		inkWidgetRef.SetVisible( m_attrFlag, menuData.attrFlag );
		inkTextRef.SetText( m_attrText, IntToString( menuData.attrText ) );
		inkWidgetRef.SetVisible( m_perkFlag, menuData.perkFlag );
		inkTextRef.SetText( m_perkText, IntToString( menuData.perkText ) );
		inkWidgetRef.SetVisible( m_levelFlag, menuData.attrFlag || menuData.perkFlag );
		if( menuData.icon != '' )
		{
			inkImageRef.SetVisible( m_icon, true );
			inkImageRef.SetTexturePart( m_icon, menuData.icon );
		}
		else
		{
			inkImageRef.SetVisible( m_icon, false );
		}
		if( m_menuData.disabled )
		{
			GetRootWidget().SetOpacity( 0.30000001 );
			inkImageRef.SetOpacity( m_icon, 0.1 );
			inkTextRef.SetOpacity( m_label, 0.1 );
		}
	}

	public function UpdateButton( const label : ref< String >, iconTweak : TweakDBID )
	{
		inkTextRef.SetText( m_label, label );
		inkImageRef.SetVisible( m_icon, true );
		InkImageUtils.RequestSetImage( this, m_icon, iconTweak );
	}

	public function SetHyperlink( value : Bool )
	{
		m_isHyperlink = value;
	}

	public function IsHyperlink() : Bool
	{
		return m_isHyperlink;
	}

	public function SetHoverPanel( hoverPanel : inkWidgetRef )
	{
		m_hoverPanel = hoverPanel;
		if( inkWidgetRef.IsValid( m_hoverPanel ) )
		{
			inkWidgetRef.SetVisible( m_hoverPanel, false );
			inkWidgetRef.RegisterToCallback( m_hoverPanel, 'OnEnter', this, 'OnHoverPanelOver' );
			inkWidgetRef.RegisterToCallback( m_hoverPanel, 'OnLeave', this, 'OnHoverPanelOut' );
		}
	}

	protected event OnMenuChangeRelease( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) && !( m_disableClick ) )
		{
			RequestMenuSelect();
		}
	}

	public function RequestMenuSelect()
	{
		var evt : RadialSelectMenuRequest;
		var menuNotification : UIMenuNotificationEvent;
		if( !( m_menuData.disabled ) )
		{
			PlaySound( 'Button', 'OnPress' );
			evt = new RadialSelectMenuRequest;
			evt.m_eventData = this;
			QueueEvent( evt );
		}
		else
		{
			menuNotification = new UIMenuNotificationEvent;
			menuNotification.m_notificationType = UIMenuNotificationType.InventoryActionBlocked;
			QueueEvent( menuNotification );
		}
	}

	protected event OnHoverPanelOver( evt : inkPointerEvent )
	{
		m_panelHovered = true;
		UpdateState();
		UpdateDim( true );
	}

	protected event OnHoverPanelOut( evt : inkPointerEvent )
	{
		m_panelHovered = false;
		UpdateState();
		UpdateDim( false );
	}

	protected event OnItemHoverOver( evt : inkPointerEvent )
	{
		var hoverOverEvent : MenuButtonHoverOverEvent;
		if( !( m_menuData.disabled ) )
		{
			if( m_applyHoverState )
			{
				GetRootWidget().SetState( 'Hover' );
			}
			inkWidgetRef.SetOpacity( m_frameHovered, 1.0 );
			inkWidgetRef.SetOpacity( m_background, 0.5 );
			m_itemHovered = true;
			UpdateState();
		}
		if( m_queueHoverEvents )
		{
			hoverOverEvent = new MenuButtonHoverOverEvent;
			hoverOverEvent.buttonId = m_menuData.identifier;
			QueueEvent( hoverOverEvent );
		}
	}

	protected event OnItemHoverOut( evt : inkPointerEvent )
	{
		var hoverOutEvent : MenuButtonHoverOutEvent;
		if( !( m_menuData.disabled ) )
		{
			if( m_applyHoverState )
			{
				GetRootWidget().SetState( 'Default' );
			}
			inkWidgetRef.SetOpacity( m_frameHovered, 0.0 );
			inkWidgetRef.SetOpacity( m_background, 1.0 );
			m_itemHovered = false;
			UpdateState();
		}
		if( m_queueHoverEvents )
		{
			hoverOutEvent = new MenuButtonHoverOutEvent;
			hoverOutEvent.buttonId = m_menuData.identifier;
			QueueEvent( hoverOutEvent );
		}
	}

	private function UpdateState()
	{
		var evtDelay : MenuItemDelayedUpdate;
		evtDelay = new MenuItemDelayedUpdate;
		QueueEvent( evtDelay );
	}

	private function UpdateDim( value : Bool )
	{
		var evtDim : MenuItemDimRequest;
		evtDim = new MenuItemDimRequest;
		evtDim.m_dim = value;
		QueueEvent( evtDim );
	}

	protected event OnMenuItemDimRequest( evt : MenuItemDimRequest )
	{
		var buttonDimmed : Bool;
		buttonDimmed = ( ( !( m_panelHovered ) && !( m_itemHovered ) ) && ( m_menuData.parentIdentifier == -1 ) ) && evt.m_dim;
		if( m_isDimmed != buttonDimmed )
		{
			m_isDimmed = buttonDimmed;
			if( m_buttonTransitionProxy != NULL )
			{
				m_buttonTransitionProxy.Stop( true );
				m_buttonTransitionProxy = NULL;
			}
			if( m_isDimmed )
			{
				m_buttonTransitionProxy = PlayLibraryAnimationOnAutoSelectedTargets( 'button_dim', GetRootWidget() );
			}
			else
			{
				m_buttonTransitionProxy = PlayLibraryAnimationOnAutoSelectedTargets( 'button_default', GetRootWidget() );
			}
		}
	}

	protected event OnMenuItemDelayedUpdate( evt : MenuItemDelayedUpdate )
	{
		var panelVisibility : Bool;
		if( inkWidgetRef.IsValid( m_hoverPanel ) )
		{
			panelVisibility = m_itemHovered || m_panelHovered;
			if( panelVisibility != m_isPanelShown )
			{
				m_isPanelShown = panelVisibility;
				if( m_panelTransitionProxy != NULL )
				{
					m_panelTransitionProxy.UnregisterFromCallback( inkanimEventType.OnFinish, this, 'OnOutroFinished' );
					m_panelTransitionProxy.Stop( true );
					m_panelTransitionProxy = NULL;
				}
				if( m_isPanelShown )
				{
					inkWidgetRef.SetVisible( m_hoverPanel, true );
					m_panelTransitionProxy = PlayLibraryAnimationOnAutoSelectedTargets( 'buttons_panel_show', inkWidgetRef.Get( m_hoverPanel ) );
				}
				else
				{
					m_panelTransitionProxy = PlayLibraryAnimationOnAutoSelectedTargets( 'buttons_panel_hide', inkWidgetRef.Get( m_hoverPanel ) );
					m_panelTransitionProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnOutroFinished' );
				}
			}
		}
	}

	protected event OnOutroFinished( anim : inkAnimProxy )
	{
		inkWidgetRef.SetVisible( m_hoverPanel, false );
	}

}

class MenuButtonHoverOverEvent extends Event
{
	var buttonId : Int32;
}

class MenuButtonHoverOutEvent extends Event
{
	var buttonId : Int32;
}

