-------------------------------------------------------------------------------
class 'BaseScreen' (Node)
-------------------------------------------------------------------------------
local window_layer = nil
function getWindowLayer()
	if not window_layer then
		window_layer = CreateWindowLayerEntity()
		window_layer:setOrder(1)
		window_layer:setName('WindowLayerEntityInLua')
	end

	return window_layer
end
-------------------------------------------------------------------------------

function BaseScreen:__init(name)
	WriteInfo("Create BaseScreen with name "..name)

	self.name    = name
	self.timer   = Timer()
	self.destroy = false
	
	self.defaultShowHideTime = SCREEN_SHOW_HIDE
	self.bExclusiveFullScreen = false

	self:createWindow()
	Node.__init(self, self.window, 'main_entity_in_'..name, -1)

	self:createFadeObject()
	self:loadResources()

	if TEMP_USE_OLD_INVENTORY then
		if current_scene then
			current_scene:getInventory():dropTool()
		end
	end
end
-------------------------------------------------------------------------------

function BaseScreen:createWindow()
	self.window_layer = CreateWindowLayerEntity()
	self.window_layer:setOrder(1)
	self.window_layer:setName('WindowLayerEntity_'..self.name)
	
	self.window = self.window_layer:createWindow()
	self.window:setXY(0,0)
	self.window:setName('WindowEntity_'..self.name)
	self.window.onUpdate:add( bind(self.onDefaultUpdate, self) )
end
-------------------------------------------------------------------------------

function BaseScreen:createMainEntity(parent, name, order)
	Node.createMainEntity(self, parent, name, order)

	self.main_entity:setActive	( false )
	self.main_entity:setVisible	( false )
	self.main_entity:setOpacity	( 0 )
	self.main_entity:setColor	( Colorf(1, 1, 1, 0) )
	self.main_entity:setSize	( WSCR_W, WSCR_H )
	self.main_entity:setHotSpot	( (WSCR_W - WORK_W) / 2,
								  (WSCR_H - WORK_H) / 2 )
end
-------------------------------------------------------------------------------

function BaseScreen:createFadeObject()
	self.fadeObject = CreateSpriteEntity('')
	self.fadeObject:attachTo		( self:getMainEntity() )
	self.fadeObject:setName			( 'fade_in_on_load' )
	self.fadeObject:setSize			( WSCR_W, WSCR_H )
	self.fadeObject:setXY			( WORK_W / 2, WORK_H / 2 )
	self.fadeObject:setColor		( Colorf(0, 0, 0, 0.7) )
	self.fadeObject:setOrder		( -1 )
	self.fadeObject:setRespondable	( false )
end
-------------------------------------------------------------------------------

function BaseScreen:loadResources()
	if self.CreateLoaderTable then
		local table_resource = self:CreateLoaderTable()
		LoadScene( self.main_entity, table_resource )
	end
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- GETTERS --------------------------------------------------------------------
-------------------------------------------------------------------------------

function BaseScreen:getName()
	return self.name
end
-------------------------------------------------------------------------------

function BaseScreen:getTimer()
	return self.timer
end
-------------------------------------------------------------------------------

function BaseScreen:getWindowLayer()
	return self.window_layer
end
-------------------------------------------------------------------------------

function BaseScreen:getWindow()
	return self.window
end
-------------------------------------------------------------------------------

function BaseScreen:getFadeObject()
	return self.fadeObject
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- CONTROLS -------------------------------------------------------------------
-------------------------------------------------------------------------------

function BaseScreen:_activateMe()
	self.main_entity:setVisible(true)
	self.main_entity:setActive(true)
end
-------------------------------------------------------------------------------

function BaseScreen:show(time)
	do
		local notification = getLoadingQueueIsEmptyNotification()
		if self.resourcesLoaded == true then
			notification = nil
		end
		local function activateMe()
			if self:isDestroy() then
				WriteWarning("trying to activate screen " .. self.name .. " that is already destoyed.")
				return
			end
			self:_activateMe()		    
			time = (time == nil) and self.defaultShowHideTime or time
	
			if time == 0 then
				self.main_entity:setOpacity(1)
				self.main_entity:destroyEffectors()
				self:onShown()
			else
				local effector

				if self.bExclusiveFullScreen then
					self.main_entity:setOpacity(1)
					self.main_entity:destroyEffectors()
					self:getFadeObject():setOrder(self:getMaxOrder() + 1)
					effector = self:getFadeObject():ChangeOpacity(1, 0, time)
				else
					self:getFadeObject():setOrder(-1)
					effector = self.main_entity:ChangeOpacity(nil, 1, time)
				end

				effector.onFinish:add(	function()
											self:onShown()
										end)
