---
--- @package   eg4-crank-ui
--- @file      driver-information.lua
--- @brief     functionality related setting and showing the departure time 
---
--- @copyright Copyright 2021 Hanover Displays Limited.
--- @license   This program is the confidential and proprietary product of
---            Hanover Displays Limited. Any unauthorised use, reproduction or
---            transfer of this program is strictly prohibited. (Subject to 
---            limited distribution and restricted disclosure only.) All 
---            rights reserved.                                              
---

---
-- @brief  Clear departure time by hiding the tile and clearing the text
-- @param gre#context mapargs
--
local function DepartureTimeCleared()
  local group = "time_of_departure_group"
  gre.set_value("home_tiles_layer."..group..".departure_info_message.text", "")
  DeactivateTile(group)
end

---
-- @brief  Handle departure time cleared by hiding the tile and clearing the text
-- @param gre#context mapargs
--
function CBDepartureTimeCleared(mapargs)
  DepartureTimeCleared()
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 UpdateDepartureTile(text)
  local group = "time_of_departure_group"
  gre.set_value("home_tiles_layer."..group..".departure_info_message.text", text)
  ActivateTile(group)
end

---
-- @brief  Handle the new departure time by showing the tile and adding the text
-- @param gre#context mapargs
--
function CBDepartureTimeMessage(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
  UpdateDepartureTile(text)
end

---
-- @brief  Updates the departure time icon.
--
function UpdateDepartureTimeIcon() 
  -- Check if category time or countdown
  local category = sbsettings.get_departure_time_category()
  if category == 1 or category == 2 then
    SetHomeScreenIcon("departure_time_control", 0)
  else
    SetHomeScreenIcon("departure_time_control", 1)
    DepartureTimeCleared()
  end
end

---
-- @brief  Sends the clear departure time to the backend
--- @param gre#context mapargs
function CBClearDepartureTime(mapargs) 
  SendEvent("clear_departure_time")
  TransitionBackScreen(mapargs)
end

---
-- @brief  Fetches the hour:minute from the setting controls
-- @return string formatted as HH:MM in 24 hout clock.
local function GetTimeSetting()
  local layer = "departure_time_layer"
  local hour = GetRow(layer..".hour.value")
  if gre.get_value(layer..".am_pm.grd_hidden") == 1 then
    hour = hour - 1
  else
    local am_pm = GetRow(layer..".am_pm.value")
    if am_pm == 2 then
      hour = hour + 12
    end
  end
  local minute = GetRow(layer..".minute.value") - 1

  return string.format("%02d:%02d", hour, minute)  
end

-- @brief  Sends the departure time to the backend
--- @param gre#context mapargs
function CBSetDepartureTime(mapargs) 
  SendText("set_departure_time", GetTimeSetting())
  TransitionBackScreen(mapargs)
end

-- @brief  Starts the setting of the departure time/countdown. 
--         Selectes screen depends on mode.
--- @param gre#context mapargs
function CBStartDepartureTimeSetting(mapargs)
  if IconMenuOpening() then return end
  if sbsettings.get_departure_time_category() == 1 then
    InitTimeScreen("departure_time_layer", os.date("*t"))
    ScreenTransition("bd_departure_time")
  else
    ScreenTransition("bd_departure_countdown")
  end
end

-- @brief  Sends the selected departure countdown to the backend. 
--- @param gre#context mapargs
function CBSetDepartureCountdown(mapargs)
  if tonumber(mapargs.value) > 0 then 
    SendText("set_departure_countdown", tostring(mapargs.value))
    TransitionBackScreen(mapargs)
  end
end


-- @brief  Sends the selected departure countdown to the backend. 
--- @param gre#context mapargs
function CBClearDepartureCountdown(mapargs) 
  SendEvent("clear_departure_countdown")
  TransitionBackScreen(mapargs)
end

-- @brief  Sends the clear departure time/countdown to the backend depending on current mode. 
--- @param gre#context mapargs
function CBClearDepartureInfo(mapargs)
  if ScrollActive() then return end
  if sbsettings.get_departure_time_category() == 1 then
    CBClearDepartureTime(mapargs)
  else
    CBClearDepartureCountdown(mapargs)
  end
end

-- @brief  Updates departure time with new substitution text 
-- @param  substition_text - new string to use.
function SetDepartureTime(substition_text)
  if sbsettings.get_departure_time_category() == 1 then
    -- Check if string indicates cleared
    if substition_text == "" or substition_text == "--:--" then
      DepartureTimeCleared()
    else
      local text = gre.get_value("home_tiles_layer.time_of_departure_group.departure_time_message.text")
      text = ProcessSubstition(text)
      UpdateDepartureTile(text)
    end
  end
end
