
function render_alert(msg, colour)
  local alert_width = (string.len(msg) * 8)
  local alert_height = 8
  render_centrebox((alert_width + 20), 20, purple, 1, white)
  screen:print((((screen_width - alert_width) / 2) + shadow_xoffset), (((screen_height - alert_height) / 2) + shadow_yoffset), msg, black)
  screen:print(((screen_width - alert_width) / 2), ((screen_height - alert_height) / 2), msg, colour)
end

function render_confirm(msg, colour, confirm_option)

  msgconfirm = msg
  msgcolour = colour
  alert_width = (string.len(msg) * 8)
  alert_height = 8
  msgyes = "YES"
  msgyes_width = (string.len(msgyes) * 8)
  msgno = "NO"
  msgno_width = (string.len(msgno) * 8)

  if confirm_option then
    msgyes_colour = oncol
    msgno_colour = offcol
  else
    msgyes_colour = offcol
    msgno_colour = oncol
  end
  
  -- render box
  render_centrebox((alert_width + 20), 40, purple, 1, white)

  -- render message
  screen:print((((screen_width - alert_width) / 2) + shadow_xoffset), ((((screen_height - alert_height) / 2) - 8) + shadow_yoffset), msgconfirm, black)
  screen:print(((screen_width - alert_width) / 2), (((screen_height - alert_height) / 2) - 8), msgconfirm, msgcolour)

  -- render yes/no
  screen:print(((((screen_width - msgyes_width) / 2) - 24) + shadow_xoffset), ((((screen_height - alert_height) / 2) + 8) + shadow_yoffset), msgyes, black)
  screen:print((((screen_width - msgyes_width) / 2) - 24), (((screen_height - alert_height) / 2) + 8), msgyes, msgyes_colour)
  screen:print(((((screen_width - msgno_width) / 2) + 24) + shadow_xoffset), ((((screen_height - alert_height) / 2) + 8) + shadow_yoffset), msgno, black)
  screen:print((((screen_width - msgno_width) / 2) + 24), (((screen_height - alert_height) / 2) + 8), msgno, msgno_colour)

  padmode = "confirm"

end

function render_file_operations(selected_operation)

  -- file operations
  file_operations = {}
  if table.getn(selected_files) >= 1 then
    table.insert(file_operations, { name = "Cut item(s)", action = "file_cut()" })
  end
  if table.getn(selected_files) >= 1 then
    table.insert(file_operations, { name = "Copy item(s)", action = "file_copy()" })
  end
  if table.getn(cut_files) >= 1 or table.getn(copy_files) >= 1 then
    table.insert(file_operations, { name = "Paste item(s)", action = "file_paste()" })
  end
  if table.getn(selected_files) >= 1 then
    table.insert(file_operations, { name = "Delete item(s)", action = "file_delete()" })
  end
  table.insert(file_operations, { name = "Select all", action = "file_select(\"" .. currentdir .. "\", nil, \"all\")" })
  if table.getn(selected_files) >= 1 then
    table.insert(file_operations, { name = "Unselect all", action = "file_select(\"" .. currentdir .. "\", nil, \"none\")" })
  end
  table.insert(file_operations, { name = "Cancel", action = "" })

  if table.getn(file_operations) > 1 then

    msg = "Select option:"
    local alert_width = (string.len(msg) * 8)
    for k,v in pairs(file_operations) do
      if (string.len(v.name) * 8) > alert_width then
        alert_width = (string.len(v.name) * 8)
      end
    end
    local alert_height = ((table.getn(file_operations) * 14) + 10)

    -- render box
    render_centrebox((alert_width + 20), (alert_height + 20), purple, 1, white)

    -- render message
    screen:print((((screen_width - alert_width) / 2) + shadow_xoffset), (((screen_height - alert_height) / 2) + shadow_yoffset), msg, black)
    screen:print(((screen_width - alert_width) / 2), ((screen_height - alert_height) / 2), msg, white)

    -- render file operations
    local xops = ((screen_width - alert_width) / 2)
    local yops = (((screen_height - alert_height) / 2) + 14)
    for k,v in pairs(file_operations) do
      screen:print((xops + shadow_xoffset), (yops + shadow_yoffset), v.name, black)
      if k == selected_operation then
        screen:print(xops, yops, v.name, oncol)
      else
        screen:print(xops, yops, v.name, offcol)
      end
      yops = yops + 14
    end

    padmode = "file_operations"

  else

    render_alert("No files selected", white)

  end

