--[[
WARNING!!!

This file will spoil some levels!
(Ignore this if you are the pack developer.)





















]]

hintdata = {
  ["12level"] = { -- overcast digsite
    [1] = {
      ["name"] = "Cross The Spike",
      [1] = "Only two rules can be formed.\nWould either of these be permanent?",
      [2] = "Have you thoroughly explored the rightmost room?",
      [3] = "When @baba@ moves onto a space next to an object it @fear@s,\n@baba@ will move away from that object before anything else.",
      [4] = "Solution on next page!",
      [5] = "Form \"@rock@ is @push@\" and position the @rock@ just above the @spike@.\nRetrieve the text to form \"@baba@ fear @rock@\",\nthen move onto the @spike@ to cross over.",
    },
    [2] = {
      ["name"] = "Cross The Spikes (Again)",
      [1] = "If something might be @push@able,\nit's best to check if it actually is.",
      [2] = "@spike@ will forever be @defeat@.\nPerhaps it can be something else, too?",
      [3] = "At some point, you will need to travel over the @spike@\nfrom the first part of the level again.",
      [4] = "Solution on next page!",
      [5] = "Push \"@push@\" to the right and move upwards into the \"is\". Push the \"is\" back down\ninto the hole where you came from, and hop over the @spike@ again. Push \"@spike@\"\nup to form \"@spike@ is @push@\", then push past the spikes in the bottom of the room.",
    },
    [3] = {
      ["name"] = "Win",
      [1] = "If @text@ is @hide@, then hidden rules\nwon't show up in the pause menu.",
      [2] = "If even part of a complete rule is not @hide@,\nthe entire rule will show in the pause menu.",
      [3] = "How can @win@ be freed?\nWhat other objects do you possess control over?",
      [4] = "Solution on next page!",
      [5] = "Using \"@bug@ is @up@\", \"@bug@ is @right@\", and \"@bug@ is @move@\",\nmaneuver the @bug@ into the single gap in the wall below @win@.\nMove @win@ above the @bug@, then make the @bug@ move up.",
    },
  },
  ["14level"] = { -- core.pck
    [1] = {
      ["name"] = "Free Fire Text",
      [1] = "What unique obstacle does the @text@ on the @star@ pose?",
      [2] = "In order to break \"@fire@ is @defeat@\",\nwhat else must happen on that same turn?",
      [3] = "Solution on next page!",
      [4] = "Position the @still@ texts in a line so that,\nin one move, they replace both @defeat@ texts.",
    },
    [2] = {
      ["name"] = "Win",
      [1] = "How might you get the @win@ text around the bend?",
      [2] = "With only one @you2@ text, is there any way\nthat you could form \"@fire@ is @you2@\" and have \n@you2@ on the @star@?",
      [3] = "Solution on next page!",
      [4] = "Maneuver the vertical \"@fire@ is ...\" rule one space right, then \nform \"@fire@ is @you2@\" horizontally and push the @you2@ text onto the @star@. \nUse both controllable objects to retrieve the @win@ text.",
    },
  },
  ["11level"] = { -- the parchment
    [1] = {
      ["name"] = "Puzzle 1",
      [1] = "Take note of everything in the level. \nWhat could be considered a \"bath\"?",
      [2] = "When you first step on a bit of @water@, imagine \nthat you begin drawing an invisible line. Where \nmust this line go, and where can it never go?",
      [3] = "Solution on next page!",
      [4] = "Start on one bit of @water@, and get to the other without \nreturning to a space you've already been. Once there, \nreturn to the first @water@ while still avoiding previously traveled spaces.",
    },
    [2] = {
      ["name"] = "Puzzle 2",
      [1] = "You'll be needing 6 @rock@s for this. \nYou may need to find a better route for puzzle 1.",
      [2] = "How might the @reed@s and the @grass@ correspond to each other?",
      [3] = "The @reed@s' positions in the level don't necessarily \ncorrelate with the @grass@' positions, only the @reed@s \nwithin the same clusters matter.",
      [4] = "Solution on next page!",
      [5] = "The @reed@s surrounding the @water@ indicate where to position the @rocks@ \non the @grass@. The left cluster of @reed@s corresponds to the right \npatch of @grass@, and vice versa.",
    },
    [3] = {
      ["name"] = "Puzzle 3",
      [1] = "The order in which you solve the puzzles doesn't matter, \nyou need only know their clues beforehand.",
      [2] = "From the beginning, how might all 7 @rock@s form a line?",
      [3] = "There are only 2 possible positions for this line. \nCan you rule either one out?",
      [4] = "Solution on next page!",
      [5] = "Form a vertical line of @rock@s in the center column \nof the level. The @rock@ stuck at the top of the level \nwill be the topmost @rock@.",
    },
    [4] = {
      ["name"] = "Puzzle 4",
      [1] = "1, 2, 3, 4, 5, 6.",
      [2] = "What else within the level could the 6 be referring to?",
      [3] = "The numbers indicate an order.",
      [4] = "Solution on next page!",
      [5] = "Push the @rock@s off of the level in the following order: \nRight, Right, Left, Right, Left, Left.",
    },
  },
  ["10level"] = { -- rewritten
    [1] = {
      ["name"] = "Pass The Bolts",
      [1] = "What happens when something comes into contact with the @bolt@? \nWhat could be causing the @bolt@ to act this way?",
      [2] = "The @yellow@ and @red@ fields' colors are not arbitrary. \nWhy might they be causing objects to act in the way they are?",
      [3] = "To pass the second @bolt@, you'll need to get rid of it.",
      [4] = "Outside of \"@bolt@ is @text@\", there is only one other \nrule that can make the @bolt@ no longer be a @bolt@.",
      [5] = "The color fields prevent you from @push@ing anything \noutside of the leftmost room. How else might you \nfree up the objects left inside?",
      [6] = "Solution on next page!",
      [7] = "Form \"@text@ is @pull@\" to retrieve the @robot@ text, \nthen form \"@bolt@ is @robot@\".",
    },
    [2] = {
      ["name"] = "Win",
      [1] = "If @yellow@ means @open@ and @red@ means @shut@, \nwhat could white mean?",
      [2] = "You must gain two more controllable objects to reach the goal. \nWhat objects can be transformed, and what controllable \nobjects exist within the level?",
      [3] = "You will need two @robot@s within the level. \nIf you're missing one, you'll need to backtrack.",
      [4] = "The @robot@ at the entrance of the rightmost room \nwill need to be in its rightmost position.",
      [5] = "Do the white pixels work as @text@? \nWhat if @baba@ is standing on one?",
      [6] = "The rule \"@robot@ is @baba@\" will need to be made.",
      [7] = "Solution on next page!",
      [8] = "@baba@ acts as text while on a white pixel. \nUsing this, form \"@robot@ is @baba@\" and \"pixel is @baba@\" \nand cross to the monitor.",
    },
  },
  ["22level"] = { -- overcast digsite code
    [1] = {
      ["name"] = "Code",
      [1] = "How many squares are within the hint, \nand how might that correspond to the code?",
      [2] = "There are 26 squares within the hint, \nand 26 of something within the code level.",
      [3] = "Recall how the lamps worked in Overcast Digsite.",
      [4] = "Solution on next page!",
      [5] = "\"HOPING\"",
    },
  },
  ["23level"] = { -- core.pck code
    [1] = {
      ["name"] = "Code",
      [1] = "What \"bizarre broken text\" in core.pck \nis the clue referring to?",
      [2] = "Yes, the clue rhymes.",
      [3] = "You'll never have a clear vision of \nthe object within core.pck.",
      [4] = "Solution on next page!",
      [5] = "\"STAR\"",
    },
  },
  ["19level"] = { -- gnirevaw code
    [1] = {
      ["name"] = "Code",
      [1] = "Does the code mean anything, or is it gibberish?",
      [2] = "The codes refer to their respective levels. \nWhat information does the level provide?",
      [3] = "\"ELTIT\" is not a word. \nWhat could it mean, and is it similar \nto anything related to the level?",
      [4] = "Solution on next page!",
      [5] = "\"WAVERING\"",
    },
  },
  ["21level"] = { -- the parchment code
    [1] = {
      ["name"] = "Code",
      [1] = "Recall the second puzzle from The Parchment.",
      [2] = "In the second puzzle, two types of objects \nformed a clue. What objects were they?",
      [3] = "The rocks and foliage are unrelated to the code.",
      [4] = "Solution on next page!",
      [5] = "\"CLOUD\"",
    },
  },
  ["20level"] = { -- rewritten code
    [1] = {
      ["name"] = "Code",
      [1] = "White, yellow, and red... \nWhere have you seen these colors before?",
      [2] = "What property did each color field bestow \nupon objects in the level Rewritten?",
      [3] = "White - Word \nYellow - Open \nRed - Shut",
      [4] = "Solution on next page!",
      [5] = "\"RESPOND\"",
    },
  },
}

