---
-- @package   eg4-crank-ui
-- @file      display-brightness.lua
-- @brief     Handles display brightness settings.
--
-- @copyright Copyright 2019 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.                                              
--
--

local brightness_update_timer_id = nil
local current_theme
local display_inactivity_timeout
local display_off_timeout

---
--- @brief  Send a read display brightness request to the backend
---
function CBReadDisplayBrightness()
  local min, max = sbsettings.get_brightness_limits()
  gre.set_value("display_control_layer.menu_items.display_brightness_slider.min_value", min)
  gre.set_value("display_control_layer.menu_items.display_brightness_slider.max_value", max)

  local value = (sbsettings.get_backlight_mode() == 0) and sbsettings.get_manual_brightness() or sbsettings.get_display_brightness()
  SetSliderPosition("display_control_layer.menu_items.display_brightness_slider", value)
end

---
--- @brief  Callback to start the periodic brightness updates
---
function CBStartPeriodicBrightnessUpdates()
  brightness_update_timer_id = gre.timer_set_interval(CBReadDisplayBrightness, 2000)
end

---
--- @brief  Callback to stop the periodic brightness updates
---
function CBStopPeriodicBrightnessUpdates()
  if brightness_update_timer_id ~= nil then
    gre.timer_clear_timeout(brightness_update_timer_id)
    brightness_update_timer_id = nil;
  end
end

---
--- @brief  Sets limits value text.
---
local function SetAutoThemeThresholds(min, max)
  local scaled = string.format("%d%% - %d%%", min, max)
  gre.set_value("auto_theme_thresholds_layer.percentage_range_control.text", scaled)
end

---
--- @brief  Callback to process the current brightness settings event
--- @param  gre#context mapargs
---
function CBProcessCurrentDisplayBrightness(mapargs) 
  if mapargs.context_event_data.text == nil then
    return
  end
  local settings = assert(loadstring(mapargs.context_event_data.text))()
  if settings ~= nil then

    SBCSetRangeSlider("auto_theme_thresholds_layer.range_slider", settings.auto_dark_theme_threshold / 100, settings.auto_light_theme_threshold / 100)
    SetAutoThemeThresholds(settings.auto_dark_theme_threshold, settings.auto_light_theme_threshold)

  end
end

---
--- @brief  Callback for the Display brightness limits setting
--- @param  value - value for slider
--- @param  active - true the slider is being moved.
---
function CBUpdateBusDriverDisplayBrightness(value, active)
  sbsettings.set_manual_brightness(value)
  if brightness_update_timer_id ~= nil then
    CBStopPeriodicBrightnessUpdates()
    CBStartPeriodicBrightnessUpdates()
  end
end

---
--- @brief  Callback for the auto theme light/dark value settings
--- @param  value1 - value for slider 1
--- @param  value2 - value for slider 2
--- @param  active - true the slider is being moved.
---
function CBUpdateAutoThemeThresholds(value1, value2, active)

  local limit_min = math.floor(value1 * 100 + 0.5)
  local limit_max = math.floor(value2 * 100 + 0.5)
  SetAutoThemeThresholds(limit_min, limit_max)
end

---
--- @brief  Callback to update the auto display theme
--- @param  gre#context mapargs
---
function CBUpdateAutoTheme(mapargs)
  mapargs.value = mapargs.value and tonumber(mapargs.value) or sbsettings.get_auto_theme()
  sbsettings.set_auto_theme(mapargs.value)
  CBUpdateDisplayTheme({})
end

---
--- @brief  Callback to update the display theme
--- @param  gre#context mapargs
---
function CBUpdateDisplayTheme(mapargs)
  mapargs.value = mapargs.value and tonumber(mapargs.value) or sbsettings.get_theme()
  if mapargs.value == 2 then
    mapargs.value = sbsettings.get_auto_theme()
  end
  gre.set_value("font_color_normal", (mapargs.value == 1) and 0xFFFFFF or 0x000000)

  local theme = (mapargs.value == 1) and "dark" or "light"  
  if theme ~= (current_theme or fileinfo:execute("basename $(readlink -f /usr/share/eg4-crank-ui/images)")) then
    current_theme = theme
    os.execute("ln -snf /usr/share/eg4-crank-ui/themes/" .. theme .. " /mnt/hos-data/g4/eg4-crank-ui/images")
    gre.dump_resource("image")
    gre.redraw()
  end
end

---
--- @brief  Update the display inactivity timeout value
---
function CBUpdateDisplayInactivityTimeout(mapargs)
  display_inactivity_timeout = mapargs.value
  CBStartNoActivityTimer()
end

---
--- @brief  Get the display inactivity timeout value
---
function GetDisplayInactivityTimeout()
  display_inactivity_timeout = display_inactivity_timeout or sbsettings.get_display_inactivity_timeout()
  return display_inactivity_timeout
end

---
--- @brief  Update the display off timeout value
---
function CBUpdateDisplayOffTimeout(mapargs)
  display_off_timeout = mapargs.value
  CBStartNoActivityTimer()
end

---
--- @brief  Get the display off timeout value
---
function GetDisplayOffTimeout()
  display_off_timeout = display_off_timeout or sbsettings.get_display_off_timeout()
  return display_off_timeout
end

---
--- @brief  Get the display off timeout value
---
function CBUpdateDisplayBrightnessIndicator(mapargs)
  local value = sbsettings.get_ambient_light_level()
  if value < 0 then value = 0 
  elseif value > 100 then value = 100 end
  value = (818 - 32) * value / 100 + 32
  gre.set_value(mapargs.context_layer..".current_brightness.grd_x", value)
end

-- EOF --
