--[[
  @package    eg4-crank-ui-2
  @file       paging-rich-text.lua
  @brief      Supports the paging rich text control which has unique formatting.
]]

local pages_nav_rich_text_bold_format = '<p style="text-align:center"><span style="color:#%x"><b>%d</b></span><span style="color:#%x"><b>/%d</b></span></p>'
local pages_nav_rich_text_normal_format = '<p style="text-align:center"><span style="color:#%x">%d</span><span style="color:#%x">/%d</span></p>'

---
-- @brief  Make the page navigation text containing the current page index and
--         total pages using the defined rich text format
-- @param  index        The current page index
-- @param  total_pages  The total number of pages
-- @param  format       The rich text format to use (normal or bold, default is normal)
-- @return The rich text string
function make_page_text(current_page, total_pages, format)
  local fmt = (format == "bold") and pages_nav_rich_text_bold_format or pages_nav_rich_text_normal_format
  local color_normal = gre.get_value("color_header_text")
  local color_dim = gre.get_value("color_header_inactive_text")
  return string.format(fmt, color_normal, current_page, color_dim, total_pages)
end

---
--- @brief  Collect the page index from the rich text string. This
---         finds the instance of text after the pages_nav_index_marker from a string
--- @param  context_screen The current screen
--- @return The current page index
function get_page_index(context_screen)
  local text = gre.get_value(context_screen..".paging_rich_text")
  local _, stop = text:find("color:", 1)
  if stop == nil then return 1 end
  local _, stop = text:find(">", stop + 1)
  if stop == nil then return 1 end
  local index = text:match("%d+", stop + 1)
  return tonumber(index) or 1
end

---
--- @brief  Builds a rich text string with the filtered text highlighted
--- @param  text   The text to highlight
--- @param  filter The filter text
--- @param  normal_color The normal text color
--- @param  select_color The selected text color
--- @return The rich text string
function highlight_filtered_text(text, filter, normal_color, select_color)
  local span = '<span style="color:#%x">%%s</span>'
  local normal = span:format(normal_color)
  local select = span:format(select_color)
  local result = '<p style="text-align:left">'

  if normal_color == select_color then
    return result..normal:format(text)..'</p>'
  end

  local result = ""
  for c in utf8_iter(text) do
    if c == "<" then result = result.."&lt;"
    elseif c == ">" then result = result.."&gt;"
    else result = result..c
    end
  end
  text = result

  local item = text:remove_accents():upper()
  local index = 0
  local pos = 1
  local result = ""
  for word in filter:gmatch("%S+") do
    local i,j = item:find(word, index)
    if i == nil then break end
    result = result..normal:format(text:sub(pos, utf8.offset(text, i) - 1))..select:format(text:sub(utf8.offset(text, i), utf8.offset(text, j + 1) - 1))
    index = j + 1
    pos = utf8.offset(text, index)
  end

  return result..(pos and normal:format(text:sub(pos)) or '')..'</p>'
end