-- Adds button to menu
hintnum = 0
hintpage = 0
local old_pause = menufuncs.pause.enter
menufuncs.pause.enter = function(...)
  old_pause(...)
  createbutton("hint",50,screenh-20,2,4,1,"Hints","pause",3,2,menufuncs.pause.button)
end

buttonclick_list["hint"] = function(unitid)
  submenu("hint")
end

-- The actual hint pages
menufuncs.hint = {
  button = "Hints",
  escbutton = "hint_close",
  enter = function(parent,name,buttonid)
    local dynamic_structure = {}
    local x = screenw * 0.5
    local y = f_tilesize * 2
    local levelhints = hintdata[generaldata.strings[CURRLEVEL]]
    if levelhints ~= nil then
      writetext("What are you trying to do?",0,x,y,name,true,2)
      for i,data in ipairs(levelhints) do
        y = y + f_tilesize
        createbutton("goal"..i,x,y,2,12,1,data.name,name,1,3,buttonid)
        table.insert(dynamic_structure,{{"goal"..i}})
        buttonclick_list["goal"..i] = function(unitid)
          hintnum = i
          hintpage = 1
          submenu("viewhint")
        end
      end
      y = y + f_tilesize
    else
      writetext("No hints available for this level.",0,x,y,name,true,2)
    end
    y = y + f_tilesize
    createbutton("hint_close",x,y,2,12,1,langtext("editor_menu_close"),name,1,3,buttonid)
    table.insert(dynamic_structure,{{"hint_close"}})
    buildmenustructure(dynamic_structure)
  end,
  leave = function(parent,name,buttonid)
    MF_letterclear(name)
  end,
}
menufuncs.viewhint = {
  button = "HintView",
  escbutton = "hint_close",
  enter = function(parent,name,buttonid)
    local dynamic_structure = {}
    local x = screenw * 0.5
    local y = f_tilesize * 2
    local thishint = hintdata[generaldata.strings[CURRLEVEL]][hintnum]
    if thishint ~= nil then
      local totalpages = 1
      while thishint[totalpages] ~= nil do
        totalpages = totalpages + 1
      end
      totalpages = totalpages - 1

      writetext(thishint.name,0,x,y,name,true,2)
      y = y + f_tilesize
      local text = thishint[hintpage]

      local length = string.len(text)
      local i = 1
      while (i <= length) do
    		local letter = string.sub(text, i, i)

    		if (letter == "@") then
    			local sublength = 0
    			local command = ""

    			for j=i,length do
    				local subletter = ""

    				if (j < length) then
    					subletter = string.sub(text, j+1, j+1)
    				end

    				sublength = sublength + 1

    				if (subletter == "@") or (j == length) then
    					command = string.sub(text, i+1, j)
              local displayname = word_names[command] or command
              local object = colourstring(command,displayname)

    					text = string.gsub(text, "@" .. command .. "@", object)

              length = string.len(text)
    					i = i + string.len(object)
              letter = string.sub(text, i, i)
    					break
    				end
    			end
    		end

        if (letter == "\n") then
          local subtext = string.sub(text,1,i-1)
          text = string.sub(text,i+1)
          writetext(subtext,0,x,y,name,true,2)
          y = y + f_tilesize
          length = string.len(text)
          i = 0
    		end

    		i = i + 1
    	end

      writetext(text,0,x,y,name,true,2)
      y = f_tilesize * 6
      writetext("Hint "..hintpage.." / "..totalpages,0,x,y,name,true,2)
      y = y + f_tilesize
      createbutton("hint_back",x-100,y,2,6,1,"<",name,1,3,buttonid,(hintpage <= 1))
      createbutton("hint_forward",x+100,y,2,6,1,">",name,1,3,buttonid,(hintpage >= totalpages))
      table.insert(dynamic_structure,{{"hint_back"},{"hint_forward"}})
      y = y + f_tilesize
    else
      writetext("No data for this section.",0,x,y,name,true,2)
    end
    y = y + f_tilesize
    createbutton("hint_close",x,y,2,12,1,langtext("editor_menu_close"),name,1,3,buttonid)
    table.insert(dynamic_structure,{{"hint_close"}})
    buildmenustructure(dynamic_structure)
  end,
  leave = function(parent,name,buttonid)
    MF_letterclear(name)
  end,
}
buttonclick_list["hint_back"] = function(unitid)
  hintpage = hintpage - 1
  closemenu()
  submenu("viewhint")
