--============================================================================
--  mainmenu.lua
--  Copyright (c) 2009, Bamtang Games. All Rights Reserved.
------------------------------------------------------------------------------

require( "scripts/style.lua" );

BodyFont = {
    titleFont, 
    16,
    Color(252, 226, 115, 255)
}

NameFont = {
    titleFont, 
    20,
    WhiteColor
}

function handleMessage(msg)
    if msg == "playFindObjects" then
        playFindObjects(kCutsceneFile);
    elseif msg == "playDifferences" then
        playDifferences(gDifferencesLevel);
    elseif msg == "playPuzzle" then
        playPuzzle(gPuzzleLevel);
    elseif msg == "playCoordinates" then
        playCoordinates(gCoordinatesLevel);
    elseif msg == "playScramble" then
        playScramble(gScrambleLevel);
    end
end

function playFindObjects(filePath)
    kCutsceneFile = filePath;
    SwapToModal("scripts/findobjects.lua");
end

function playDifferences(idLevel)
    gDifferencesLevel = idLevel;
    SwapToModal("scripts/differencesgame.lua");
end

function playPuzzle(idLevel)
    gPuzzleLevel = idLevel;
    SwapToModal("scripts/puzzlegame.lua");
end

function playCoordinates(idLevel)
    gCoordinatesLevel = idLevel;
    SwapToModal("scripts/coordinatesgame.lua");
end

function playScramble(idLevel)
    gScrambleLevel = idLevel;
    SwapToModal("scripts/scramblegame.lua");
end


function doTutorial()
    DoModal("scripts/tutorial_panel.lua");
end

function currentScene(cscene)
	gScene = cscene;
end

function checkRoom(done1, done2, done3)
	gRoomComplete1 = done1;
	gRoomComplete2 = done2;
	gRoomComplete3 = done3;
end

function allPuzzlesComplete(done1, done2, done3)
	gRoomAllPuzzlesComplete1 = done1;
	gRoomAllPuzzlesComplete2 = done2;
	gRoomAllPuzzlesComplete3 = done3;
end

function gotoMap(idLevel, locked1, locked2, locked3)
    gMapLevel = idLevel;
	gLocked1 = locked1;
	gLocked2 = locked2;
	gLocked3 = locked3;
    DoModal("scripts/map.lua");
end

function showMessageItem(show)
    if (show) then
        SetState(kPause, true);
        DoModal("scripts/message_item.lua");
    else
        PopModal("MessageItem");
        SetState(kPause, false);
    end
end

function CheckUsers()
    if (GetNumUsers() == 0) then
        gEnterNameValue = "";
        name = DoModal("scripts/entername.lua");
        if (not(name == nil)) then
            CreateNewUser(name);
            return true ;
        end
        return false ;
    else
        return true;
    end
end

function QuitGame()
    CloseWindow();
end

function showCutscene(nLevel) --fileName)
	if (nLevel == 0) then
		kCutsceneFile = "cutscene/cs01.xml";
	elseif (nLevel == 1) then
		kCutsceneFile = "cutscene/cs02.xml";
	elseif (nLevel == 2) then
		kCutsceneFile = "cutscene/cs03.xml";
	elseif (nLevel == 3) then
		kCutsceneFile = "cutscene/cs04.xml";
	elseif (nLevel == 4) then
		kCutsceneFile = "cutscene/cs05.xml";
	elseif (nLevel == 5) then
		kCutsceneFile = "cutscene/cs06.xml";
	elseif (nLevel == 6) then
		kCutsceneFile = "cutscene/cs07.xml";
	elseif (nLevel == 7) then
		kCutsceneFile = "cutscene/cs08.xml";
	end;
	DebugOut("[Lua] cutscene: "..nLevel.."->"..kCutsceneFile);
	SwapToModal("scripts/cutscene.lua");
end

function endCutsceneFinish()
	PopModal("CutsceneDialog");
end;

--kcsFinishLevel01 = false;

function showCutsceneFinish()
	--if (kcsFinishLevel01 == false) then
		kCutsceneFile = "cutscene/csfinish.xml";
		DebugOut("[Lua] cutscene: "..kCutsceneFile);
		DoModal("scripts/cutscene.lua");
	--	kcsFinishLevel01 = true;
	--end;
end