--				self.main_entity:setRespondable(false)
--				self.timer:add( function()
--									self.main_entity:setRespondable(true)
--								end, time)
			end
			self.resourcesLoaded = true
		end

		if notification then
			notification:add(function()
									self.resourcesLoaded = false
									activateMe()
								end)
		else
			activateMe()
		end
	end
end
-------------------------------------------------------------------------------

function BaseScreen:hide(time)
	time = (time == nil) and self.defaultShowHideTime or time
	
	if time == 0 then
		self.main_entity:setOpacity(0)
		self.main_entity:destroyEffectors()
	else
		if self.bExclusiveFullScreen then
			self:getFadeObject():setOrder(self:getMaxOrder() + 1)
			self:getFadeObject():ChangeOpacity(0, 1, time)
			self.timer:add( function()
								self:getFadeObject():setOpacity(0)
							end, time)
		else
			self:getFadeObject():setOrder(-1)
			self.main_entity:ChangeOpacity(nil, 0, time)
		end
		self.main_entity:setRespondable(false)
		self.timer:add( function()
							self.main_entity:setRespondable(true)               
						end, time)
	end
end
-------------------------------------------------------------------------------

function BaseScreen:isVisible()
	return self.main_entity:getOpacity() == 1
end
-------------------------------------------------------------------------------

function BaseScreen:go()
	self.window:setRespondable(true)
	self.window:setActive(true)
end
-------------------------------------------------------------------------------

function BaseScreen:stop()
	self.window:setRespondable(false)
	self.window:setActive(false)
end
-------------------------------------------------------------------------------

function BaseScreen:isActive()
	return self.window:isActive()
end
-------------------------------------------------------------------------------

function BaseScreen:destroyScreen()
	self.destroy = true

	if self.window:isActive() then
		local time = self.defaultShowHideTime
		if self.bExclusiveFullScreen then
			self:getFadeObject():setOrder(self:getMaxOrder() + 1)
			self:getFadeObject():ChangeOpacity(0, 1, time)
		else
			self:getFadeObject():setOrder(-1)
			self.main_entity:ChangeOpacity(nil, 0, time)
		end
		self.main_entity:setRespondable(false)
		self.timer:add( function()
							self:getWindowLayer():destroy()
						end, time)
	else
		self:getWindowLayer():destroy()
	end
end
-------------------------------------------------------------------------------

function BaseScreen:isDestroy()
	return self.destroy
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- EVENTS ---------------------------------------------------------------------
-------------------------------------------------------------------------------

function BaseScreen:onShown()
end
-------------------------------------------------------------------------------

function BaseScreen:onDefaultUpdate(entity, dt)
	self.timer:update(dt)

	if self.destroy == true then       -- !!! TODO: пернести строчки обновления таймера ниже, так как после удаления скрина, таймер не должен обновляться. Сделать после 18.01.2013
		return
	end

	if self.onUpdate then
		self:onUpdate(dt)
	end
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- HELPERS --------------------------------------------------------------------
-------------------------------------------------------------------------------

function BaseScreen:addHotKey(hotKey, handler)
	self:getWindow():getHotkeySignal(hotKey):add(handler)
end
-------------------------------------------------------------------------------

function BaseScreen:removeAllHanderInHotKey(hotKey)
	self:getWindow():getHotkeySignal(hotKey):removeAll()
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- CREATER --------------------------------------------------------------------
-------------------------------------------------------------------------------

function CreatedScreen(scriptFileScreen, screenClassName, ...)
	dofile(scriptFileScreen)
	local screen = Helpers.CallFunctionByName(screenClassName, screenClassName, ...)
	
	return screen
end
-------------------------------------------------------------------------------