enum EVehicleOfferState
{
	Default = 0,
	New = 1,
	Owned = 2,
	Locked = 3,
}

class VehicleOfferLogicController extends BaseButtonView
{
	private editable var m_vehicleImage : inkImageRef;
	private editable var m_border : inkWidgetRef;
	private editable var m_ownedIndicator : inkWidgetRef;
	private editable var m_nameText : inkTextRef;
	private editable var m_gunImage : inkImageRef;
	private editable var m_rocketImage : inkImageRef;
	private editable var m_priceTextWrapper : inkWidgetRef;
	private editable var m_priceText : inkTextRef;
	private editable var m_originalPriceTextWrapper : inkWidgetRef;
	private editable var m_originalPriceText : inkTextRef;
	private editable var m_discountedPriceTextWrapper : inkWidgetRef;
	private editable var m_discountedPriceText : inkTextRef;
	private editable var m_discountWrapper : inkWidgetRef;
	private editable var m_discountText : inkTextRef;
	private editable var m_discoutImage : inkWidgetRef;
	private var m_offerRecord : weak< VehicleOffer_Record >;
	private var m_state : EVehicleOfferState;
	private var m_styleWidget : weak< inkWidget >;
	private var m_discount : Float;
	private var m_discountApplicable : Bool;

	protected event OnInitialize()
	{
		m_styleWidget = this.GetRootWidget();
		super.OnInitialize();
	}

	public function SetUp( offerRecord : weak< VehicleOffer_Record >, state : EVehicleOfferState, optional discount : Float )
	{
		var price : Int32;
		m_offerRecord = offerRecord;
		m_discount = discount;
		m_discountApplicable = m_offerRecord.DiscountApplicable();
		price = m_offerRecord.Price().OverrideValue();
		inkWidgetRef.SetVisible( m_ownedIndicator, state == EVehicleOfferState.Owned );
		inkTextRef.SetText( m_nameText, m_offerRecord.Name() );
		inkImageRef.SetAtlasResource( m_vehicleImage, m_offerRecord.PreviewImage().AtlasResourcePath() );
		inkImageRef.SetTexturePart( m_vehicleImage, m_offerRecord.PreviewImage().AtlasPartName() );
		inkImageRef.SetVisible( m_gunImage, m_offerRecord.HasMachineGun() );
		inkImageRef.SetVisible( m_rocketImage, m_offerRecord.HasRocketLauncher() );
		inkTextRef.SetText( m_priceText, IntToString( price ) );
		inkTextRef.SetText( m_originalPriceText, IntToString( price ) );
		inkTextRef.SetText( m_discountedPriceText, IntToString( VehicleShopUtils.GetDiscountedPrice( price, m_discount ) ) );
		inkTextRef.SetText( m_discountText, FloatToStringPrec( m_discount * 100.0, 0 ) );
		UpdateState( state );
	}

	public function UpdateState( state : EVehicleOfferState )
	{
		m_state = state;
		m_styleWidget.SetState( StateValueToName( state ) );
		UpdateDiscountVisibility();
	}

	private function UpdateDiscountVisibility()
	{
		var discountVisible : Bool;
		discountVisible = ( ( m_discountApplicable && ( m_discount > 0.0 ) ) && m_state != EVehicleOfferState.Owned ) && m_state != EVehicleOfferState.Locked;
		inkWidgetRef.SetVisible( m_priceTextWrapper, !( discountVisible ) );
		inkWidgetRef.SetVisible( m_originalPriceTextWrapper, discountVisible );
		inkWidgetRef.SetVisible( m_discountedPriceTextWrapper, discountVisible );
		inkWidgetRef.SetVisible( m_discountWrapper, discountVisible );
		inkWidgetRef.SetVisible( m_discoutImage, discountVisible );
	}

	public function SetHoverState( isHovered : Bool )
	{
		if( isHovered )
		{
			switch( m_state )
			{
				case EVehicleOfferState.New:
					m_styleWidget.SetState( 'HoverNew' );
				break;
				case EVehicleOfferState.Owned:
					m_styleWidget.SetState( 'HoverOwned' );
				break;
				case EVehicleOfferState.Locked:
					m_styleWidget.SetState( 'HoverLocked' );
				break;
				default:
					m_styleWidget.SetState( 'Hover' );
				break;
			}
		}
		else
		{
			UpdateState( ( ( m_state == EVehicleOfferState.New ) ? ( EVehicleOfferState.Default ) : ( m_state ) ) );
		}
	}

	private function StateValueToName( state : EVehicleOfferState ) : CName
	{
		switch( state )
		{
			case EVehicleOfferState.New:
				return 'New';
			case EVehicleOfferState.Owned:
				return 'Owned';
			case EVehicleOfferState.Locked:
				return 'Locked';
			default:
				return 'Default';
		}
	}

	public function GetOfferRecord() : weak< VehicleOffer_Record >
	{
		return m_offerRecord;
	}

	public function GetState() : EVehicleOfferState
	{
		return m_state;
	}

}

