--[[
  @package eg4-crank-ui-2
  @file    themes.lua
  @brief   Handles the display theme
]]


local function themes()
  return {
    color_default_background                              = { dark = 0x000000, light = 0xFFFFFF },
    color_hanover_background_logo                         = { dark = 0x17181B, light = 0xEBEEF1 },
    color_hanover_logo_red                                = { dark = 0xF03223, light = 0xF03223 },
    color_hanover_logo_text                               = { dark = 0x9AA0A6, light = 0x282A2D },

    color_primary_button_background                       = { dark = 0x60A5FF, light = 0x2C87FF },
    color_primary_button_text                             = { dark = 0x0E1013, light = 0xFFFFFF },
    color_primary_button_pressed_background               = { dark = 0x345076, light = 0xCEE3FF },
    color_primary_button_pressed_text                     = { dark = 0xE8EAED, light = 0x121C29 },

    color_secondary_button_background                     = { dark = 0xBDC1C6, light = 0xC0C9D5 },
    color_secondary_button_text                           = { dark = 0x17181B, light = 0x121C29 },
    color_secondary_button_pressed_background             = { dark = 0x345076, light = 0x86BAFF },
    color_secondary_button_pressed_text                   = { dark = 0xE8EAED, light = 0x121C29 },

    color_icon_menu_background                            = { dark = 0x17181B, light = 0x1F3047 },
    color_icon_menu_icon_background                       = { dark = 0xBDC1C6, light = 0xFFFFFF },
    color_icon_menu_icon_text                             = { dark = 0x000000, light = 0x121C29 },
    color_icon_menu_icon_pressed_background               = { dark = 0x345076, light = 0xCEE3FF },
    color_icon_menu_icon_pressed_text                     = { dark = 0xF1F3F4, light = 0x2C87FF },
    color_icon_menu_text                                  = { dark = 0xBDC1C6, light = 0xFFFFFF },

    color_navigation_separator                            = { dark = 0x282A2D, light = 0xC0C9D5 },
    color_navigation_separator_scroll_hint                = { dark = 0xDADCE0, light = 0x2C87FF },
    color_navigation_button_background                    = { dark = 0x2E3134, light = 0xBEDAFF },
    color_navigation_button_text                          = { dark = 0xBDC1C6, light = 0x121C29 },
    color_navigation_button_pressed_background            = { dark = 0x345076, light = 0x86BAFF },
    color_navigation_button_pressed_text                  = { dark = 0xE8EAED, light = 0x121C29 },
    color_navigation_alt_button_background                = { dark = 0x2E3134, light = 0xE8EAED },
    color_navigation_alt_button_text                      = { dark = 0xBDC1C6, light = 0x17181B },
    color_navigation_alt_button_pressed_background        = { dark = 0x345076, light = 0xCEE3FF },
    color_navigation_alt_button_pressed_text              = { dark = 0xE8EAED, light = 0x121C29 },

    color_destination_inactive_text                       = { dark = 0x9AA0A6, light = 0x121C29 },
    color_destination_active_text                         = { dark = 0xBDC1C6, light = 0x17181B },

    color_keypad_background                               = { dark = 0x0E1013, light = 0xC0C9D5 },
    color_keys_normal_background                          = { dark = 0x282A2D, light = 0xFFFFFF },
    color_keys_normal_text                                = { dark = 0xE8EAED, light = 0x121C29 },
    color_keys_primary_background                         = { dark = 0x60A5FF, light = 0x2C87FF },
    color_keys_primary_text                               = { dark = 0x0E1013, light = 0xFFFFFF },
    color_keys_secondary_background                       = { dark = 0x3C4043, light = 0xCEE3FF },
    color_keys_secondary_text                             = { dark = 0xE8EAED, light = 0x121C29 },

    color_numeric_entry_heading                           = { dark = 0xBDC1C6, light = 0x17181B },
    color_numeric_entry_hint                              = { dark = 0x5F6368, light = 0x345076 },
    color_numeric_entry_error_text                        = { dark = 0xED4541, light = 0xF03223 },
    color_numeric_entry_box_background                    = { dark = 0x17181B, light = 0xFFFFFF },
    color_numeric_entry_box_text                          = { dark = 0xBDC1C6, light = 0x17181B },
    color_numeric_entry_box_cursor                        = { dark = 0xBDC1C6, light = 0x2C87FF },
    color_numeric_entry_box_icon                          = { dark = 0xBDC1C6, light = 0x8FA0B7 },
    color_numeric_entry_box_icon_pressed_background       = { dark = 0x345076, light = 0xCEE3FF },
    color_numeric_entry_box_inner_border                  = { dark = 0x60A5FF, light = 0x2C87FF },
    color_numeric_entry_box_outer_border                  = { dark = 0x003069, light = 0x6BABFF },
    color_numeric_entry_box_error_inner_border            = { dark = 0xF03223, light = 0xF03223 },
    color_numeric_entry_box_error_outer_border            = { dark = 0x54120C, light = 0xED4541 },

    color_text_entry_box_background                       = { dark = 0x17181B, light = 0x090E15 },
    color_text_entry_box_text                             = { dark = 0xBDC1C6, light = 0xFFFFFF },
    color_text_entry_box_cursor                           = { dark = 0xBDC1C6, light = 0xE8EAED },
    color_text_entry_box_icon                             = { dark = 0xBDC1C6, light = 0xBDC1C6 },
    color_text_entry_box_separator                        = { dark = 0x2E3134, light = 0x2E3134 },
    color_text_entry_box_hint                             = { dark = 0x5F6368, light = 0xC0C9D5 },
    color_text_entry_box_inner_border                     = { dark = 0x60A5FF, light = 0x2C87FF },
    color_text_entry_box_outer_border                     = { dark = 0x003069, light = 0x143D73 },
    color_text_entry_box_error_inner_border               = { dark = 0xF03223, light = 0xF03223 },
    color_text_entry_box_error_outer_border               = { dark = 0x54120C, light = 0x54120C },
    color_entry_cursor_locator                            = { dark = 0x60A5FF, light = 0x2C87FF },

    color_header_background                               = { dark = 0x000000, light = 0x1F3047 },
    color_header_text                                     = { dark = 0xBDC1C6, light = 0xFFFFFF },
    color_header_separator                                = { dark = 0x2E3134, light = 0x2F486A },
    color_header_focus_text                               = { dark = 0x60A5FF, light = 0x60A5FF },
    color_header_inactive_text                            = { dark = 0x5F6368, light = 0xA5B4C8 },
    color_header_pressed_background                       = { dark = 0x345076, light = 0xCEE3FF },
    color_header_pressed_text                             = { dark = 0xE8EAED, light = 0x121C29 },
    color_header_button_background                        = { dark = 0x60A5FF, light = 0x60A5FF },
    color_header_button_text                              = { dark = 0x17181B, light = 0x17181B },
    color_header_button_border                            = { dark = 0x003069, light = 0x003069 },
    color_header_tile_background                          = { dark = 0x202124, light = 0x4D607A },
    color_header_tile_text                                = { dark = 0xBDC1C6, light = 0xEBEEF1 },
    color_header_tile_button_background                   = { dark = 0xBDC1C6, light = 0xEBEEF1 },
    color_header_tile_button_text                         = { dark = 0x17181B, light = 0x1F3047 },

    color_list_background                                 = { dark = 0x17181B, light = 0xFFFFFF },
    color_list_selected_background                        = { dark = 0xF03223, light = 0xF03223 },
    color_list_separator                                  = { dark = 0x2E3134, light = 0xC0C9D5 },
    color_list_text_option                                = { dark = 0xBDC1C6, light = 0x121C29 },
    color_list_text_value                                 = { dark = 0x5F6368, light = 0x8FA0B7 },
    color_list_text_title                                 = { dark = 0x60A5FF, light = 0x2C87FF },
    color_list_pressed_background                         = { dark = 0x345076, light = 0xEAF3FF },
    color_list_pressed_text_option                        = { dark = 0xE8EAED, light = 0x2C87FF },
    color_list_button_primary_background                  = { dark = 0x60A5FF, light = 0x2C87FF },
    color_list_button_primary_text                        = { dark = 0x000000, light = 0xFFFFFF },
    color_list_button_secondary_background                = { dark = 0x17181B, light = 0xDDE4ED },
    color_list_button_secondary_text                      = { dark = 0x000000, light = 0x121C29 },

    color_icon_bar_background                             = { dark = 0x000000, light = 0x1F3047 },
    color_icon_bar_text                                   = { dark = 0xBDC1C6, light = 0xFFFFFF },
    color_icon_bar_separator                              = { dark = 0x2E3134, light = 0x273C59 },
    color_icon_bar_pressed_background                     = { dark = 0x345076, light = 0xCEE3FF },
    color_icon_bar_pressed_text                           = { dark = 0xE8EAED, light = 0x121C29 },

    color_slider_background                               = { dark = 0x1F2023, light = 0xC0C9D5 },
    color_slider_grad_markers                             = { dark = 0x3C4043, light = 0xFFFFFF },
    color_slider_foreground                               = { dark = 0xBDC1C6, light = 0x2C87FF },
    color_slider_current_value_marker                     = { dark = 0x60A5FF, light = 0x2C87FF },
    color_slider_control                                  = { dark = 0xD9D9D9, light = 0xD9D9D9 },
    color_slider_value_text                               = { dark = 0x9AA0A6, light = 0x8FA0B7 },
    color_slider_limits_text                              = { dark = 0x5F6368, light = 0x8FA0B7 },

    color_toggle_background                               = { dark = 0x000000, light = 0xD5DDE4 },
    color_toggle_inactive_text                            = { dark = 0xBDC1C6, light = 0x223A59 },
    color_toggle_active_background                        = { dark = 0x60A5FF, light = 0x2C87FF },
    color_toggle_active_text                              = { dark = 0x000000, light = 0xFFFFFF },

    color_switch_inactive_background                      = { dark = 0xBDC1C6, light = 0xDDE4ED },
    color_switch_active_background                        = { dark = 0x60A5FF, light = 0x2C87FF },
    color_switch_control_button                           = { dark = 0xE8EAED, light = 0xBEDAFF },

    color_checkbox_inactive_background                    = { dark = 0x17181B, light = 0xEBEEF1 },
    color_checkbox_inactive_border                        = { dark = 0x2E3134, light = 0x8FA0B7 },
    color_checkbox_active_background                      = { dark = 0x60A5FF, light = 0x2C87FF },
    color_checkbox_active_text                            = { dark = 0x17181B, light = 0xFFFFFF },

    color_spinner_box_inner_border                        = { dark = 0x60A5FF, light = 0x2C87FF },
    color_spinner_box_outer_border                        = { dark = 0x003069, light = 0x6BABFF },

    color_background_tiles                                = { dark = 0x17181B, light = 0xC0C9D5 },
    color_tile_background                                 = { dark = 0x202124, light = 0xFFFFFF },

    color_temperature_border                              = { dark = 0x3C4043, light = 0xC0C9D5 },
    color_temperature_ok                                  = { dark = 0x60A5FF, light = 0x60A5FF },
    color_temperature_warning                             = { dark = 0xFEB547, light = 0xFEB547 },
    color_temperature_error                               = { dark = 0xED4541, light = 0xED4541 },
    color_status_title_text                               = { dark = 0xBDC1C6, light = 0x121C29 },
    color_status_value_text                               = { dark = 0x9AA0A6, light = 0x345076 },
    color_status_info_text                                = { dark = 0x5F6368, light = 0x8FA0B7 },
    color_status_info_warning                             = { dark = 0xFEB547, light = 0xFEB547 },
    color_status_tile_separator                           = { dark = 0x272A2C, light = 0xEBEEF1 },
    color_status_icon_text                                = { dark = 0xBDC1C6, light = 0x345076 },
    color_status_ok_green                                 = { dark = 0x54C375, light = 0x54C375 },
    color_status_error_red                                = { dark = 0xED4541, light = 0xED4541 },

    color_hour_glass_background                           = { dark = 0x202124, light = 0xCEE3FF },
    color_hour_glass_text                                 = { dark = 0x3C4043, light = 0x2C87FF },
    color_hour_glass_highlight                            = { dark = 0x60A5FF, light = 0x2C87FF },

    color_progress_background                             = { dark = 0x202124, light = 0xEBEEF1 },
    color_progress_text                                   = { dark = 0xE8EAED, light = 0x2C87FF },
  }