end
buttonclick_list["hint_forward"] = function(unitid)
  hintpage = hintpage + 1
  closemenu()
  submenu("viewhint")
end
buttonclick_list["hint_close"] = function(unitid)
  closemenu()
end

-- Returns a string with colour from a string without colour.
function colourstring(text_,display_,custom)
  local text = text_ or ""
  local display = display_ or text
  local result = ""
  if string.sub(text,1,4) == "not " and custom == nil then
    local n1,n2 = getpalcolour("text_not","active")
    if n1 ~= 0 or n2 ~= 3 then
      result = "$"..n1..","..n2.."not "
    else
      result = "not "
    end
    text = string.sub(text,5)
    display = string.sub(display,5)
  end
  local c1,c2 = getpalcolour("text_"..text,"active",true)
  while string.sub(text,1,5) == "text_" and custom == nil and c1 == nil do
    local n1,n2 = getpalcolour("text_text_","active")
    if n1 ~= 0 or n2 ~= 3 or result ~= nil then
      result = result.."$"..n1..","..n2.."text_"
    else
      result = "text_"
    end
    text = string.sub(text,6)
    display = string.sub(display,6)
    c1,c2 = getpalcolour("text_"..text,"active",true)
  end
  if c1 == nil then
    c1,c2 = 0,3
  end
  local insert = custom or display
	-- metatext display
	if true then
		local textcount = 0
		insert,textcount = string.gsub(insert,"text_","")
		if insert == "" then
			insert = "text_"
			textcount = textcount - 1
		end
		if textcount > 0 then
			insert = insert .. "("..textcount..")"
		end
	end
  if (c1 ~= 0 or c2 ~= 3) then
    result = result .. "$"..c1..","..c2..insert.."$0,3"
  elseif result ~= "" then
    result = result .. "$0,3" .. insert
  else
    result = insert
  end
  return result
