class 'LabyrinthScreen' (PlayField)

function LabyrinthScreen:__init(game_scene, name, path)
	PlayField.__init(self, game_scene, name, path)

	self.MaxWrongSteps = 2 -- without self.bMazeEnable

    if LabyrinthScreen.CreateLoaderTable then
        local table_resource = LabyrinthScreen.CreateLoaderTable(self)
        LoadScene(self:getMainEntity(), table_resource)
    end 
    self.anchor	= self:getEntityNamed('anchor')
end

function LabyrinthScreen:Start()
	self:linkScene('area_link_back',   'CursorDown',    self.previous_location_name)
    if self.bMazeEnable then
        self:getPanel():setMode(EPanelMode.MAZE)
    end
    local game_state_name = self:getRoomStateName()
    local room_name 
    if HasGameState(game_state_name) then
        room_name = GetGameState(game_state_name):get(0)
    else
        room_name = self.start_room_name
        AddGameState(game_state_name, room_name)
    end
    self:loadRoom(room_name)
end

function LabyrinthScreen:loadRoom(room_name)

    -- if self.room then
    --     self.room:getMainEntity():destroy()
    -- end

    -- SetGameState(self:getRoomStateName(), room_name)

    if room_name == self.end_room_name then
        self:getPanel():setMode(EPanelMode.QUEST)  
    end

	dofile (GetParentFolderPath(self:getPathLua()) .. room_name .. '.lua')
	self.room = Helpers.CallFunctionByName(room_name, self.anchor, self.name)
	self.loadData(self.room)

	-- local function initScreenData(scene)    
 --        local table_resource = scene:CreateLoaderTable()

 --        for _, obj in pairs(table_resource) do      
 --            local _obj = scene:getEntityNamed(obj.name)
 --            --_obj:setXY(obj.x - self.anchor:getX(), obj.y - self.anchor:getY())
 --            if string.sub(obj.name, 1, 16) == 'area_click_link_' then 
 --                _obj:setName(obj.name..'_'..room_name)    
 --            end      
 --        end
 --    end

 --    initScreenData(self.room)

    self:registerLinks(room_name)

    if self.room.roomCallback then 
        self.room.roomCallback(self)
    end

    if room_name == self.error_room_name then
        if not HasGameState(self:getBlinkStateName()) then
            AddGameState(self:getBlinkStateName(),'')

            self:showBlackBar('BBT_05_FOREST_PATH_WRONG_WAY_'..math.random(3))
            self:getMouseBlocker():push()

            self.timer:add(function ()
                self:getMouseBlocker():pop()                
                self:hideBlackBar()

                local level_EPanelMode = self.game_scene:getPanel():getMode()
                self.game_scene:getPanel():setMode(EPanelMode.CLEAN)
                local video = CreateVideoEntity('Data/Video/Scenes/05_Forest_path/room_1_blink.webm',true)
                video:attachTo(self:getMainEntity())
                video:setName('video_room_1_blink')
                video:setXY(512, 384)
                video:setOrder(1000)
                video:setScaleX(4.5)
                video:setScaleY(4.5)
                video:setRespondable(false)
                video:setSoftEdgesSize(soft or Vector2f(0, 0))
                video.onFinish:add(function()
                    self.game_scene:getPanel():setMode(level_EPanelMode)
                    video:destroy()
                    --self:showBlackBar('BBT_05_FOREST_PATH_WRONG_WAY_'..math.random(3))   
                end)
            end, 1)
        end
    end

end

function LabyrinthScreen:registerLinks(room_name)
    local room_number = tonumber(string.sub(room_name, 6))
    for i,data in ipairs(self.link_table[room_number]) do
        local link_name = data[1]
        local cursor_name = data[2]
        local obj_name = 'area_click_link_'..i  --..'_'..room_name

        local w_game_state_name = self.name..'_Wrong_Steps'

        if string.sub(link_name, 1, 4) == 'Room' then
            self:registerActiveObject(obj_name, function (e)
                if not e.save then
                    if self.bMazeEnable then
                        --self:loadRoom(link_name)
                        SetGameState(self:getRoomStateName(), link_name)
                        startLevel('l_'..self.name)
                    else
                        --self:loadRoom(self.exit_room_name)    
                        local wrong_steps_count = 0
                        if HasGameState(w_game_state_name) then
                            wrong_steps_count = Helpers.RestoreObjectFromGameState(w_game_state_name)
                        end
                        wrong_steps_count = wrong_steps_count + 1
                        if  wrong_steps_count < self.MaxWrongSteps then
                            Helpers.SaveObjectToGameState(w_game_state_name, wrong_steps_count)
                            SetGameState(self:getRoomStateName(), link_name)
                            startLevel('l_'..self.name)   
                        else
                            --Helpers.SaveObjectToGameState(w_game_state_name, 0)
                            SetGameState(self:getRoomStateName(), self.error_room_name)
                            startLevel('l_'..self.name)
                        end
                    end
                end
            end, true, cursor_name)
        else
            if room_name == self.end_room_name then
                self:linkScene(obj_name, cursor_name, link_name)
            else
                self:registerActiveObject(obj_name, function (e)
                    if not e.save then
                        Helpers.SaveObjectToGameState(w_game_state_name, 0)
                        SetGameState(self:getRoomStateName(), self.start_room_name)
                        if HasGameState(self:getBlinkStateName())   then
                            RemoveGameState(self:getBlinkStateName())
                        end
                        startLevel('l_'..link_name)
                    end
                end, true, cursor_name)
            end
        end
        
    end
end

function LabyrinthScreen:getRoomStateName()
    return self.name..'_Last_Room_Name' 
end

function LabyrinthScreen:getBlinkStateName()
    return self.name..'_Blink_Error_Room_Showed' 
end

function LabyrinthScreen:loadData()
    if self.CreateLoaderTable then
        local table_resource = self.CreateLoaderTable()
        LoadScene(self:getMainEntity(), table_resource)
    end 
end

function LabyrinthScreen:SkipMiniGame()
    --self:loadRoom(self.end_room_name)
    SetGameState(self:getRoomStateName(), self.end_room_name)
    startLevel('l_'..self.name)
end

--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function LabyrinthScreen:CreateLoaderTable()
	local objects =
	{
		{ name = 'anchor',  entityType = 'SpriteEntity',  x = 0,  y = 0,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
		{ name = 'area_link_back',  entityType = 'SpriteEntity',  x = 512,  y = 718,  order = 100,  scaleX = 11,  scaleY = 2,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
	}
	return objects
end
