###
### Color theme preparation.
###
### This template is responsible with reading the current color theme and preparing the $colorTheme variable.
### The current color theme can be configured in the user profile for each user, at the space level, or globally, at the
### wiki level.
###
###
## Initialization of the theme with the defaults
## All keys must match the ColorThemeClass properties
#set ($theme = {'pageBackgroundColor': '#ECECEC',
'pageHeaderBackgroundColor'        : '#FFFFFF',
'pageContentBackgroundColor'       : '#FFFFFF',
'textColor'                        : '#4D4D4D',
'textPrimaryColor'                 : '#656565',
'textSecondaryColor'               : '#656565',
'titleColor'                       : '#4D5860',
'borderColor'                      : '#E8E8E8',
'linkColor'                        : '#528BA1',
'highlightColor'                   : '#FFFFDD',
'fieldGradientColor'               : '#F5F5F5',
'menuBackgroundColor'              : '#ECECEC',
'menuGradientColor'                : '#FFFFFF',
'menuLinkColor'                    : '#4D5860',
'menuContentBackgroundColor'       : '#ECECEC',
'menuContentGradientColor'         : '#FFFFFF',
'menuContentLinkColor'             : '#4D5860',
'menuSelectedEntryBackgroundColor' : '#FFFFFF',
'menuSelectedEntryLinkColor'       : '#4D5860',
'menuAddEntryBackgroundColor'      : '#4D9244',
'menuAddEntryLinkColor'            : '#FFFFFF',
'submenuContentBackgroundColor'    : '#ECECEC',
'panelBackgroundColor'             : '#FFFFFF',
'panelHeaderBackgroundColor'       : '#ECECEC',
'panelHeaderGradientColor'         : '#FFFFFF',
'panelHeaderTextColor'             : '#4D5860',
'panelTextColor'                   : '#4D4D4D',
'panelCollapsedBackgroundColor'    : '#ECECEC',
'panelCollapsedGradientColor'      : '#FFFFFF',
'panelCollapsedTextColor'          : '#4D5860',
'buttonPrimaryBackgroundColor'     : '#528BA1',
'buttonPrimaryGradientColor'       : '#528BA1',
'buttonPrimaryTextColor'           : '#FFFFFF',
'buttonSecondaryBackgroundColor'   : '#ECECEC',
'buttonSecondaryGradientColor'     : '#FFFFFF',
'buttonSecondaryTextColor'         : '#4D5860',
'backgroundSecondaryColor'         : '#F3F3F3',
'notificationInfoColor'            : '#336699',
'notificationSuccessColor'         : '#008000',
'notificationWarningColor'         : '#D09000',
'notificationErrorColor'           : '#CC3333'
})
##
## Overwrite with the values set in the custom theme
#if (!$themeObj)
## First, try to take the theme from the request URL
    #set ($themeDocFullName = "$!{request.colorTheme}")
## Second, try to take the theme from the preferences (user, space, wiki)
    #if ($themeDocFullName == '')
        #set ($themeDocFullName = "$!xwiki.getUserPreference('colorTheme')")
    #end
## Last, just use the default theme
    #if ($themeDocFullName == '')
        #set ($themeDocFullName = 'ColorThemes.DefaultColorTheme')
    #end
    #set ($themeDoc = $xwiki.getDocument($themeDocFullName))
    #set ($themeObj = $themeDoc.getObject('ColorThemes.ColorThemeClass'))
#end
#if ("$!themeObj" != '')
    #foreach ($prop in $themeObj.properties)
    ## Simple validation of the color values.
        #if (!$prop.name.endsWith('Color') || $prop.value.matches('transparent|#[0-9a-fA-F]{3}|#[0-9a-fA-F]{6}'))
            #set ($discard = $theme.put($prop.name, $prop.value))
        #end
        #if ($prop.name.endsWith('Image'))
            #if ("$!prop.value" != '')
                #set ($discard = $theme.put($prop.name, "url(${themeDoc.getAttachmentURL($prop.value)})"))
            #else
                #set ($discard = $theme.put($prop.name, ''))
            #end
        #end
    #end
    #set ($gradientColorsToRemove = [])
    #foreach ($prop in $theme.entrySet())
        #if ($prop.key.endsWith('GradientColor') && "$!{themeObj.getProperty($prop.key).value}" == '')
            #set ($discard = $gradientColorsToRemove.add($prop.key))
        #end
    #end
    #foreach ($prop in $gradientColorsToRemove)
        #set ($discard = $theme.put($prop, ''))
    #end
## For old themes that don't have separate values for #mainmenu and #contentmenu overwrite #contentmenu values with the ones from the #mainmenu
    #if ("$!{themeObj.getProperty('menuContentLinkColor').value}" == '')
        #set ($discard = $theme.put('menuContentLinkColor', "$!{themeObj.getProperty('menuLinkColor').value}"))
    #end
    #if ("$!{themeObj.getProperty('menuContentBackgroundColor').value}" == '')
        #set ($discard = $theme.put('menuContentBackgroundColor', "$!{themeObj.getProperty('menuBackgroundColor').value}"))
    #end
    #if ("$!{themeObj.getProperty('submenuContentBackgroundColor').value}" == '')
        #set ($discard = $theme.put('submenuContentBackgroundColor', "$!{themeObj.getProperty('menuBackgroundColor').value}"))
    #end
#end

#if(!$doNotLoadColorThemeFromCSS)
    #set($theme2 = $services.lesscss.getColorThemeFromSkinFile("style.less.vm"))
    #foreach($key in $theme2.keySet())
        #set($discard = $theme.put($key, $theme2.get($key)))
    #end
#end