function showGainTrophy(trophy)
	DebugOut("ShowGainTrophy("..trophy..")");
    gGainTrophy = "-1";
    if ((trophy ~= "-1") and (trophy ~= "")) then
		gGainTrophy = trophy;
		DoModal("scripts/gain_trophy.lua");
	end;
end

function onMaxHintReached()
    DoModal("scripts/hint_system.lua");
end

MakeDialog {
    -- Name the modal dialog
    name="mainmenu",
    MainMenu  {
        fit = true;

        Bitmap {
            name = "backgroundimage",
            image = "images/gui/ae_titlecard_background",

            Text {
                font = BodyFont,
                name = "lblWelcome",
                x = 348, y = 522,
                w = 100, h = 30,
                flags = kHAlignCenter + kVAlignCenter,
                label = "welcome"
            };

            Text {
                font = NameFont,
                name = "txtName",
                x = 333, y = 538,
                w = 130, h = 30,
                flags = kHAlignCenter + kVAlignBottom,
                label = "#PLAYERNAME"
            };

            SetStyle(ButtonStyle),
            Button {
                x = 290,
                y = 444,
                name = "btnPlay",
                graphics = PlayButtonGraphics, 
                command = 
                    function()
						-- showCurscene(GetLevelToPlay());
						--showCutscene(GetLastSavedLevel());
                        if IsGameCompleted() then
                            DoModal("scripts/mode_selection.lua");
                        else
                            onButtonPress("ButtonPlay");
                        end
                    end
            },

            Button {
                x = 0,
                y = 544,
                name = "options",
                graphics = OptionsButtonGraphics, 
                command = 
                    function()
                        DoModal("scripts/options.lua");
                    end
            },

            Button {
                x = 119,
                y = 494,
                name = "btnTrophies",
                graphics = TrophiesButtonGraphics, 
                command = 
                    function()
                        DoModal("scripts/trophies.lua")
                    end
            },

            Button {
                x = 605,
                y = 544,
                name = "quit",
                graphics = QuitButtonGraphics,
                command=
                    function()
                        result = DisplayDialog {
                            "scripts/yesno.lua",
                            title = "", 
                            body ="quitconfirm"
                        };
                        if (result=='yes') then
                            PostMessage(CreateNamedMessage(kQuitNow, "Quit"));
                        end
                    end
            },

            Button {
                x = 342,
                y = 571,
                name = "btnChangeplayer",
                graphics = ChangeButtonGraphics,
                command=
                    function()
                        DoModal("scripts/chooseplayer.lua");
                    end
            },

            Button {
                x = 485,
                y = 494,
                name = "btnHelp",
                graphics = HelpButtonGraphics,
                command=
                    function()
                        DoModal("scripts/help.lua");
                    end
            },
--[[
			Button {
                x = 340,
                y = 254,
                name = "btnMap",
                label = "#Map...",
                command = 
                    function()
						currentScene("scene1");
						checkRoom(true, true, true);
						allPuzzlesComplete(true, true, false);
                        gotoMap(8, false, true, false);
                    end
            },

            Button {
                x = 340,
                y = 414,
                name = "btnDifferences",
                label = "#Differences...",
                command = 
                    function()
                        playDifferences(1);
                    end
            },

			Button {
                x = 340,
                y = 374,
                name = "btnPuzzle",
                label = "#Puzzle1...",
                command = 
                    function()
                        playPuzzle(4);
                    end
            },
			
			Button {
                x = 340,
                y = 294,
                name = "btnCoordinates",
                label = "#Coordinates...",
                command = 
                    function()
                        playCoordinates(1);
                    end
            },

            Button {
				x = 340,
				y = 254,
				name = "btnScramble",
				label = "#Scramble...",
				command =
					function ()
						playScramble(4);
					end				
            },

            Button {
				x = 340,
				y = 214,
				name = "Cutscene",
				label = "#cutscene...",
				command =
					function ()
						showCutscene("cutscene/cutscenetest.xml");
					end				
            },
--]]            
        },
    },

    FadeWindow {
        w = 800,
        h = 600,
        name = "FadeWindow",

        Bitmap {
            image = "images/panelblack",
            name = "bmpPanelBlack",
            x = 0,
            y = 0,
        }
    },
   
}

ChangeUserData();
