

-- reserve constructor
CRollHoldChoice = 
{
	s_NumChoices = 3
}

function CRollHoldChoice:Create( i_oParent, i_oChoice, index, base )
log( "CREATE" .. index )
    -- inherit all functions from Movie
    local oRet = i_oChoice
    
    CIrpMan:Attach( oRet )
    
	-- bind into our class
	CRollHoldChoice:Bind( oRet )
	
	-- make sure we get updates
	oRet:SetOnUpdateEnabled( true );

	-- initialization
	oRet.parent = i_oParent
	oRet.index = index
	oRet.base = base
	oRet:Init()
	
	return oRet
end

-----------------------------------------------------------------------------------
-- public functions
-----------------------------------------------------------------------------------

function CRollHoldChoice:Init( i_obj )
	
log( "INIT" .. self.index )
	self.rolling = false
	self.hold = false
	
	if ( self.stopX == nil ) then
		self.stopX = self:GetX()
		self.stopY = self:GetY()

		self.height = self:GetHeight()
	end
	
	self.lastY = 0
	
	self:SetFrame( self.base )
	
end

function CRollHoldChoice:SetBase( base, frame )
	self.base = base
	
	self:SetFrame( self.base )
end

function CRollHoldChoice:SetPick( pick )
	-- ignore negative pick, they are just uninitialized
	if ( pick == -1 ) then
		pick = 0
	end
	
	self:SetFrame( self.base + pick )
end

function CRollHoldChoice:Roll( extraTime )
	if ( (self.rolling == true) or (self.parent.playing == false) ) then
		return
	end
	
	self.rolling = true
	
	local oIrp = self:CreateIrp( self.stopX, ( 45 + math.random( -2, 2 ) * 2 ) * self.height, 4 + extraTime, CIrpFuncs.mEaseOutQuad, 0 )
	
	oIrp.m_oHookContext = self
	
	oIrp.mOnChange = function( i_oIrp, i_oContext )
		local y = (self.stopY - self.height) + math.mod( i_oIrp:GetVal(), self.height*2 )
		--log ( "X: " .. self.stopY .. " - " .. self.height .. " + " .. math.mod( i_oIrp:GetVal(), self.height*2 ) )
		--log ("=== Y: " .. y )
		i_oContext:SetY( y )
		
		-- pick a new frame if the wheel has completed a full revolution
		if ( self.lastY > y ) then
			local iCurPick = i_oContext:GetFrame() - i_oContext.base
			i_oContext:SetFrame( i_oContext.base + math.mod( iCurPick + 1, 5 ) )
			
			--i_oContext:CanvasClear()
			--i_oContext:CanvasDrawTextLine( 10, 10, 90, 0xffffffff, tostring( math.mod( iCurPick + 1, 5 ) ) )
			--PlaySound("Tick")
		end
		
		-- note the last position so we can detect when the revolution is complete
		self.lastY = y
	end

	oIrp.mOnComplete = function( i_oIrp, i_oContext )
		i_oContext.rolling = false
		i_oContext.parent:RollResult( i_oContext.index, (i_oContext:GetFrame() - i_oContext.base))
		PlaySound("FRU_reelStop" )
	end
end

-- silently set the choice
function CRollHoldChoice:FakeRoll( extraTime )
	if ( (self.rolling == true) or (self.parent.playing == false) ) then
		return
	end
	
	self.rolling = true
	
	local oIrp = self:CreateIrp( self.stopX, 45 * self.height, 4 + extraTime, CIrpFuncs.mEaseOutQuad, 0 )
	
	oIrp.m_oHookContext = self
	
	oIrp.mOnChange = function( i_oIrp, i_oContext )
		
	end

	oIrp.mOnComplete = function( i_oIrp, i_oContext )
		i_oContext.rolling = false
		i_oContext.parent:RollResult( i_oContext.index, (i_oContext:GetFrame() - i_oContext.base))
	end
end


function CRollHoldChoice:OnUpdate()
	-- update the irpman
	self:UpdateIrps( 1 / 60 )
end

-- call this in C++ after a reload to rebind functions
function CRollHoldChoice:Bind( i_oObj )
	BindObj( CRollHoldChoice, i_oObj )
	return i_oObj
end

