----------------------------------------------------------------------
-- Some utility stuff
----------------------------------------------------------------------

-- We're going to clobber the joystick button reading function so that
-- we don't immediately start doing whatever's hooked up to it after
-- exiting a UI screen.

waitingForJoyRelease = false;
realGetControlBoolValue = getControlBoolValue;
function fakeGetControlBoolValue(button)

   if waitingForJoyRelease then

      -- Check the first three buttons to see if they're still being
      -- held down.
      for i = 0, 9, 1 do
         if realGetControlBoolValue(i) then
            return false;
         end
      end

      -- None of the buttons were held. Clear the waiting flag.
      waitingForJoyRelease = false;
   end

   return realGetControlBoolValue(button);
   
end
getControlBoolValue = fakeGetControlBoolValue;

----------------------------------------------------------------------
-- Ugly test UI
----------------------------------------------------------------------

rootContainer = WidgetContainer:new();
rootContainer:setNumRows(3);
rootContainer:setStretchableRow(1);
rootContainer:setNumCols(3);
rootContainer:setStretchableCol(1);
--widgetSetRoot(rootContainer);

paddedBox = WidgetPaddedBox:new();
paddedBox:setBackgroundImage("editordata/np_ff1.ninepatch");
paddedBox:setMinWidth(32);
paddedBox:setMinHeight(32);
paddedBox:setWidth(32);
paddedBox:setHeight(32);
rootContainer:addChild(paddedBox);

buttonTest = WidgetButton:new();
paddedBox:setChild(buttonTest);

--rootContainer:addChild(nil);

paddedBox = WidgetPaddedBox:new();
paddedBox:setBackgroundImage("editordata/np_ff1.ninepatch");
paddedBox:setMinWidth(128);
paddedBox:setMinHeight(32);
paddedBox:setWidth(32);
paddedBox:setHeight(32);
--rootContainer:addChild(paddedBox);
rootContainer:setSlot(paddedBox, 2, 0);


buttonTest:setLabel("UI System test. Please ignore.");
buttonTest:setBackgroundImage("editordata/1pxbox1.ninepatch");
buttonTest:setBackgroundDefault("editordata/1pxbox1.ninepatch");
buttonTest:setBackgroundMouseOver("editordata/1pxbox2.ninepatch");
buttonTest:setBackgroundActive("editordata/1pxbox3.ninepatch");

--buttonTest:setLuaEventHandler("onClick", "print(\"DICKS\"); return true;");
buttonTest:setLuaEventHandler("onClick", "popUIScreen(); return true;");

----------------------------------------------------------------------
-- UI Stack System
----------------------------------------------------------------------

uiStack = {};

