
Level = {}


----------------------------------------------------------------------------------------------------
function Level:new()
	obj = {}
	setmetatable(obj, self)
	self.__index = self
	
    obj.mLocations = {}
    obj.levelIdx = #Levels.items + 1
 
	return obj
end

----------------------------------------------------------------------------------------------------
function Level:initLocation(aName, aScene)
    self.mLocations[aName] = Profile:getLocationInfo(aName, aScene)
    if self.mLocations[aName] == nil then
        self.mLocations[aName] = {}
        self.mLocations[aName].totalHids = 12
        self.mLocations[aName].hidCount = 0
        self.mLocations[aName].keyCount = 0
        self.mLocations[aName].scene = aScene
        self.mLocations[aName].challengePassed = false
        Profile:setLocationInfo(aName, aScene, self.mLocations[aName])
    end
    if not Levels.links[aName] then Levels.links[aName] = {} end
    Levels.links[aName][aScene] = obj.levelIdx
end

----------------------------------------------------------------------------------------------------
function Level:getInfo(aLocationName)
    if self.mLocations[aLocationName] == nil then
        return
    end

    return
    {
        totalHids = self.mLocations[aLocationName].totalHids,
        collectedHids = self.mLocations[aLocationName].hidCount,
        keys = self.mLocations[aLocationName].keyCount,
        challengePassed = self.mLocations[aLocationName].challengePassed,
        scene = self.mLocations[aLocationName].scene
    }
end

----------------------------------------------------------------------------------------------------
function Level:setInfo(aLocationName, aParams)
    if self.mLocations[aLocationName] == nil or aParams == nil then
        return
    end

    if aParams.totalHids ~= nil then
        self.mLocations[aLocationName].totalHids = aParams.totalHids
    end

    if aParams.collectedHids ~= nil then
        self.mLocations[aLocationName].hidCount = aParams.collectedHids
    end
--[[
    if aParams.keys ~= nil then
        self.mLocations[aLocationName].keyCount = aParams.keys
    end
]]
    if aParams.challengePassed ~= nil then
        self.mLocations[aLocationName].challengePassed = aParams.challengePassed
    end

end

----------------------------------------------------------------------------------------------------
function Level:addKey(aLocationName)
    self.mLocations[aLocationName].keyCount = self.mLocations[aLocationName].keyCount + 1
end

----------------------------------------------------------------------------------------------------
function Level:isLocationFinished(aLocName)
    return self.mLocations[aLocName].hidCount >= self.mLocations[aLocName].totalHids and self.mLocations[aLocName].challengePassed
end

----------------------------------------------------------------------------------------------------
function Level:isPassed()
    local passed = true
    table.foreach(self.mLocations,
        function(aLoc)
            if self.mLocations[aLoc].hidCount < self.mLocations[aLoc].totalHids or not self.mLocations[aLoc].challengePassed then
                passed = false
            end
        end)

    return passed
end

----------------------------------------------------------------------------------------------------
function Level:checkPassed()
    if self:isPassed() then
        if self.mCutscene ~= nil then
            Profile.data.cutscene = self.mCutscene
        end

        Profile:setLevel(Profile:getLevel() + 1)
        Inventory:clear()
    end
end

function Level:getLocationsList()
    local tLocations = {}
    table.foreach(self.mLocations, function(aLoc) table.insert(tLocations, aLoc) end)
    return tLocations
end

----------------------------------------------------------------------------------------------------
function Level:setMinigame(aName, aParams)
    self.mMinigame = {name = aName, params = aParams}
end

----------------------------------------------------------------------------------------------------
function Level:setCutscene(aIdx)
    self.mCutscene = aIdx
end

----------------------------------------------------------------------------------------------------
function Level:onWin()
    if self.mMinigame ~= nil then
        if self.mMinigame.params ~= nil then
            local minigame = nil
            if self.mMinigame.name == 'Match2' then
                Match2.currentLayout = self.mMinigame.params.layout
                minigame = Match2

            elseif self.mMinigame.name == 'Collapse' then
                Collapse.colors = self.mMinigame.params.colors
                Collapse.speed = self.mMinigame.params.speed
                Collapse.powerups = self.mMinigame.params.powerups
                minigame = Collapse

            --[[elseif self.mMinigame.name == 'TextTwist' then
                --if not self.mMinigame.params.word then
                TextTwist.StrongWord = TextTwistDict[self.mMinigame.params.word][#TextTwistDict[self.mMinigame.params.word] ]
                --end
                minigame = TextTwist]]
            end

            if minigame ~= nil then
                minigame.tutorial = (self.mMinigame.params.tutorial ~= nil)
            end

        end
        --timer_mgr:add_timer(function() level_mgr:switch_to_level(self.mMinigame.name) fade_panel:fade_out() end, 2.0, 1)
        level_mgr:switch_to_level(self.mMinigame.name) fade_panel:fade_out()
    else
        if HOLevel.ExitTarget ~= "" then
            --timer_mgr:add_timer(function() level_mgr:switch_to_level(HOLevel.ExitTarget) fade_panel:fade_out() end, 2.0, 1)
            level_mgr:switch_to_level(HOLevel.ExitTarget) fade_panel:fade_out()
            HOLevel.ExitTarget = ""
        else
            --timer_mgr:add_timer(function() level_mgr:switch_to_level("Map") fade_panel:fade_out() end, 2.0, 1)
            level_mgr:switch_to_level("Map") fade_panel:fade_out()
        end
    end
end

----------------------------------------------------------------------------------------------------
function Level:isKeysFound(aLocName)
    local keys_count = self.mLocations[aLocName].keyCount
    return (keys_count == 3)
end

----------------------------------------------------------------------------------------------------
--[[function Level:getKeysCount()
    local count = 0

    table.foreach(self.mLocations,
        function(aLoc)
            local keys = Profile:getKeys(aLoc)
            if keys == nil then
                return
            end

            if keys[self.mLocations[aLoc].scene] then
                count = count + 1
            end
        end)

    return count
end]]