--[[
  @package    eg4-crank-ui-2
  @file       home-status-tiles.lua
  @brief      Functionality related setting and showing the departure time
]]


local TILE = "departure_time"
local DEPARTURE_TIME_SETTING_LAYER = "departure_time_setting_layer"

---
--- @brief  Checks if the departure time icon should be shown
--- @return true if icon should be shown
function show_departure_time()
  -- Check if category time or countdown
  local category = sbsettings.get_departure_time_category()
  if category == 1 or category == 2 then
    return true
  end
  deactivate_tile(TILE)
  return false
end

---
--- @brief  Updates the departure time with new text and starts the animation if required.
--- @param  text - string to display on the departure time tile
local function update_departure_tile(text)
  gre.set_value("status_tiles_layer."..TILE..".message.text", text)
  activate_tile(TILE)
end

---
--- @brief  Handle the new departure time by showing the tile and adding the text
--- @param gre#context mapargs
function cb_departure_time_message(mapargs)
  -- Limit to 2 line of text only
  local text = mapargs.context_event_data.text
  local lines = text:split("\n")
  if #lines > 2 then
    text = lines[1] .. lines[2]
  end
  update_departure_tile(text)
end

---
--- @brief  Sends the clear departure time to the backend
--- @param gre#context mapargs
function cb_clear_departure_time(mapargs)
  send_event("clear_departure_time")
  screen_transition_idle()
end

---
--- @brief  Sends the departure time to the backend
--- @param gre#context mapargs
function cb_set_departure_time(mapargs)
  send_event("set_departure_time", get_time_setting(DEPARTURE_TIME_SETTING_LAYER))
  screen_transition_idle()
end

function cb_init_set_departure_time(mapargs)
  init_set_time(DEPARTURE_TIME_SETTING_LAYER, os.date("*t"))
end

---
--- @brief  Starts the setting of the departure time/countdown.
---         Selectes screen depends on mode.
--- @param gre#context mapargs
function cb_start_setting_of_departure(mapargs)
  -- Check if category time or countdown
  if sbsettings.get_departure_time_category() == 1 then
    screen_transition("set_departure_time")
  else
    cb_numeric_entry_start(mapargs)
  end
end

---
--- @brief  Sends the selected departure countdown to the backend.
--- @param value - countdown value to send
function cb_set_departure_countdown(value)
  if tonumber(value) == 0 then return end
  send_event("set_departure_countdown", tostring(value))
  screen_transition_idle()
  return true
end


-- @brief  Sends the selected departure countdown to the backend.
--- @param gre#context mapargs
function cb_clear_departure_countdown(mapargs)
  send_event("clear_departure_countdown")
  screen_transition_idle()
end

---
--- @brief  Sends the clear departure time/countdown to the backend depending on current mode.
--- @param gre#context mapargs
function cb_clear_departure_info(mapargs)
  -- Check if category time or countdown
  if sbsettings.get_departure_time_category() == 1 then
    cb_clear_departure_time(mapargs)
  else
    cb_clear_departure_countdown(mapargs)
  end
end

---
--- @brief  Updates departure time with new substitution text
--- @param  substition_text - new string to use.
function set_departure_time(substition_text)
  -- Check if category time
  if sbsettings.get_departure_time_category() == 1 then
    -- Check if string indicates cleared
    if substition_text == "" or substition_text == "--:--" then
      deactivate_tile(TILE)
    else
      local text = gre.get_value("status_tiles_layer."..TILE..".message.departure_time_text")
      update_departure_tile(process_substition(text))
    end
  end
end