end

-- Returns the colour of the name entered from the object palette.
function getpalcolour(object,value_,checkexist)
  local name = ""
  if unitreference[object] ~= nil then
    name = unitreference[object]
  elseif objectpalette[object] ~= nil then
    name = objectpalette[object]
  end
  if #name > 0 then
    local unitinfo = tileslist[name]

  	local defaultcolour = colours.default

  	local value = value_ or "colour"

  	if (objectcolours[name] ~= nil) then
  		local c = objectcolours[name]

  		if (c[value] ~= nil) then
  			local cc = c[value]
  			return cc[1],cc[2]
  		elseif (value == "colour") or (unitinfo == nil) or (unitinfo[value] == nil) then
  			if (c.colour ~= nil) then
  				local cc = c.colour
  				return cc[1],cc[2]
  			end
  		end
  	end

  	if (unitinfo == nil) then
  		return defaultcolour[1],defaultcolour[2]
  	else
  		if (unitinfo[value] == nil) then
  			if (unitinfo.colour == nil) then
  				return defaultcolour[1],defaultcolour[2]
  			else
  				local colour = unitinfo.colour
  				return colour[1],colour[2]
  			end
  		else
  			local colour = unitinfo[value]
  			return colour[1],colour[2]
  		end
  	end

  	return defaultcolour[1],defaultcolour[2]
  end
  if checkexist then
    return nil
  end
  return 0,3
end
