--CampaignMenu.lua

local P = createMenuSheet("CampaignMenu")

function P:onShow()
    P:updateButtons()
end

function P.updateButtons()
    P.updateButton(0, winMgr:getWindow("orxonox/Mission1Button"))
    P.updateButton(1, winMgr:getWindow("orxonox/Mission2Button"))
    P.updateButton(2, winMgr:getWindow("orxonox/Mission3Button"))
    P.updateButton(3, winMgr:getWindow("orxonox/Mission4Button"))
    P.updateButton(4, winMgr:getWindow("orxonox/Mission5Button"))
    P.updateButton(5, winMgr:getWindow("orxonox/Mission6Button"))
    P.updateButton(6, winMgr:getWindow("orxonox/Mission7Button"))
    P.updateButton(7, winMgr:getWindow("orxonox/Mission8Button"))
    P.updateButton(8, winMgr:getWindow("orxonox/Mission9Button"))


    if (P.getIndexOfLastFinishedMission() == orxonox.LevelManager:getInstance():getNumberOfCampaignMissions() - 1) then
        local label = winMgr:getWindow("orxonox/CampaignMenuCongratulation")
        label:setProperty("Visible","True")
    end
end

function P.updateButton(index, button)
    if (P.shouldDisplayButton(index)) then
        button:setProperty("Visible", "True")

        if (P.shouldEnableButton(index)) then
            button:setProperty("Disabled", "False")
        end
    end
end

function P.shouldDisplayButton(index)
    local size = orxonox.LevelManager:getInstance():getNumberOfCampaignMissions()
    return index < size
end

function P.shouldEnableButton(index)
    return index <= P.getIndexOfLastFinishedMission() + 1
end

function P.getIndexOfLastFinishedMission()
    local lastMission = orxonox.LevelManager:getInstance():getLastFinishedCampaignMission()
    if (lastMission and lastMission ~= "") then
        local size = orxonox.LevelManager:getInstance():getNumberOfCampaignMissions()
        local index = 0
        while index < size do
            local mission = orxonox.LevelManager:getInstance():getCampaignMission(index)
            if (mission == lastMission) then 
                return index
            end
            index = index + 1
        end
    end
    return -1
end

function P.Mission1Button_clicked(e)
    P.loadLevel(P.FindLevel(0))
end

function P.Mission2Button_clicked(e)
    P.loadLevel(P.FindLevel(1))
end

function P.Mission3Button_clicked(e)
    P.loadLevel(P.FindLevel(2))
end

function P.Mission4Button_clicked(e)
    P.loadLevel(P.FindLevel(3))
end

function P.Mission5Button_clicked(e)
    P.loadLevel(P.FindLevel(4))
end

function P.Mission6Button_clicked(e)
    P.loadLevel(P.FindLevel(5))
end

function P.Mission7Button_clicked(e)
    P.loadLevel(P.FindLevel(6))
end

function P.Mission8Button_clicked(e)
    P.loadLevel(P.FindLevel(7))
end

function P.Mission9Button_clicked(e)
    P.loadLevel(P.FindLevel(8))
end

function P.loadLevel(level)
    orxonox.execute("startGame " .. level:getXMLFilename())
    hideAllMenuSheets()
end

function P.FindLevel(index)
    local filename = orxonox.LevelManager:getInstance():getCampaignMission(index)
    local level = nil
    local templevel = nil
    local size = orxonox.LevelManager:getInstance():getNumberOfLevels()
    local index = 0
    while index < size do
        templevel = orxonox.LevelManager:getInstance():getAvailableLevelListItem(index)
        if(templevel:getXMLFilename()==filename) then 
            level = templevel
        end
    index=index+1
    end
    return level
end

function P.CampaignMenuBackButton_clicked(e)
    hideMenuSheet(P.name)
end

return P