class VehicleDetailsLogicController extends inkLogicController
{
	private editable var m_backButton : inkWidgetRef;
	private editable var m_purchaseButton : inkWidgetRef;
	private editable var m_ownedWidget : inkWidgetRef;
	private editable var m_insufficientMoneyWidget : inkWidgetRef;
	private editable var m_detailsImage : inkImageRef;
	private editable var m_vehicleNameText : inkTextRef;
	private editable var m_detailsText : inkTextRef;
	private editable var m_scrollControllerWidget : inkWidgetRef;
	private editable var m_gunImage : inkImageRef;
	private editable var m_rocketImage : inkImageRef;
	private editable var m_priceWrapper : inkWidgetRef;
	private editable var m_priceText : inkTextRef;
	private editable var m_discountWrapper : inkWidgetRef;
	private editable var m_discountText : inkTextRef;
	private editable var m_originalPriceWrapper : inkWidgetRef;
	private editable var m_originalPriceText : inkTextRef;
	private editable var m_discountImageWrapper : inkWidgetRef;
	private editable var m_howToUnlockWrapper : inkWidgetRef;
	private editable var m_howToUnlockText : inkTextRef;
	private var m_offerRecord : weak< VehicleOffer_Record >;
	private var m_price : Int32;
	private var m_discount : Float;

	protected event OnInitialize()
	{
		SetUpButtons();
	}

	protected event OnUninitialize()
	{
		UnregisterButtons();
	}

	public function SetUp( offerRecord : weak< VehicleOffer_Record >, state : EVehicleOfferState, playerMoney : Int32, optional discount : Float )
	{
		var discountApplicable, owned : Bool;
		var originalPrice : Int32;
		owned = state == EVehicleOfferState.Owned;
		m_offerRecord = offerRecord;
		discountApplicable = m_offerRecord.DiscountApplicable();
		m_discount = ( ( discountApplicable ) ? ( discount ) : ( 0.0 ) );
		originalPrice = m_offerRecord.Price().OverrideValue();
		m_price = ( ( owned || !( discountApplicable ) ) ? ( originalPrice ) : ( VehicleShopUtils.GetDiscountedPrice( originalPrice, discount ) ) );
		inkTextRef.SetText( m_vehicleNameText, m_offerRecord.Name() );
		inkTextRef.SetText( m_detailsText, m_offerRecord.Description() );
		inkImageRef.SetAtlasResource( m_detailsImage, m_offerRecord.PreviewImage().AtlasResourcePath() );
		inkImageRef.SetTexturePart( m_detailsImage, m_offerRecord.PreviewImage().AtlasPartName() );
		inkImageRef.SetVisible( m_gunImage, m_offerRecord.HasMachineGun() );
		inkImageRef.SetVisible( m_rocketImage, m_offerRecord.HasRocketLauncher() );
		inkTextRef.SetText( m_discountText, FloatToStringPrec( m_discount * 100.0, 0 ) );
		inkTextRef.SetText( m_originalPriceText, IntToString( originalPrice ) );
		inkTextRef.SetText( m_priceText, IntToString( m_price ) );
		SetUpHowToUnlockText( m_offerRecord.UnlockType().Type() );
		( ( inkScrollController )( inkWidgetRef.GetController( m_scrollControllerWidget ) ) ).SetScrollPosition( 0.0 );
		UpdateState( state, playerMoney );
	}

	public function UpdateState( state : EVehicleOfferState, playerMoney : Int32 )
	{
		inkWidgetRef.SetVisible( m_priceWrapper, state != EVehicleOfferState.Owned && state != EVehicleOfferState.Locked );
		inkWidgetRef.SetVisible( m_ownedWidget, state == EVehicleOfferState.Owned );
		inkWidgetRef.SetVisible( m_purchaseButton, ( state != EVehicleOfferState.Owned && state != EVehicleOfferState.Locked ) && ( playerMoney >= m_price ) );
		inkWidgetRef.SetVisible( m_insufficientMoneyWidget, ( state != EVehicleOfferState.Owned && state != EVehicleOfferState.Locked ) && ( playerMoney < m_price ) );
		inkWidgetRef.SetVisible( m_howToUnlockWrapper, state == EVehicleOfferState.Locked );
		inkImageRef.Get( m_detailsImage ).SetEffectEnabled( inkEffectType.ColorCorrection, 'ColorCorrection_0', state == EVehicleOfferState.Locked );
		UpdateDiscountVisibility( state );
	}

