local DANCERS_COUNT = 17

class 'l_Room' (PlayField)

function l_Room:__init(game_scene, name, path)
	local previous_scene_name = name
	local previous_se_scene_name = nil
	local previous_ce_scene_name = nil
	if HasGameState(LAST_VISITED_SCENE, nil, EGameStateTag.GENERAL) then
		previous_scene_name = GetGameState(LAST_VISITED_SCENE, EGameStateTag.GENERAL):get(0)
	end

	if HasGameState(LAST_VISITED_SCENE, nil, EGameStateTag.SE) then
		previous_se_scene_name = GetGameState(LAST_VISITED_SCENE, EGameStateTag.SE):get(0)
	end

	if HasGameState(LAST_VISITED_SCENE, nil, EGameStateTag.CE) then
		previous_ce_scene_name = GetGameState(LAST_VISITED_SCENE, EGameStateTag.CE):get(0)
	end

	local is_exit_to_main_menu = HasGameState('PARENT_ACHIEVMENT_MAINMENU', 'true', EGameStateTag.GENERAL)
	Console:pushText('is_exit_to_main_menu = '.. tostring(is_exit_to_main_menu))

	if is_exit_to_main_menu then
		RemoveGameState('PARENT_ACHIEVMENT_MAINMENU', nil, EGameStateTag.GENERAL)
	end

	PlayField.__init(self, game_scene, name, path)

	if previous_se_scene_name then
		SetGameState(LAST_VISITED_SCENE, previous_se_scene_name, EGameStateTag.SE)
	elseif HasGameState(LAST_VISITED_SCENE, nil, EGameStateTag.SE) then
		RemoveGameState(LAST_VISITED_SCENE, nil, EGameStateTag.SE)
	end

	if previous_ce_scene_name then
		SetGameState(LAST_VISITED_SCENE, previous_ce_scene_name, EGameStateTag.CE)
	elseif HasGameState(LAST_VISITED_SCENE, nil, EGameStateTag.CE) then
		RemoveGameState(LAST_VISITED_SCENE, nil, EGameStateTag.CE)
	end

	SetGameState(LAST_VISITED_SCENE, previous_scene_name, EGameStateTag.GENERAL)
	
	if not HasGameState(PLAY_MODE) then
		SetGameState(PLAY_MODE, EPlayMode.CASUAL)
	end
	if not HasGameState('HintFillPercentage') then
		SetGameState('HintFillPercentage', '0')
	end

	self:getPanel():setMode(EPanelMode.CLEAN)

	if HasGameState('PLAY_MODE_NEED_TO_BE_REMOVED', 'true', EGameStateTag.GENERAL) then
		RemoveGameState('PLAY_MODE_NEED_TO_BE_REMOVED', nil, EGameStateTag.GENERAL)
		RemoveGameState(PLAY_MODE, EPlayMode.CASUAL, tag)
		RemoveGameState(LAST_VISITED_SCENE, nil, EGameStateTag.GENERAL)
	end

	self.game_scene:getInformManager():set_content('room_link')
	self.game_scene:getInformManager().inform_screen:getEntityNamed('inform_title'):hide()

	local function on_bttn_home_click()
		if is_exit_to_main_menu then
			exitToMainMenu()
		else
			self.game_scene:loadPlayField(previous_scene_name)
		end
	end
	self:getEntityNamed('bttn_back').onPress:add(on_bttn_home_click)

	self.infoWindow 	= self:getEntityNamed('InfoWindow')
	self.infoText 		= self:getEntityNamed('info_text')
	self.infoNameText 	= self:getEntityNamed('info_name_text')

	self:initTables() 

	self:setAchievmentsOpacity(0.01) 
	self:setAchievmentsHoverVisible(false) 

	self:registerAchievmntsEvents()
	self:initAchievmentObject()
end