end

---
--- @brief  Applies the theme
--- @param  string theme - either "dark" or "light"
-- TODO: set to default in backend
local current_theme
function apply_theme(theme)
  if theme == current_theme then return end
  current_theme = theme
  local data = {}
  for k,v in pairs(themes()) do
    data[k] = v[theme]
  end
  gre.set_data(data)

  local value = fileinfo:execute('readlink -f '..gre.APP_ROOT..'/images')
  if #value > 0 and value[1]:ends_with("/"..theme) then return end

  if tostring(gre.APP_ROOT):starts_with("/usr/share/") then
    os.execute("ln -snf "..gre.APP_ROOT.."/themes/" .. theme .. " "..get_data_folder().."/images-2")
  else
    os.execute("ln -snf "..gre.APP_ROOT.."/themes/" .. theme .. " "..gre.APP_ROOT.."/images")
  end
  gre.dump_resource("image")
  gre.redraw()
end

---
--- @brief  Updates theme to new value
--- @param  value (optional) - either 0 (light), 1 (dark), or 2 (auto)
function update_theme(value)
  value = value or sbsettings.get_theme()
  if value == 2 then
    value = sbsettings.get_auto_theme()
  end
  apply_theme((value == 1) and "dark" or "light")
end

---
--- @brief  Sets theme to new value
--- @param  value - either 0 (light), 1 (dark), or 2 (auto)
function set_theme(value)
  value = tonumber(value)
  sbsettings.set_theme(value)
  update_theme(value)
end

---
--- @brief  Indicates whether the auto-theme thresholds option should be shown
--- @param  value -
function show_auto_theme_thresholds()
  return sbsettings.get_theme() == 2
end

---
--- @brief  Indicates whether the auto-theme thresholds should be shown
function cb_update_auto_theme_light(mapargs)
  sbsettings.set_auto_theme(0)
  update_theme()
end

---
--- @brief  Indicates whether the auto-theme thresholds should be shown
function cb_update_auto_theme_dark(mapargs)
  sbsettings.set_auto_theme(1)
  update_theme()
end

---
--- @brief  Indicates whether the toggle theme icon should be shown
--- @return true if the toggle theme icon should be shown
function show_toggle_theme()
  return sbsettings.get_show_toggle_theme_icon() == 1
end

---
--- @brief  Toggles the theme
function cb_toggle_theme()
  if sbsettings.get_theme() == 2 then
    sbsettings.set_auto_theme(1 - sbsettings.get_auto_theme())
  else
    sbsettings.set_theme(1 - sbsettings.get_theme())
  end
  update_theme()
end