	private function UpdateDiscountVisibility( state : EVehicleOfferState )
	{
		var discountVisible : Bool;
		discountVisible = ( state != EVehicleOfferState.Owned && state != EVehicleOfferState.Locked ) && ( m_discount > 0.0 );
		inkWidgetRef.SetVisible( m_discountWrapper, discountVisible );
		inkWidgetRef.SetVisible( m_originalPriceWrapper, discountVisible );
		inkWidgetRef.SetVisible( m_discountImageWrapper, discountVisible );
	}

	private function SetUpHowToUnlockText( unlockType : gamedataVehicleUnlockType )
	{
		if( unlockType == gamedataVehicleUnlockType.StreetCred )
		{
			inkTextRef.SetText( m_howToUnlockText, GetLocalizedText( "LocKey#94412" ) );
		}
		else if( unlockType == gamedataVehicleUnlockType.CourierMissions )
		{
			inkTextRef.SetText( m_howToUnlockText, GetLocalizedText( "LocKey#94411" ) );
		}
		else
		{
			inkTextRef.SetText( m_howToUnlockText, "" );
		}
	}

	private function SetUpButtons()
	{
		inkWidgetRef.RegisterToCallback( m_backButton, 'OnRelease', this, 'OnBackClicked' );
		inkWidgetRef.RegisterToCallback( m_backButton, 'OnHoverOver', this, 'OnBackHoverOver' );
		inkWidgetRef.RegisterToCallback( m_backButton, 'OnHoverOut', this, 'OnBackHoverOut' );
		inkWidgetRef.RegisterToCallback( m_purchaseButton, 'OnRelease', this, 'OnPurchaseClicked' );
		inkWidgetRef.RegisterToCallback( m_purchaseButton, 'OnHoverOver', this, 'OnPurchaseHoverOver' );
		inkWidgetRef.RegisterToCallback( m_purchaseButton, 'OnHoverOut', this, 'OnPurchaseHoverOut' );
	}

	private function UnregisterButtons()
	{
		inkWidgetRef.UnregisterFromCallback( m_backButton, 'OnRelease', this, 'OnBackClicked' );
		inkWidgetRef.UnregisterFromCallback( m_backButton, 'OnHoverOver', this, 'OnBackHoverOver' );
		inkWidgetRef.UnregisterFromCallback( m_backButton, 'OnHoverOut', this, 'OnBackHoverOut' );
		inkWidgetRef.UnregisterFromCallback( m_purchaseButton, 'OnRelease', this, 'OnPurchaseClicked' );
		inkWidgetRef.UnregisterFromCallback( m_purchaseButton, 'OnHoverOver', this, 'OnPurchaseHoverOver' );
		inkWidgetRef.UnregisterFromCallback( m_purchaseButton, 'OnHoverOut', this, 'OnPurchaseHoverOut' );
	}

	protected event OnBackClicked( evt : inkPointerEvent )
	{
		var backEvt : VehicleShopBackEvent;
		if( evt.IsAction( 'click' ) )
		{
			backEvt = new VehicleShopBackEvent;
			QueueEvent( backEvt );
		}
	}

	protected event OnBackHoverOver( evt : inkPointerEvent )
	{
		inkWidgetRef.SetState( m_backButton, 'Hover' );
	}

	protected event OnBackHoverOut( evt : inkPointerEvent )
	{
		inkWidgetRef.SetState( m_backButton, 'Default' );
	}

	protected event OnPurchaseClicked( evt : inkPointerEvent )
	{
		var purchaseEvt : VehicleShopPurchaseEvent;
		if( evt.IsAction( 'click' ) )
		{
			purchaseEvt = new VehicleShopPurchaseEvent;
			purchaseEvt.m_offerRecord = m_offerRecord;
			QueueEvent( purchaseEvt );
		}
	}

	protected event OnPurchaseHoverOver( evt : inkPointerEvent )
	{
		inkWidgetRef.SetState( m_purchaseButton, 'Hover' );
	}

	protected event OnPurchaseHoverOut( evt : inkPointerEvent )
	{
		inkWidgetRef.SetState( m_purchaseButton, 'Default' );
	}

}