function l_Room:initTables() 
	local compositionEntity = self:getEntityNamed('compositionBG')
	self.animObjects = {
		area_click_Dog			= {item 	= { compositionEntity:getElementByName('Dog'),
							 				    compositionEntity:getElementByName('Dog_head') },
							 	   hover 	= { compositionEntity:getElementByName('dog_hover_head'),
												compositionEntity:getElementByName('dog_hover') },
								   pos 		= { x = 798, y = 416} },

		area_click_Helmet	 	= {item 	= { compositionEntity:getElementByName('Helmet') },
							 	   hover 	= { compositionEntity:getElementByName('roman_helmet_light') },
							 	   pos 		= { x = 586, y = 205} },

		area_click_ERS_rabbit	= {item 	= { compositionEntity:getElementByName('ERS_rabbit') },
							 	   hover 	= { compositionEntity:getElementByName('ERS_rabbit_light') },
							 	   pos 		= { x = 754, y = 250} },

		area_click_Camera		= {item		= { compositionEntity:getElementByName('Camera') },
							 	   hover 	= { compositionEntity:getElementByName('Camera_light') },
							 	   pos 		= { x = 398, y = 218} },


		area_click_Gold_eagle	= {item 	= { compositionEntity:getElementByName('Gold_eagle') },
							 	   hover 	= { compositionEntity:getElementByName('Gold_eagle_light') },
							 	   pos 		= { x = 545, y = 258} },

		area_click_Magnifier	= {item 	= { compositionEntity:getElementByName('Magnifier') },
							 	   hover 	= { compositionEntity:getElementByName('Magnifier_light') },
							 	   pos 		= { x = 736, y = 368} },

		area_click_Sand_clock	= {item 	= { compositionEntity:getElementByName('Sand_clock') },
							 	   hover 	= { compositionEntity:getElementByName('Sand_clock_light') }, 
							 	   pos 		= { x = 508, y = 318} },

		area_click_Spyglass		= {item 	= { compositionEntity:getElementByName('Spyglass') },
							 	   hover 	= { compositionEntity:getElementByName('Spyglass_light') }, 
							 	   pos 		= { x = 312, y = 446} },

		area_click_Sun_clock	= {item 	= { compositionEntity:getElementByName('Sun_clock') },
							 	   hover 	= { compositionEntity:getElementByName('Sun_clock_light') }, 
							 	   pos 		= { x = 410, y = 340} },

		area_click_Compass		= {item 	= { compositionEntity:getElementByName('Compass') },
							 	   hover 	= { compositionEntity:getElementByName('Compass_light') },
							 	   pos 		= { x = 384, y = 396} },

		area_click_Owl	 		= {item 	= { compositionEntity:getElementByName('cabin'):getElementByName('Owl') },
							 	   hover 	= { compositionEntity:getElementByName('cabin'):getElementByName('Owl_light') }, 
							 	   pos 		= { x = 266, y = 532} },
	
	}
	
end

function l_Room:setAchievmentsOpacity(opacity) 
	for index, obj in pairs(self.animObjects) do
		for _, inner_obj in pairs(obj.item) do
			inner_obj:setOpacity(opacity)
		end		
	end
end

function l_Room:setAchievmentsHoverVisible(visible) 
	for index, obj in pairs(self.animObjects) do
		for _, inner_obj in pairs(obj.hover) do
			local opacity = visible and 1 or 0
			inner_obj:setOpacity(opacity)
		end		
	end
end

function l_Room:showInfoWindow(pos, visible)
	if (self.effector) then
		self.infoWindow:destroyEffectors()
		self.effector = nil
	end
	self.infoWindow:setXY(pos.x, pos.y)

	if visible then	
		self.infoWindow:ChangeOpacity(0, 1, 0.4)
	else
		self.effector = self.infoWindow:ChangeOpacity(1, 0, 0.4)
	end
end

function l_Room:registerAchievmntsEvents() 	

	local function focusInArea(area_entity)
		local index = area_entity:getName()
		for _, obj in pairs(self.animObjects[index].hover) do
			obj:setOpacity(1)
			
		end

		self:stopSound('AchievementHover')
		self:playSound('AchievementHover')

		self.infoText:setText( StringManager:getString( string.upper(index) .. '_INFO' ) )
		self.infoNameText:setText( StringManager:getString( string.upper(index) .. '_NAME_INFO' ) )

		self:showInfoWindow(self.animObjects[index].pos, true)
	end

	local function focusOutArea(area_entity)
		local index = area_entity:getName()
		for _, obj in pairs(self.animObjects[index].hover) do
			obj:setOpacity(0)
		end
		self:showInfoWindow(self.animObjects[index].pos, false)
	end

	for index, obj in pairs(self.animObjects) do
		local obj = self:getEntityNamed(index)
		obj.onFocusIn:add( focusInArea )
		obj.onFocusOut:add( focusOutArea )	
	end
end

function l_Room:initAchievmentObject()
	self.achievmentObjects = 
	{
		NoHint5HO 		= { sprite_name = 	self.animObjects.area_click_Magnifier.item },			
		NoSkipMG 		= { sprite_name = 	self.animObjects.area_click_Spyglass.item }, 		
		Collect10Photo 	= { sprite_name = 	self.animObjects.area_click_Camera.item },
		Collect1Clock 	= { sprite_name = 	self.animObjects.area_click_Sand_clock.item }, 
	}

	for achievment_name, obj in pairs(self.achievmentObjects) do
		local achievment = current_scene:getAchievmentsManager():getAchievment(achievment_name)
		if achievment and achievment:isCompleted() then
			for _, item in pairs(obj.sprite_name) do
				item:setOpacity(1.0)
			end				
		end
	end
end

function l_Room:init_hint()
	-- self.dancer_hint = 
	-- {
	-- 	[3] = 'NoSkipMG_Hint',
	-- 	[4] = 'NoHintHO_Hint',
	-- 	[5] = 'NoHint10HO_Hint',
	-- 	[1] = 'Collect1Photo_Hint',
	-- 	[2] = 'Collect1Clock_Hint',
	-- }

	-- self.hint_text = self:getEntityNamed('HintOnAchievment')
	-- self.hint_text:setVisible(false)
	-- self:getMainEntity().onUpdate:add(bind(self.updateHintText, self))
	-- for i = 1, 4 do
	-- 	self:getEntityNamed('dancer_'..i..'_locked').onFocusIn:add(bind(self.onFocusIn, self))
	-- 	self:getEntityNamed('dancer_'..i..'_locked').onFocusOut:add(bind(self.onFocusOut, self))
	-- end
