-------------------------------------------------------------------------------
class 'MapScreen' (Node)
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
local PAGE_COUNT 	    = 3
local PAGE_NAME         = 'map_page_'
local WAY_NAME 	        = 'anchor_ways'

local LOC_START_SCALE   = 1.0
local LOC_FINAL_SCALE   = 2.0

local DEFAULT_INDEX     = 1
local MAP_BG            = 'map_bg'
local LEVELS_ANCHOR     = 'anchor_map_levels'
local MAP_ANCHOR        = 'anchor_map_bg'
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function MapScreen:__init(screen)   
    Node.__init(self, screen:getMainEntity(), 'MapScreen', 0)  
    
    self.main_entity:attachTo( screen:getEntityNamed('anchor_content') )
    self.main_entity:setXY(10, -772)

    self.screen     = screen 
    self.map_pages  = {}

    self:loadData()
    self:init()    
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function MapScreen:init()
    self.level_connections = {
        [1] = { 2 },
        [2] = { 3 },
        [3] = { 4 },
        [4] = { 5 },
        [5] = { 6 },
        [6] = { 7 },
        [7] = { 8 },
        [8] = { 6 },
        [9] = { 6 },
    }


    self.map_bg  = self:getEntityNamed(MAP_BG) 
    self.anchor_levels = self:getEntityNamed(LEVELS_ANCHOR)
    self.anchor_map = self:getEntityNamed(MAP_ANCHOR)
    self.anchor_ways = self:getEntityNamed(WAY_NAME)
    
    self.map_bg.onClickBegin:add( function() self:onDragBegin() end )
    self.map_bg.onClickEnd:add( function() self:onDragEnded() end )
    self.map_bg.onFocusOut:add( function() self:onFocusLost() end )
    self.map_bg.onUpdate:add( function() self:onUpdate() end )

    self.scaleMap = 1.0
    self.anchor_map:setScale(self.scaleMap)
    self.anchor_map:setHotSpot(424 * self.scaleMap, 223 * self.scaleMap)
    --self.map_bg:setHotSpot(0 * self.scaleMap, 768 * self.scaleMap)

    self:showPageContent()
    -- self:checkBttnsStates()
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function MapScreen:onDragBegin() 
    self.previousPosition = GetCursorEntity():getPosition()
    self.isDraging = true
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function MapScreen:onDragEnded() 
    self.isDraging = false
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function MapScreen:onFocusLost() 
    self.isDraging = false
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function MapScreen:onUpdate(dt) 
    if self.isDraging then
        local currentPosition = GetCursorEntity():getPosition()

        if currentPosition.x ~= self.previousPosition.x and 
           currentPosition.y ~= self.previousPosition.y then 

            local x = self.map_bg:getX()
            local y = self.map_bg:getY()

            local dx = currentPosition.x - self.previousPosition.x
            local dy = currentPosition.y - self.previousPosition.y

            local finalPos = { x = x + dx, y = y + dy }
            if ((finalPos.x >= -424) or (finalPos.x <= -942)) then
                finalPos.x = x
            end

            if ((finalPos.y <= 223) or (finalPos.y >= 518)) then
                finalPos.y = y
            end

            self.map_bg:setXY(finalPos.x, finalPos.y)
            self.anchor_levels:setXY(finalPos.x, finalPos.y)
            self.anchor_ways:setXY(finalPos.x, finalPos.y)
            self.previousPosition = currentPosition
        end
    end
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function MapScreen:createWays()
    local locations_buttons = self:getLocationBttns()

    for index, v in ipairs(locations_buttons) do  
        local next_locations     = self.level_connections[index] 

        for _, value in pairs(next_locations) do 
            local loc_name = string.sub(locations_buttons[value], 3)      
            if (isSceneVisited(loc_name)) then
                local location_button   = self:getEntityNamed(locations_buttons[value])   
                local prev_bttn         = self:getEntityNamed(v)

                local start_pos         = Vector2f(location_button:getX(), location_button:getY())
                local finish_pos        = Vector2f(prev_bttn:getX(), prev_bttn:getY())
                local max_delta_pos     = 50
                local num_keys          = 300

                local keys = HelpersEffect.createSplineBezier(  start_pos,
                                                                start_pos + Vector2f(max_delta_pos, max_delta_pos),
                                                                finish_pos,
                                                                finish_pos + Vector2f(max_delta_pos, max_delta_pos),
                                                                num_keys)
                local prev_pos = keys[1]
                local dpos = 12
                self.ways = {}

                local anchor = CreateSpriteEntity('')
                anchor:setName(WAY_NAME .. index .. value .. prev_bttn:getName())   
                anchor:attachTo(self:getEntityNamed(WAY_NAME)) 
                anchor:setRespondable(false)
                anchor:setOrder(2)   

                for i, position in ipairs(keys) do
                    if math.abs(prev_pos.x - position.x) >= dpos or 
                       math.abs(prev_pos.y - position.y) >= dpos then
                        local way = CreateSpriteEntity('Data/Textures/Map/point_way.png')
                        way:setName(prev_bttn:getName() .. '_pw_' .. i)          
                        way:setRespondable(false)
                        way:attachTo(anchor)
                        way:setXY(position.x, position.y)               
                        way:setOrder(2)     
                        way:setScale(0.5)   

                        prev_pos = position  
                    end          
                end
            end
        end
    end   
