
require "StandardMouseButton"
require "CDialog"
require "util"

-- reserve constructor
UILevelIntroDialog = 
{
	name		= "LevelIntroDialog",

	-- $ jbilas -- hardcoded cause I can't get the actual dimensions (GetWidth() includes modal backplane)
	s_fWidth	= 242.0,
	s_fHeight	= 184.0,
	s_fOpenDur = 5,
	s_fClosingDur = 3,
	
	State_Closing	= 100,
}


function UILevelIntroDialog:Create(  )
	
	-- The main screen container
	local oRet = root.dialogs[ self.name ]

	oRet:SetX( 400 - UILevelIntroDialog.s_fWidth / 2 )
	oRet:SetY( 300 - UILevelIntroDialog.s_fHeight / 2 )
	
	CDialog:Attach( oRet )
	UILevelIntroDialog:Bind( oRet )
	
	oRet:SetOnUpdateEnabled( true )
	
	return oRet
end

function UILevelIntroDialog:Init()
	log("LI init")
	self.m_aFields = { "_timeRemaining", "_itemsRemaining" }
	self._timeRemaining:SetText( self.m_sTimeRemaining )
	self._itemsRemaining:SetText( tostring( self.m_iItemsRemaining ) )
	
	for i = 1, table.getn( self.m_aFields ), 1 do
		local oField = self[ self.m_aFields[ i ] ]
		oField.m_fOrigX = oField:GetX()
		oField.m_fOrigY = oField:GetY()
		oField.m_fOrigWidth = oField:GetWidth()
		oField.m_fOrigHeight = oField:GetHeight()
	end

	local fTimeMod = 0.01
	
	--[[ particle markers
	self._effectLayerBelow:CreateParticleEmitter( "borderGlitter.xml",
												"borderGlitter", 
												self:GetX(),
												self:GetY() )
	self.m_iEmitterLeftId = self._effectLayerBelow:GetLastEmitterId()

	self._effectLayerBelow:CreateParticleEmitter( "borderGlitter.xml",
												"borderGlitter", 
												self:GetX(),
												self:GetY() )
	self.m_iEmitterRightId = self._effectLayerBelow:GetLastEmitterId()
	--]]
												
	self.m_oBorderIrp = self:CreateIrp( 0, 
								self._particleMarkerBorder:GetNumFrames() - 1,
								3, 
								CIrpFuncs.mBounce, 
								0 )

	self.m_oBorderIrp.m_oHookContext = self
	self.m_oBorderIrp.mOnChange = function( i_oIrp, i_oContext )
		i_oContext._particleMarkerBorder:SetFrame( math.floor( i_oIrp:GetVal() ) )

		--[[
		i_oContext._effectLayerBelow:MoveParticleEmitterById( i_oContext.m_iEmitterLeftId,
												i_oContext._particleMarkerBorder._marker1:GetX() - 9,
												i_oContext._particleMarkerBorder._marker1:GetY() - 12 )

		i_oContext._effectLayerBelow:MoveParticleEmitterById( i_oContext.m_iEmitterRightId,
												i_oContext._particleMarkerBorder._marker2:GetX() - 9,
												i_oContext._particleMarkerBorder._marker2:GetY() - 12 )
		--]]
	end
	
	self.m_oBorderIrp.mOnComplete = function( i_oIrp, i_oContext )
		--i_oContext._effectLayerBelow:KillParticleEmitterById( i_oContext.m_iEmitterLeftId )
		--i_oContext._effectLayerBelow:KillParticleEmitterById( i_oContext.m_iEmitterRightId )
	end

									
	self.m_oTextIrp = self:CreateIrp( 0,
										1, 
										1.5, 
										CIrpFuncs.mEaseInQuad, 
										0 )

	self.m_oTextIrp.m_oHookContext = self

	self.m_oTextIrp.mOnChange = function( i_oIrp, i_oContext )
		local fScale = 2 - 1 * i_oIrp:GetVal()

		for i = 1, table.getn( i_oContext.m_aFields ), 1 do
			local oField = i_oContext[ i_oContext.m_aFields[ i ] ]
			
			oField:SetScale( fScale, fScale )
			oField:SetX( oField.m_fOrigX - ( oField:GetWidth() - oField.m_fOrigWidth ) / 2 )
			local fModX = ( 1 - i_oIrp:GetVal() ) * 15
			-- make left field come from upper left, right from upper right
			if ( i == 1 ) then
				fModX = fModX * -1
			end
			oField:SetX( oField:GetX() + fModX )
			
			oField:SetY( oField.m_fOrigY - ( 1 - i_oIrp:GetVal() ) * 15 - ( oField:GetHeight() - oField.m_fOrigHeight ) / 2 )
			oField:SetOpacity( i_oIrp:GetVal() )
		end
	end
	
	-- make back plane responsive
	self.__UIScreenBack.m_oParent = self
	function self.__UIScreenBack:OnLeftMouseDown()
		self.m_oParent:OnLeftMouseDown()
	end
	
end

function UILevelIntroDialog:OnUpdate( )
	self:OnUpdateDialog()
	self:RunStateMachine()
end

function UILevelIntroDialog:OnLeftMouseDown()
	if ( self:GetState() == CDialog.State_Showing
			and self.m_iNextState ~= UILevelIntroDialog.State_Closing ) then
		self:SetState( UILevelIntroDialog.State_Closing )
	end
end

function UILevelIntroDialog:OnRunState()
	if ( self:GetState() == CDialog.State_Showing ) then
		if ( self.m_fTimeInState > UILevelIntroDialog.s_fOpenDur ) then
			self:SetState( UILevelIntroDialog.State_Closing )
		end
	elseif ( self:GetState() == UILevelIntroDialog.State_Closing ) then
		if ( self.m_fTimeInState > UILevelIntroDialog.s_fClosingDur ) then
			self:CloseAndSave( )
		end
	end
end

function CDialog:OnEnterState( i_eNewState )
	if ( self:GetState() == UILevelIntroDialog.State_Closing ) then
		-- show particle anim to book fields
		--CBook.G:RequestTextHighlightAnim( 2, 0.5 )
		
		-- fade
		local oNewIrp = self:CreateIrp( self:GetOpacity(), 
									0,
									2, 
									CIrpFuncs.mEaseOutQuad, 
									1 )

		oNewIrp.m_oHookContext = self
		oNewIrp.mOnChange = function( i_oIrp, i_oContext )
			i_oContext:SetOpacity( i_oIrp:GetVal() )
		end
	end
end

function UILevelIntroDialog:CharDown( i_Key )
	if ( self:GetState() == CDialog.State_Showing
			or self:GetState() == CDialog.State_TransitionIn ) then
		self:SetState( UILevelIntroDialog.State_Closing )
	end
end


function UILevelIntroDialog:Bind( i_oObj )
	BindObj( UILevelIntroDialog, i_oObj )
	return i_oObj
end

UILevelIntroDialog:Create()