function pushUIScreen(widget)

	uiStack[#uiStack + 1] = widget;
	widgetSetRoot(widget);

	widgetFocusRoot();

end

function popUIScreen()

	local tmpWidget = uiStack[#uiStack];
	uiStack[#uiStack] = nil;
	
	widgetSetRoot(uiStack[#uiStack]);
	
    waitingForJoyRelease = true;

	return tmpWidget;
	
end

function popAndDeleteUIScreen()

	local tmpWidget = popUIScreen();
	
	if not tmpWidget == nil then
		Widget:delete(tmpWidget);
		return true;
	end
	return false;
end

----------------------------------------------------------------------
-- Generic info box
----------------------------------------------------------------------

function setButtonDefaults(button)
   button:setBackgroundImage("editordata/transparentbutton1.ninepatch");
   button:setBackgroundDefault("editordata/transparentbutton1.ninepatch");
   button:setBackgroundActive("editordata/transparentbutton1_md.ninepatch");
   button:setBackgroundMouseOver("editordata/transparentbutton1_mo.ninepatch");
end

function setButtonForJoystick(button)
   button:setBackgroundMouseOver("editordata/transparentbutton1_md.ninepatch");
end

function makeMessageBox(text)
	
	local alignedBox = WidgetAlignedBox:new();
	
	local paddedBox = WidgetPaddedBox:new();
	paddedBox:setBackgroundImage("editordata/np_ff2.ninepatch");
	paddedBox:setPadding(16);
	paddedBox:setMinWidth(384);

	local textWidget = WidgetText:new();
	textWidget:setText(text);
	textWidget:setWordWrap(true);

	local container = WidgetContainer:new();
	container:setNumRows(2);
	container:setCellSpacing(16);
	
	local closeButton = WidgetButton:new();
	setButtonDefaults(closeButton);
	closeButton:setLabel("Close");
	closeButton:setLuaEventHandler("onClick", "popAndDeleteUIScreen(); return true;");
	
	local closeButtonBox = WidgetAlignedBox:new();

	container:addChild(textWidget);
	closeButtonBox:setChild(closeButton);
	container:addChild(closeButtonBox);
	paddedBox:setChild(container);
	alignedBox:setChild(paddedBox);

	return alignedBox;
	
end

function popupMessageBox(text)
	pushUIScreen(makeMessageBox(text));
end

----------------------------------------------------------------------
-- Welcome thinger
----------------------------------------------------------------------

popupMessageBox(
	"Lily alpha v0.1*10^-13\n\n"..
	"Welcome to the Lily alpha. This game is nowhere near complete, and serves mostly as a demo of the engine. See the README.txt file for more information or don't complain to me when you can't figure out how to start the game.\n\n"..
	"And one last time: The only bugs I'm interested in hearing about right now are crashes, terrible performance, or inability of the game to run.\n\n"..
	"   -ExpiredPopsicle"
);


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




----------------------------------------------------------------------
-- New save menu!
----------------------------------------------------------------------

function selectSave(saveName)

   writeConsole("ACK "..saveName.."\n");

   paused = false;
   fade_fadeBlack = false;
   fade_alphaBlack = 1;
   fade_speed = 0.05;
   fade_update();

   clearEverything();
   unnameDeadEntities();

   loadGame(saveName);

   popAndDeleteUIScreen();
end

function popUpNewSaveMenu()

	local alignedBox = WidgetAlignedBox:new();
	
	local paddedBox = WidgetPaddedBox:new();
	paddedBox:setBackgroundImage("editordata/np_ff2.ninepatch");
	paddedBox:setPadding(16);
	paddedBox:setMinWidth(384);

    alignedBox:setChild(paddedBox);

    local mainContainer = WidgetContainer:new();
    paddedBox:setChild(mainContainer);
    mainContainer:setNumRows(3);
    mainContainer:setStretchableRow(1);
    
    local infoText = WidgetText:new();
    mainContainer:addChild(infoText);
    infoText:setText("Select a save game slot");
    infoText:setFontName("games/lily/fonts/arcadeFont.tga");
    
    local saveListCont = WidgetContainer:new();
    mainContainer:addChild(saveListCont);

    -- Get existing save games.
    local saveList = getSaveList();

    -- Get default saves. (These might get nuked later.)
    local defaultSaves = {
		default1=true;
		default2=true;
		default3=true;
		debug=true;
    };

    -- Find all the saves that match one of the default save names and
    -- turn them off. (So we don't have the real save and the fake
    -- entry for the default save at the same time.)

    -- While we're at it, also see what level each save is in.

	local k, v;
    local levelNames = {};
    local realSaveName = getSaveName();
	for k, v in pairs(saveList) do

       -- Get the save's globalData table.
       setSaveName(v);
       local saveData = loadTable("globaldata.xml");

       -- Pull out the current level name.
       if saveData and saveData.currentLevel then
          levelNames[v] = saveData.currentLevel;
       end

       -- Disable it in the list.
       if defaultSaves[v] then
          defaultSaves[v] = false;
       end

    end

    setSaveName(realSaveName);

    -- Add the fake entries for default saves into the save list.
	for k, v in pairs(defaultSaves) do
       if v == true then
          saveList[#saveList+1] = k;
       end
    end

    -- Sort the list of saves.
    table.sort(saveList);

    -- Add a button for each save.
    for k, v in pairs(saveList) do

       local saveSlotButton = WidgetButton:new();
       saveListCont:addChild(saveSlotButton);

       setButtonDefaults(saveSlotButton);
       --setButtonForJoystick(saveSlotButton);

       local levelString = "";
       
       if levelNames[v] then
          levelString = " ("..levelNames[v]..")";
       end

       local buttonLabel = v..levelString;

       saveSlotButton:setLabel(buttonLabel);
       saveSlotButton:setLuaEventHandler("onClick", "selectSave(\""..v.."\"); return false;");

    end

    saveListCont:setNumRows(saveListCont:getNumChildren() + 1);
    saveListCont:setStretchableRow(saveListCont:getNumChildren());

    local paddedBox = WidgetPaddedBox:new();
    mainContainer:addChild(paddedBox);
    paddedBox:setPadding(4);

    local cancelBox = WidgetAlignedBox:new();
    paddedBox:setChild(cancelBox);

    local cancelButton = WidgetButton:new();
    cancelBox:setChild(cancelButton);
    setButtonDefaults(cancelButton);
    setButtonForJoystick(cancelButton);
    cancelButton:setLabel("Cancel");
    cancelButton:setLuaEventHandler("onClick", "popAndDeleteUIScreen(); return false;");

    pushUIScreen(alignedBox);

    paused = true;

end


