


-- Table containing all scenes.
currentCutScene = { };

-- What scene of the cutscene we're on.
currentCutSceneIndex = 1;

-- What line of the current scene we're on.
currentCutSceneLine = 1;

-- For each scene we need...
--   background
--   music?
--   lines

-- Each line is...
--   characterdef (or nil if no change)
--   text

-- Cutscene UI elements.
cutSceneWidgets = { };

cutSceneLineTimeout = nil;

function cutSceneUpdate()

   --cutSceneWidgets.characterRight:setMinWidth(1024 + math.random() * 5);

   -- TODO: Handle fade in/out.

   -- Handle timeouts.
   if not (cutSceneLineTimeout == nil) then

      cutSceneLineTimeout = cutSceneLineTimeout - (1/30);

      if cutSceneLineTimeout <= 0 then
         cutSceneAdvance();
      end

   end

end

function cutSceneAdvance()

   currentCutSceneLine = currentCutSceneLine + 1;
   writeConsole("Advancing line: "..currentCutSceneLine.."\n");

   if currentCutSceneIndex <= 0 or not (currentCutScene[currentCutSceneIndex].lines) or currentCutSceneLine > #currentCutScene[currentCutSceneIndex].lines then

      -- Done with this scene. Advance to the next.
   
      currentCutSceneIndex = currentCutSceneIndex + 1;
      currentCutSceneLine = 1;

      writeConsole("Advancing scene index: "..currentCutSceneIndex.."\n");
      
      if currentCutSceneIndex > #currentCutScene then

         writeConsole("No more scene indexes: "..currentCutSceneIndex.."\n");

         -- Nothing else to do? Clean up.
         cutSceneEnd();
         return;

      end

      -- Set the background, if it's changed.
      if currentCutScene[currentCutSceneIndex].background then
         cutSceneWidgets.background:setBackgroundImage(getRealFile("textures/"..currentCutScene[currentCutSceneIndex].background));
      end

   end

   local subScene = currentCutScene[currentCutSceneIndex];
   local line = subScene.lines[currentCutSceneLine];

   -- Update the text.
   if line.text then
      cutSceneWidgets.text:setText(line.text);
   end

   -- Update timeout.
   if line.timeout then
      cutSceneLineTimeout = line.timeout;
   else
      cutSceneLineTimeout = nil;
   end

   function cutSceneUpdateCharacter(charWidget, charDef)
      baseLayer = charDef[1];
      charWidget:clearChildren();

      if baseLayer then
         charWidget:setBackgroundImage(getRealFile("textures/"..baseLayer));
      else
         charWidget:setBackgroundImage("");
      end
      
      for i = 2, #charDef do
         writeConsole("Layer "..i..": "..charDef[i].."\n");

         local layer = Widget:new();
         layer:setBackgroundImage(getRealFile("textures/"..charDef[i]));
         charWidget:addChild(layer);

      end

   end

   if line.characterdef_left then
      cutSceneUpdateCharacter(cutSceneWidgets.characterLeft, line.characterdef_left);
   end

   if line.characterdef_right then
      cutSceneUpdateCharacter(cutSceneWidgets.characterRight, line.characterdef_right);
   end

end

function cutSceneEnd()

   currentCutScene = { };
   currentCutSceneIndex = 0;
   currentCutSceneLine = 0;
   cutSceneWidgets = { };

   popAndDeleteUIScreen();

end

function startCutscene()

   currentCutScene = {

      {
         background = "cyborghell_farbg.tga",
         lines = {
            { text = "herpderp" },
            { text = "der herpherp" },
            { text = "flerp" },
            { text = "merp" },
         },
      },
      
      {
         background = "cs_bg1.tga",
         lines = {
            { timeout = 5,
              characterdef_right = { "cs_samplechar2.tga", "cs_samplechar2_face1.tga" },
              text = "Warg!" },

            { text = "Blargh!" },

            { characterdef_left = { "cs_samplechar1.tga", "cs_samplechar1_face1.tga" },
              text = "What the christ?" },
         },
      },
      
      {
         background = "cyborghell_farbg.tga",
         lines = {
            { characterdef_right = { nil },
              text = "DERPYDOO" },
            { text = "What in the fuck just happened?" },
            { text = "I.. uhh..." },
            { characterdef_left = { nil },
              text = " " }
         },
      },

   };

   currentCutSceneIndex = 0;
   currentCutSceneLine = 0;

   local function FBOWrap(w)
      local fbo = WidgetFBO:new();
      fbo:setChild(w);
      fbo:setAlpha(0.75);
      return fbo;
   end

   local topAlignedBox = WidgetAlignedBox:new();

   --topFBO:setChild(topAlignedBox);
   --topAlignedBox:setChild(topFBO);

   local layers = WidgetLayerBox:new();
   topAlignedBox:setChild(layers);
   --topFBO:setChild(layers);
   --topFBO:setMinWidth(64);
   --topFBO:setMinHeight(64);

   -- FIXME: Hardcoded values here?
   layers:setMinWidth(1024);
   layers:setMinHeight(768);
   layers:setBackgroundImage(getRealFile("textures/cs_bg1.tga"));
   layers:setLuaEventHandler("WIDGETEVENT_MOUSEDOWN", "cutSceneAdvance(); return false;");

   -- Left character.
   local alignLeft = WidgetAlignedBox:new();
   layers:addChild(alignLeft);
   alignLeft:setHAlign(-1);

   local characterLeft = WidgetLayerBox:new();
   alignLeft:setChild(characterLeft);
   characterLeft:setMinWidth(512);
   characterLeft:setMinHeight(768);
   characterLeft:setBackgroundImage(getRealFile("textures/cs_samplechar1.tga"));

   -- Right character.
   local alignRight = WidgetAlignedBox:new();
   layers:addChild(alignRight);
   alignRight:setHAlign(1);

   local characterRight = WidgetLayerBox:new();
   alignRight:setChild(characterRight);
   characterRight:setMinWidth(512);
   characterRight:setMinHeight(768);
   characterRight:setBackgroundImage(getRealFile("textures/cs_samplechar2.tga"));

   -- Save the text for last so it's on top of the characters.
   local textHolder = WidgetAlignedBox:new();
   textHolder:setVAlign(1);
   textHolder:setHStretch(true);
   layers:addChild(textHolder);
   
   local textHolder2 = WidgetPaddedBox:new();
   textHolder2:setPadding(32);
   textHolder2:setBackgroundImage(getRealFile("textures/cs_textbg.tga"));
   textHolder:setChild(textHolder2);

   local textBox = WidgetText:new();
   textHolder2:setChild(textBox);
   textBox:setWordWrap(true);
   textBox:setMinWidth(1024);
   textBox:setScale(2);
   textBox:setText(""..
                   "words words words words words"..
                   "words words words words words"..
                   "words words words words words"..
                   "words words words words words"..
                   "\n");


   cutSceneWidgets.characterLeft = characterLeft;
   cutSceneWidgets.characterRight = characterRight;
   cutSceneWidgets.background = layers;
   cutSceneWidgets.text = textBox;

   pushUIScreen(topAlignedBox);
   cutSceneAdvance();

end