end
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
function MapScreen:showPageContent()
    local locations_buttons         = self:getLocationBttns()
    local location_name_txt_obj     = self:getEntityNamed('map_name_txt')
    local location_button_frame_obj = self:getEntityNamed('map_button_frame')

    local blocked_levels = {'08_Lauras_room', '08_Lauras_room_HO'}  

    self:createWays()         

    for index, v in ipairs(locations_buttons) do
        local location_button = self:getEntityNamed(v)
        local loc_name = string.sub(v, 3)
    
        if not isSceneVisited(loc_name) then
            self:entityHide(v)  

            local next_locations     = self.level_connections[index] 
            for _, value in pairs(next_locations) do  
                if self:hasEntityNamed(WAY_NAME .. index .. value .. location_button:getName()) then            
                    self:entityHide(WAY_NAME .. index .. value .. location_button:getName())  
                end   
            end          
        else
            local function show_frame()                 
                location_button_frame_obj:attachTo(location_button)
                location_button_frame_obj:setXY(2, -16)
                location_name_txt_obj:setText(StringManager:getString('MAP_LOCATION_NAME_'..string.upper(loc_name)))
                location_button_frame_obj:ChangeOpacity(0, 1, 0.2)
                location_button_frame_obj:setVisible(true)
                location_button_frame_obj:setOrder(2)

                location_button:setOrder(16)
                self.location_eff = location_button:ChangeScale(LOC_START_SCALE, LOC_FINAL_SCALE, 0.2)

                playCommonSound('MapShowLocation')
            end

            local function hide_frame()
                location_button_frame_obj:setVisible(false)

                if self.location_eff then
                    location_button:destroyEffectors()
                    self.location_eff = nil
                end
                location_button:setOrder(10)
                location_button:ChangeScale(location_button:getScale().x, LOC_START_SCALE, 0.2)
            end

            -- blocked levels  
            local function is_blocked_level(name)
             for _, name_level in ipairs(blocked_levels) do
                 if name_level == name then
                     return true
                 end
             end
             return false
            end

            local function do_transition()
                if is_blocked_level(current_scene:getPlayField():getName()) then
                    return 
                end

                local transition_level = v
                if current_scene.name ~= loc_name then
                    self.screen:onCloseButtonClick()       
                    startLevel(transition_level)
                end
            end
          
            local current_location = current_scene:getPlayField():getName()
            if (current_location == loc_name)  then
                self:getEntityNamed('map_level_complete_' .. index):setOpacity(0)
                self:getEntityNamed('map_current_level_' .. index):setOpacity(1)
            elseif (IsThereSomethingToDo(loc_name)) then
                self:getEntityNamed('map_level_complete_' .. index):setOpacity(1)
                self:getEntityNamed('map_current_level_' .. index):setOpacity(0)
            else 
                self:getEntityNamed('map_level_complete_' .. index):setOpacity(0)
                self:getEntityNamed('map_current_level_' .. index):setOpacity(0)
            end

            location_button.onFocusIn:add(show_frame)
            location_button.onFocusOut:add(hide_frame)
            location_button.onPress:add(do_transition)
        end
    end
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function MapScreen:getLocationBttns()
    local entity_table      = self:CreateLoaderTable()
    local location_entity   = {}
    local index = 1

    for _, obj in pairs(entity_table) do
        if string.sub(obj.name, 1, 2) == 'l_' then
            location_entity[index] = obj.name
            index = index + 1
        end
    end

    return location_entity