end

function render_centrebox(width, height, colour, border_width, border_colour)
  if border_width >= 1 then
    screen:fillRect(((screen_width - (width + (border_width * 2))) / 2), ((screen_height - (height + (border_width * 2))) / 2), (width + (border_width * 2)), (height + (border_width * 2)), border_colour)
  end
  screen:fillRect(((screen_width - width) / 2), ((screen_height - height) / 2), width, height, colour)
end

function render_statmsg(msg, colour)
--  screen:print(10, 240, msg, colour)
end

function render_filename(filename)
  if string.len(filename) >= max_filechars then
    return string.sub(filename, 1, (max_filechars - 3)) .. "..."
  else
    return filename
  end
end

function render_selected_files()
  local y = 170
  for k,v in pairs(selected_files) do
    screen:print((10 + shadow_xoffset), (y + shadow_yoffset), k .. " = " .. v, black)
    screen:print(10, y, k .. " = " .. v, white)
    y = y + 10
  end
end

function render_filesize(filesize)
  if filesize >= (1024 * 1024 * 1024) then
    return string.format("%.0f", (filesize / 1024 / 1024 / 1024)) .. " Gb"
  end
  if filesize >= (1024 * 1024) then
    return string.format("%.0f", (filesize / 1024 / 1024)) .. " Mb"
  end
  if filesize >= (1024) then
    return string.format("%.0f", (filesize / 1024)) .. " Kb"
  end
  if filesize <= 1023 then
--    return filesize .. " b "
    return "1 Kb"
  end
end

function render_filelist()

  -- display background image
  screen:blit(0, 0, background, 0, 0, background:width(), background:height(), false)

  -- print current directory
  screen:print((10 + shadow_xoffset), (10 + shadow_yoffset), "[" .. currentdir .. "]", black)
  screen:print(10, 10, "[" .. currentdir .. "]", white)

  local xposname = 10
  local xpossize = 420
  local ypos = 20
  
  i = 1
  i2 = start_pointer
  if num_files < num_listings then
    end_pointer = num_files
  else
    end_pointer = num_listings
  end
  while i <= end_pointer do

    -- concatenate path/file
    local path_file = remove_trailing_slash(currentdir) .. "/" .. files[i2]["name"]

    -- render text
    if i2 == menu_pointer then
      showcol = oncol
    elseif in_array(cut_files, (path_file)) then
      showcol = cutcol
    elseif in_array(copy_files, (path_file)) then
      showcol = copycol
    elseif in_array(selected_files, (path_file)) then
      showcol = selectedcol
    else
      showcol = offcol
    end
    screen:print((xposname + shadow_xoffset), (ypos + shadow_yoffset), render_filename(files[i2]["name"]), black)
    screen:print(xposname, ypos, render_filename(files[i2]["name"]), showcol)
    if files[i2]["directory"] == true then
      screen:print((xpossize + shadow_xoffset), (ypos + shadow_yoffset), pad_string("left", 7, "<dir>"), black)
      screen:print(xpossize, ypos, pad_string("left", 7, "<dir>"), showcol)
    else
      screen:print((xpossize + shadow_xoffset), (ypos + shadow_yoffset), pad_string("left", 7, render_filesize(files[i2]["size"])), black)
      screen:print(xpossize, ypos, pad_string("left", 7, render_filesize(files[i2]["size"])), showcol)
    end
    ypos = ypos + 10
    i = i + 1
    i2 = i2 + 1
  end

  -- render selected files
--  render_selected_files()

  -- render tagline
  render_tagline()

end

function render_tagline()
--  screen:print(10, 250, "mp=" .. menu_pointer .. ", sp=" .. start_pointer .. ", ep=" .. end_pointer, white)
  local msg = "Nav=Up/Down | Enter=Cross | Cancel=Circle | Help=Select"
  screen:print((((screen_width - (string.len(msg) * 8)) / 2) + shadow_xoffset), (255 + shadow_yoffset), msg, black)
  screen:print(((screen_width - (string.len(msg) * 8)) / 2), 255, msg, grey)
end
