--============================================================================
--  main.lua
--  Copyright (c) 2010, Bamtang Games. All Rights Reserved.
------------------------------------------------------------------------------

gInit = false;
gQuitVerifyActive = 0;
gMaxLengthUserName = 10;
gMaximumNumberOfPlayers = 5;
gRegisterMode = "cancel";

gAnimOpen = 1;
gGameMode = "Classic";
gGameDifficulty = "easy"; --(easy, medium, hard)
gGameStyle = ""; -- (quest, quick)
gGoal = 0;
gReward = 0;
SetState(kPause, false);

gEnterNameValue = "";
gTypeAvatar = "";

gAllButtonsDisabled = false;

------------------------------------------------------------------------------
-- Swit global variables.
if gsw_Debug == nil then
    DebugOut('[ERROR] gsw_Debug not defined!');
    gsw_Debug = 0;
end
if gsw_LocalizedPath == nil then
    DebugOut('[ERROR] gsw_LocalizedPath not defined!');
    gsw_LocalizedPath = 'loc/en/';
end

------------------------------------------------------------------------------

-- Popup messages

function ShowLoseLife(num)
    result = DisplayDialog {
        "scripts/lose_life.lua";
        lifeCount = num;
    };
    numberLifes(num);
end

function selectorTitle(name)
    if name == 1 then
        return "bonus level";
    else
        return "bonus keep going";
    end
end

function selectorRound(round, total)
    if round == total then
        return "bonus last round";
    else
        return "#"..GetString("bonus round", tostring(round), tostring(total));
    end
end

function selectorTile(tile)
    if tile then
        return "bonus completed all";
    else
        return "#"..GetString("bonus completed", tostring(round));
    end
end

function ShowBonusLevel(bonus, total, game)
    result = DisplayDialog {
        "scripts/bonus_level.lua";
        title = selectorTitle(bonus);
        body = "bonus jokers";
        mode = game;
        round = selectorRound(bonus, total);
    };
end

function ShowBonusComplete(pRound, pNumRound, pTile, pNumTiles, pScore, pHighScore)
    result = DisplayDialog {
        "scripts/bonus_complete.lua";
        complete = tostring(pRound);
        tile = pTile;
        numberTiles = tostring(pNumTiles);
        score = tostring(pScore);
        highScore = tostring(pHighScore);
    };
end

function ShowLevelInstructions()
    local result = gfn_isShownInstruction(gGameMode);
    if result == 1 then
        DoModal("scripts/instructions_level.lua");
    end
end

function DisableAllButtons()
    gAllButtonsDisabled = true;
end

function EnableAllButtons()
    gAllButtonsDisabled = false;
end

------------------------------------------------------------------------------
-- Level Games
function ShowScoreCard()
    result = DisplayDialog {
        "scripts/score_card.lua";
        image = "gpx/popups/scorecard";
    };
end
------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- Panel - Reward
function setReward(reward)
    gReward = reward;
end
------------------------------------------------------------------------------
------------------------------------------------------------------------------
function setGoalPanel(goal)
    gGoal = goal;
end
------------------------------------------------------------------------------
------------------------------------------------------------------------------

-- Main game loop
function Main()
    if gsw_Debug == 0 then
        DisplaySplash('', 'splash/gamehouselogo_background', 4000);
        DisplaySplash('splash/bamtang_animated.swf', 'splash/bamtang_still', 4000);
    end

    -- If there is no login user, pop a dialog that lets the user create one
    if gsw_getNumUsers() == 0 then
        gEnterNameValue = "";
        gTypeAvatar = "";
        gRegisterMode = "nocancel";
        name = DoModal('scripts/register_user.lua');
        if name ~= nil then
            gsw_createNewUser(name);
            gnf_changeAvatar(gTypeAvatar);
            gfn_changeUserData();
        end
    end

    while true do
        -- DoMainWindow will exit only if there are NO windows pushed on the stack,
        -- so a PopModal()/PushModal() combination will not cause this to loop.
        DoMainWindow('scripts/main_menu.lua');
    end
end

-- Return a function to be executed in a thread
return Main
