
local SolutionPages = {
     {left  = {"$#24296F", "sys_solution_1", "$#000000\n", "sys_solution_2"   }, bg = "solution_page_1"  }, -- 1
     {right = {"$#24296F", "sys_solution_3", "$#000000\n", "sys_solution_4"   }, bg = "solution_page_2"  },  -- 2
     {right = {"sys_solution_5"},                     bg="solution_page_3"    },  -- 3
     {left  = {"$#24296F","sys_solution_6","$#000000\n", "sys_solution_7"     }, bg = "solution_page_4"  },-- 4
     {left  = {"sys_solution_8"},                     bg="solution_page_5"    },-- 5
     {left  = {"$#24296F", "sys_solution_9", "$#000000\n", "sys_solution_10"  }, bg = "solution_page_6"  },-- 6
     {right = {"sys_solution_12", "sys_solution_13"}, bg="solution_page_7"    }, -- 7
     {right = {"$#24296F","sys_solution_14","$#000000\n", "sys_solution_15"   }, bg = "solution_page_8"  }, -- 8
     {left  = {"$#24296F","sys_solution_16", "$#000000\n","sys_solution_17"   }, bg = "solution_page_9"  },-- 9
     {left  = {"sys_solution_18"},                    bg="solution_page_10"   },-- 10
     {right = {"$#24296F", "sys_solution_19", "$#000000\n", "sys_solution_20" }, bg = "solution_page_11" }, -- 11
     {left  = {"sys_solution_21"},                    bg="solution_page_12"   } -- 12
}

local LastPage = 1
function OnShow()
  ShowPage()
  s.sys_menu_solution = 1
end

function OnHide()
  s.sys_menu_solution = 0
  for i=1,12 do 
    UI:UnloadResource("solution_page_"..tostring(i))
  end
  UI:UnloadResource("solution_bg")
  table.insert(_G.FunctionsToDoAfterFrame, function() UI:DelWidget(self:GetName()) end)
end

function OnClose()
  self:SetVisible(false)
  Widgets.MainMenu.CloseSolution()
end

function OnMouseEnter(_sender)
  SetInterfaceCaption(L10n:ExpandString(_sender:GetCaption()))
end

function OnMouseLeave()
  SetInterfaceCaption("")
end

function OnPrevPage() if LastPage > 1 then LastPage = LastPage -1 end ShowPage() end
function OnNextPage() if LastPage < #SolutionPages then LastPage = LastPage + 1 end ShowPage() end

function ShowPage()
  local function setText(txt, name, t)
    local textSet = SolutionPages[t][name]
    local text = ""
    if textSet ~= nil then
      for i = 1,#textSet do 
        if    string.sub(textSet[i], 1, 1) == "$" then text = text..string.sub(textSet[i], 2  )
        else  text = text.."\n\t"..L10n:ExpandString(textSet[i]) end
      end
      txt:SetText(text)
      txt:SetVisible(true)
    else
      txt:SetVisible(false)
    end
  end
 
  local img   = self:FindChild("content")
  local left  = self:FindChild("SolutionLeftPage")
  local right = self:FindChild("SolutionRightPage")
  
  setText(left, "left", LastPage)
  setText(right, "right", LastPage)
  
  if SolutionPages[LastPage]["bg"] ~= nil then 
    img:SetSkin(SolutionPages[LastPage]["bg"])
  else
    img:SetSkin("solution_page_1")
  end
  UpdateArrowsVisible()
end

function UpdateArrowsVisible()

  self:FindChild("PrevPage"):SetVisible( LastPage > 1 )
  self:FindChild("NextPage"):SetVisible( LastPage < #SolutionPages )
    
  if self:FindChild("NextPage"):IsVisible() then
    self:FindChild("NextPage"):SetSkin("EmptyFrame")
    self:FindChild("NextPage"):SetSkin("notes_arrow_right")
    self:FindChild("NextPage"):SetText("sys_next_page")
  end
  
  if self:FindChild("PrevPage"):IsVisible() then
    self:FindChild("PrevPage"):SetSkin("EmptyFrame")
    self:FindChild("PrevPage"):SetSkin("notes_arrow_left")
    self:FindChild("PrevPage"):SetText("sys_prev_page")
  end
end