end

function l_Room:updateHintText()
	-- local mouse = GetCursorEntity():getScrPosition()
		
	-- self.hint_text:setScrXY(mouse.x + 10, mouse.y -35)
end

function l_Room:onFocusIn(entity)
	-- local text = self.dancer_hint[tonumber(string.match(entity:getName(), '%d+'))]
	-- self.hint_text:setText(StringManager:getString(text))
	-- self.hint_text:setVisible(true)
end

function l_Room:onFocusOut(entity)
	-- self.hint_text:setVisible(false)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function l_Room:CreateLoaderTable()
	local objects =
	{
		{ name = 'HintOnAchievment',  entityType = 'TextEntity',  x = 24,  y = 18,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  text = 'text',  font = 'Data/Fonts/tool_tip.fnt',  height = 29,  },
		{ name = 'Room_anim',  entityType = 'CompositionContainer',  x = -171,  y = 0,  order = 12,  editingLocked = false,  treeItemIsExpanded = false,  path = 'Data/Scripts/Scenes/Room/Room_anim.lua',  },
		{ name = 'compositionBG',  entityType = 'CompositionSequence',  parentName = 'Room_anim',  x = 0,  y = 0,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  isCyclic = true,  speed = 1,  },
		{ name = 'menu_idle',  entityType = 'VideoEntity',  x = 512,  y = 276,  order = 0,  scale = 1.5,  editingLocked = false,  treeItemIsExpanded = false,  path = 'Data/Video/Menu/menu_idle.webm',  hasAlpha = false,  isLooped = true,  fps = 24,  },
		{ name = 'area_click_Camera',  entityType = 'SpriteEntity',  x = 101,  y = 178,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 186,  height = 359,  },
		{ name = 'area_click_ERS_rabbit',  entityType = 'SpriteEntity',  x = 504,  y = 151,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 186,  height = 251,  },
		{ name = 'area_click_Helmet',  entityType = 'SpriteEntity',  x = 898,  y = 256,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 230,  height = 310,  },
		{ name = 'area_click_Magnifier',  entityType = 'SpriteEntity',  x = 934,  y = 499,  order = 110,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 181,  height = 106,  },
		{ name = 'area_click_Sand_clock',  entityType = 'SpriteEntity',  x = 769,  y = 426,  order = 105,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 147,  height = 244,  },
		{ name = 'area_click_Sun_clock',  entityType = 'SpriteEntity',  x = 658,  y = 460,  order = 110,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 132,  height = 105,  },
		{ name = 'area_click_Dog',  entityType = 'SpriteEntity',  x = 538,  y = 438,  order = 120,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 132,  height = 149,  },
		{ name = 'area_click_Gold_eagle',  entityType = 'SpriteEntity',  x = 329,  y = 438,  order = 105,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 219,  height = 295,  },
		{ name = 'area_click_Compass',  entityType = 'SpriteEntity',  x = 156,  y = 502,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 191,  height = 176,  },
		{ name = 'area_click_Spyglass',  entityType = 'SpriteEntity',  x = 191,  y = 602,  order = 125,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 345,  height = 96,  },
		{ name = 'area_click_Owl',  entityType = 'SpriteEntity',  x = 512,  y = 665,  order = 125,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 192,  height = 210,  },
		{ name = 'InfoWindow',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 300,  isRespondable = false,  opacity = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Room/Layers/InfoWindow.png',  width = 402,  height = 296,  },
		{ name = 'info_text',  entityType = 'TextEntity',  parentName = 'InfoWindow',  x = 1,  y = 15,  order = 0,  scale = 0.72,  editingLocked = false,  treeItemIsExpanded = false,  text = 'fdddd',  font = 'Data/Fonts/dialog.fnt',  height = 30,  boundsX = 476,  boundsY = 270,  },
		{ name = 'info_name_text',  entityType = 'TextEntity',  parentName = 'InfoWindow',  x = 3,  y = -113,  order = 0,  scale = 0.72,  editingLocked = false,  treeItemIsExpanded = false,  text = 'fdddd',  font = 'Data/Fonts/dialog.fnt',  height = 36,  boundsX = 476,  boundsY = 50,  },
		{ name = 'bttn_back',  entityType = 'ButtonEntity',  x = 194,  y = 730,  order = 60,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Room/Layers/bttn_back.png',  text = '',  },
		{ name = 'Layer_title',  entityType = 'SpriteEntity',  x = 512,  y = 36,  order = 600,  scale = 0.95,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Room/Layers/Layer_title.PNG',  width = 316,  height = 62,  },
	}
	return objects
end
