--[[
  @package eg4-crank-ui-2
  @file    screen-transition.lua
  @brief   Screen transition utility functions
]]

---
-- @brief  Send event to start the screen transition
-- @param  value  The screen to transition to
function screen_transition(value)
  gre.set_value("screen_transition", value)
  gre.send_event("screen_transition")
end

---
-- @brief  Send event to start the screen transition
-- @param  value  The screen to transition to
local idle_screen
function screen_transition_idle()
  screen_transition(idle_screen)
end
cb_screen_transition_idle = screen_transition_idle

---
--- @brief  Set the idle screen to the current screen
--- @param  gre#context mapargs
function set_screen_transition_idle(screen)
  idle_screen = screen
end

---
-- @brief  Callback to start the screen transition
-- @param  mapargs.screen  The next screen
function cb_screen_transition(mapargs)
  screen_transition(mapargs.screen or mapargs.context_control:object_name())
end

---
-- @brief  Callback to transition to the last screen
-- @param  mapargs.context_screen..".back_screen"  The screen to transition to
function cb_back_screen(mapargs)
  local context_screen = mapargs and mapargs.context_screen or gre.env("active_screen")
  local back_screen = gre.get_value(context_screen..".back_screen")
  if not back_screen then return end
  screen_transition(back_screen)
end

---
--- @brief  Optional callback to handle the cancelled action
--- @param  gre#context mapargs
function cb_action_cancelled(mapargs)
  local callback = gre.get_value(mapargs.context_screen..".cancelled_callback")
  if _G[callback] then
    _G[callback](mapargs)
    gre.set_value(mapargs.context_screen..".cancelled_callback", "")
  end
end

---
--- @brief  Determine whether the back screen should be set.
--- @param  context_screen  The current screen
--- @return true if the back screen should be set
local last_screen
function check_back_screen_set(context_screen)
if last_screen == context_screen then return end
return last_screen and gre.get_value(last_screen..".back_screen") ~= context_screen
end

---
-- @brief  Callback to set the back screen for the currrent screen
-- @param  mapargs.context_screen..".back_screen"  The screen to transition to
-- @note   last_screen is a global variable that is set to the current screen
--         so that the back_screen can be set on the next screen
function cb_set_back_screen(mapargs)
  if check_back_screen_set(mapargs.context_screen) then
    -- Only set the back screen if the last screen's back screen was not this screen.
    -- Note: If the last screen's back screen is this screen then we are navigating up the menu tree.
    gre.set_value(mapargs.context_screen..".back_screen", last_screen)
  end
  -- Check if the new screen should skip being logged as a back screen for the next screen
  local ignore = gre.get_value(mapargs.context_screen..".ignore_back_screen")
  if ignore == true or tonumber(ignore) == 1 then return end
  last_screen = mapargs.context_screen
end

function clear_last_screen(mapargs)
  last_screen = nil
end