end
-------------------------------------------------------------------------------

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

-------------------------------------------------------------------------------
function MapScreen:showScreen()
	self.main_entity:show()
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function MapScreen:hideScreen()
	self.main_entity:hide()
end
-------------------------------------------------------------------------------

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

function MapScreen:CreateLoaderTable()
	local objects =
	{
		{ name = 'anchor_control',  entityType = 'SpriteEntity',  x = 840,  y = 200,  order = 60,  isVisible = false,  isRespondable = false,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  width = 2,  height = 2,  },
		{ name = 'map_button_frame',  entityType = 'SpriteEntity',  x = 903,  y = 83,  order = 20,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Map/map_button_frame.png',  width = 228,  height = 156,  },
		{ name = 'map_name_txt',  entityType = 'TextEntity',  parentName = 'map_button_frame',  x = 0,  y = -48,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  text = '',  height = 18,  },
		{ name = 'anchor_map_bg',  entityType = 'SpriteEntity',  x = 508,  y = 382,  order = 10,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 424, y = 223},  width = 2,  height = 2,  },
		{ name = 'anchor_ways',  entityType = 'SpriteEntity',  parentName = 'anchor_map_bg',  x = -424,  y = 223,  order = 8,  scale = 0.5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 0, y = 0},  width = 2,  height = 2,  },
		{ name = 'anchor_map_levels',  entityType = 'SpriteEntity',  parentName = 'anchor_map_bg',  x = -424,  y = 223,  order = 10,  scale = 0.5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 0, y = 0},  width = 2,  height = 2,  },
		{ name = 'l_01_Boat_crash_site',  entityType = 'SpriteEntity',  parentName = 'anchor_map_levels',  x = 1473,  y = -274,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/Locations/l_01_Boat_crash_site.png',  width = 208,  height = 128,  },
		{ name = 'map_current_level_1',  entityType = 'SpriteEntity',  parentName = 'l_01_Boat_crash_site',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_current_level.png',  width = 46,  height = 46,  },
		{ name = 'map_level_complete_1',  entityType = 'SpriteEntity',  parentName = 'l_01_Boat_crash_site',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_level_complete.png',  width = 24,  height = 48,  },
		{ name = 'l_02_Mountain_path',  entityType = 'SpriteEntity',  parentName = 'anchor_map_levels',  x = 1471,  y = -394,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/Locations/l_02_Mountain_path.png',  width = 208,  height = 128,  },
		{ name = 'map_current_level_2',  entityType = 'SpriteEntity',  parentName = 'l_02_Mountain_path',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_current_level.png',  width = 46,  height = 46,  },
		{ name = 'map_level_complete_2',  entityType = 'SpriteEntity',  parentName = 'l_02_Mountain_path',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_level_complete.png',  width = 24,  height = 48,  },
		{ name = 'l_03_Panorama',  entityType = 'SpriteEntity',  parentName = 'anchor_map_levels',  x = 1136,  y = -437,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/Locations/l_03_Panorama.png',  width = 208,  height = 128,  },
		{ name = 'map_current_level_3',  entityType = 'SpriteEntity',  parentName = 'l_03_Panorama',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_current_level.png',  width = 46,  height = 46,  },
		{ name = 'map_level_complete_3',  entityType = 'SpriteEntity',  parentName = 'l_03_Panorama',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_level_complete.png',  width = 24,  height = 48,  },
		{ name = 'l_04_Gazebo',  entityType = 'SpriteEntity',  parentName = 'anchor_map_levels',  x = 915,  y = -246,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/Locations/l_04_Gazebo.png',  width = 208,  height = 128,  },
		{ name = 'map_current_level_4',  entityType = 'SpriteEntity',  parentName = 'l_04_Gazebo',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_current_level.png',  width = 46,  height = 46,  },
		{ name = 'map_level_complete_4',  entityType = 'SpriteEntity',  parentName = 'l_04_Gazebo',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_level_complete.png',  width = 24,  height = 48,  },
		{ name = 'l_05_Forest_path',  entityType = 'SpriteEntity',  parentName = 'anchor_map_levels',  x = 838,  y = -533,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/Locations/l_05_Forest_path.png',  width = 208,  height = 128,  },
		{ name = 'map_current_level_5',  entityType = 'SpriteEntity',  parentName = 'l_05_Forest_path',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_current_level.png',  width = 46,  height = 46,  },
		{ name = 'map_level_complete_5',  entityType = 'SpriteEntity',  parentName = 'l_05_Forest_path',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_level_complete.png',  width = 24,  height = 48,  },
		{ name = 'l_06_Town',  entityType = 'SpriteEntity',  parentName = 'anchor_map_levels',  x = 634,  y = -665,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/Locations/l_06_Town.png',  width = 208,  height = 128,  },
		{ name = 'map_current_level_6',  entityType = 'SpriteEntity',  parentName = 'l_06_Town',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_current_level.png',  width = 46,  height = 46,  },
		{ name = 'map_level_complete_6',  entityType = 'SpriteEntity',  parentName = 'l_06_Town',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_level_complete.png',  width = 24,  height = 48,  },
		{ name = 'l_07_Cantellos_shop',  entityType = 'SpriteEntity',  parentName = 'anchor_map_levels',  x = 539,  y = -840,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/Locations/l_07_Cantellos_shop.png',  width = 208,  height = 128,  },
		{ name = 'map_current_level_7',  entityType = 'SpriteEntity',  parentName = 'l_07_Cantellos_shop',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_current_level.png',  width = 46,  height = 46,  },
		{ name = 'map_level_complete_7',  entityType = 'SpriteEntity',  parentName = 'l_07_Cantellos_shop',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_level_complete.png',  width = 24,  height = 48,  },
		{ name = 'l_08_Lauras_room',  entityType = 'SpriteEntity',  parentName = 'anchor_map_levels',  x = 605,  y = -969,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/Locations/l_08_Lauras_room.png',  width = 208,  height = 128,  },
		{ name = 'map_current_level_8',  entityType = 'SpriteEntity',  parentName = 'l_08_Lauras_room',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_current_level.png',  width = 46,  height = 46,  },
		{ name = 'map_level_complete_8',  entityType = 'SpriteEntity',  parentName = 'l_08_Lauras_room',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_level_complete.png',  width = 24,  height = 48,  },
		{ name = 'l_09_Andreas_house',  entityType = 'SpriteEntity',  parentName = 'anchor_map_levels',  x = 856,  y = -744,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/Locations/l_09_Andreas_house.png',  width = 208,  height = 128,  },
		{ name = 'map_current_level_9',  entityType = 'SpriteEntity',  parentName = 'l_09_Andreas_house',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_current_level.png',  width = 46,  height = 46,  },
		{ name = 'map_level_complete_9',  entityType = 'SpriteEntity',  parentName = 'l_09_Andreas_house',  x = 66,  y = 25,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Map/map_level_complete.png',  width = 24,  height = 48,  },
		{ name = 'map_bg',  entityType = 'SpriteEntity',  parentName = 'anchor_map_bg',  x = -424,  y = 223,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 0, y = 768},  texturePath = 'Data/Textures/Map/map_bg.jpg',  width = 1366,  height = 768,  },
	}
	return objects
end
