<?php

/**
 * Dynamic Internal Styles
 * @package Glaciers
 * @since 1.0.0
 */

if (!defined('ABSPATH')) {
  exit; // Exit if accessed directly.
}

/* RESET CUSTOM STYLES CACHE
 @since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_reset_inline_style_cache')) :
  function glaciers_reset_inline_style_cache()
  {
    delete_transient('glaciers_inline_style');
  }
endif;
add_action('customize_save_after', 'glaciers_reset_inline_style_cache');


/* INTERNAL STYLE
 @since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_internal_styles')) :
  function glaciers_internal_styles()
  {
    if (is_customize_preview())
      return glaciers_get_inline_style();

    $glaciers_internal_styles = get_transient('glaciers_inline_style');

    if ($glaciers_internal_styles === false) {
      $glaciers_internal_styles = glaciers_get_inline_style();
      set_transient('glaciers_inline_style', esc_attr($glaciers_internal_styles));
    }

    return wp_kses_post($glaciers_internal_styles);
  }
endif;


/* PRESET COLOUR STYLES
 @since 1.0.0
   ====================================================   */
if (!function_exists('glaciers_preset_css')) :
  function glaciers_preset_css()
  {

    $glaciers_presets = get_theme_mod('glaciers_presets');
    if ($glaciers_presets && $glaciers_presets !== 'preset1') {
      echo '<style type="text/css" media="all">
		:root {', glaciers_colour_presets(), '	}
		</style>';
    }
  }
endif;
add_action('wp_head', 'glaciers_preset_css', 12);


/* CUSTOMIZER INTERNAL STYLES
 @since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_get_inline_style')) :
  function glaciers_get_inline_style()
  {
    $css = '';

    // Site title colour
    $glaciers_site_title_colour = get_theme_mod('glaciers_site_title_colour');

    if ($glaciers_site_title_colour && $glaciers_site_title_colour !== '#000000') {
      $css .= '
        .site-title a,
		.site-title a:visited {
          color: ' . esc_attr($glaciers_site_title_colour) . ';
        }';
    }

    // Site tagline colour
    $glaciers_tagline_colour = get_theme_mod('glaciers_tagline_colour');

    if ($glaciers_tagline_colour && $glaciers_tagline_colour !== '#6c757d') {
      $css .= '
        .site-description {
          color: ' . esc_attr($glaciers_tagline_colour) . ';
        }';
    }

    // Logo width
    $glaciers_logo_width = get_theme_mod('glaciers_logo_width');

    if ($glaciers_logo_width && has_custom_logo() && $glaciers_logo_width !== '250') {
      $css .= '
        .custom-logo {
          max-width:' . floatval(esc_attr($glaciers_logo_width)) . 'px;
        }';
    }

    // Site title font size
    $site_title_font_size = get_theme_mod('site_title_font_size');

    if ($site_title_font_size && $site_title_font_size !== '1.75') {
      $css .= '
        .navbar-brand {
          font-size: ' . floatval(esc_attr($site_title_font_size)) . 'rem;
        }';
    }

    // Site tagline font size
    $site_tagline_font_size = get_theme_mod('site_tagline_font_size');

    if ($site_tagline_font_size && $site_tagline_font_size !== '0.75') {
      $css .= '
        .site-description {
          font-size: ' . floatval(esc_attr($site_tagline_font_size)) . 'rem;
        }';
    }

    // Widget Title border
    $glaciers_hide_widget_border = get_theme_mod('glaciers_hide_widget_border');

    if ($glaciers_hide_widget_border && $glaciers_hide_widget_border !== false) {
      $css .= '
      .blog-sidebar .widget-title::after,
      .left-sidebar .widget-title::after,
      .right-sidebar .widget-title::after,
      .bottom-sidebar .widget-title::after {
        content: none;
      }
      .widget-title {
        margin-bottom: 0;
      ';
    }

    // Primary colour
    $glaciers_custom_primary_colour = get_theme_mod('glaciers_custom_primary_colour');

    if ($glaciers_custom_primary_colour && $glaciers_custom_primary_colour !== '#35a0a7') {
      $css .= '
	  :root {
		  --glaciers-primary: ' . esc_attr($glaciers_custom_primary_colour) . ' !important;
        }';
    }

    // Secondary colour
    $glaciers_custom_secondary_colour = get_theme_mod('glaciers_custom_secondary_colour');

    if ($glaciers_custom_secondary_colour && $glaciers_custom_secondary_colour !== '#a66335') {
      $css .= '
	  :root {
		  --glaciers-secondary:' . esc_attr($glaciers_custom_secondary_colour) . ' !important;
        }';
    }

    // Tertiary colour
    $glaciers_custom_tertiary_colour = get_theme_mod('glaciers_custom_tertiary_colour');

    if ($glaciers_custom_tertiary_colour && $glaciers_custom_tertiary_colour !== '#592b0b') {
      $css .= '
	  :root {
		  --glaciers-tertiary:' . esc_attr($glaciers_custom_tertiary_colour) . ' !important;
        }';
    }

    // Content area colors
    $glaciers_content_bg_colour = get_theme_mod('glaciers_content_bg_colour');

    if ($glaciers_content_bg_colour && $glaciers_content_bg_colour !== '#e9e9e9') {
      $css .= '
        :root {
         --glaciers-content-bg: ' . esc_attr($glaciers_content_bg_colour) . ';
        }';
    }

    // Body text colour
    $glaciers_content_area_text_colour = get_theme_mod('glaciers_content_area_text_colour');

    if ($glaciers_content_area_text_colour && $glaciers_content_area_text_colour !== '#646464') {
      $css .= '
        body {
          color: ' . esc_attr($glaciers_content_area_text_colour) . ';
        }';
    }

    // Category prefix colour
    $glaciers_category_prefix = get_theme_mod('glaciers_category_prefix');

    if ($glaciers_category_prefix && $glaciers_category_prefix !== '#c6975e') {
      $css .= '
        .archive-prefix.colour {
          color: ' . esc_attr($glaciers_category_prefix) . ';
        }';
    }

    // Secondary text colour
    $glaciers_secondary_text_colour = get_theme_mod('glaciers_secondary_text_colour');

    if ($glaciers_secondary_text_colour && $glaciers_secondary_text_colour !== '#7c7c7c') {
      $css .= '
        .tags-list a, 
        .tagcloud a, 
        .post-cats a, 
        .post-meta, 
        .post-date, 
        .comment-meta,
        .post-navigation a > .nav-meta, 
        .wp-caption-text, 
        .entry-caption,
        .entry-meta,
        .entry-meta a,
        .entry-meta a:visited,
        .form-text {
          color: ' . esc_attr($glaciers_secondary_text_colour) . ';
        }';
    }

    // Primary menu link colour
    $glaciers_nav_link_colour = get_theme_mod('glaciers_nav_link_colour');

    if ($glaciers_nav_link_colour && $glaciers_nav_link_colour !== '#495057') {
      $css .= '
	    :root {
		    --glaciers-navbar-link:' . esc_attr($glaciers_nav_link_colour) . ';
      }';
    }

    // Mobile menu  line separators
    $glaciers_mobile_line_separators = get_theme_mod('glaciers_mobile_line_separators');

    if ($glaciers_mobile_line_separators && $glaciers_mobile_line_separators !== '#ededed') {
      $css .= '
        @media (max-width: 991px) {
        #mainmenu li,
        #mainmenu .sub-menu {
          border-color: ' . hex2rgba(esc_attr($glaciers_mobile_line_separators), 0.08, true) . '
        }
						
        #mainmenu a>span::before {
          background-color: ' . hex2rgba(esc_attr($glaciers_mobile_line_separators), 0.15, true) . '
        }
			
		}';
    }

    // Headings colour
    $glaciers_headings_colour = get_theme_mod('glaciers_headings_colour');

    if ($glaciers_headings_colour && $glaciers_headings_colour !== '#292929') {
      $css .= '
        h2.entry-title, .entry-title a, .page-title, h1, h2, h3, h4, h5, h6 {
          color: ' . esc_attr($glaciers_headings_colour) . ';
        }
        h1 a:focus,  h2 a:focus,  h3 a:focus,  h4 a:focus,  h5 a:focus,  h6 a:focus, h1 a:hover,  h2 a:hover,  h3 a:hover,  h4 a:hover,  h5 a:hover, h6 a:hover {
          color: ' . hex2rgba(esc_attr($glaciers_headings_colour), 0.7, true) . '
        }';
    }


    // Content links
    $glaciers_content_links = get_theme_mod('glaciers_content_links');

    if ($glaciers_content_links && $glaciers_content_links !== '#35a0a7') {
      $css .= '
      a, a:visited {
            color: ' . esc_attr($glaciers_content_links) . ';
      }';
    }

    // Image caption overlay background
    $glaciers_image_bg_caption = get_theme_mod('glaciers_image_bg_caption');

    if ($glaciers_image_bg_caption && $glaciers_image_bg_caption !== '#ffffff') {
      $css .= '
        .wp-caption-text, .wp-element-caption {
          background-color: ' . hex2rgba(esc_attr($glaciers_image_bg_caption), 0.35, true) . ' !important;
        }';
    }

    // Image caption overlay text
    $glaciers_caption_text_color = get_theme_mod('glaciers_caption_text_color');

    if ($glaciers_caption_text_color && $glaciers_caption_text_color !== '#292929') {
      $css .= '
        #banner-sidebar .wp-caption-text, .featured-media .wp-caption-text {
          color: ' . esc_attr($glaciers_caption_text_color) . ';
        }';
    }

    // Footer text colour
    $glaciers_footer_text = get_theme_mod('glaciers_footer_text');

    if ($glaciers_footer_text && $glaciers_footer_text !== '#9ba4ad') {
      $css .= '
        .site-footer, 
        .site-footer a, 
        .site-footer a:visited {
         color: ' . hex2rgba(esc_attr($glaciers_footer_text), 0.8, true) . ';
        }';
    }

    // Hide banner captions
    $glaciers_hide_banner_captions = get_theme_mod('glaciers_hide_banner_captions');

    if ($glaciers_hide_banner_captions && $glaciers_hide_banner_captions !== false) {
      $css .= "
        #banner-sidebar .wp-caption-text {
          display: none;
        }";
    }

    // Hide featured image captions
    $glaciers_hide_featured_caption = get_theme_mod('glaciers_hide_featured_caption');

    if ($glaciers_hide_featured_caption && $glaciers_hide_featured_caption !== false) {
      $css .= "
        .featured-media .wp-caption-text {
          display: none;
        }";
    }

    $custom_css = get_theme_mod('glaciers_custom_css');

    if ($custom_css) {
      $css .= wp_filter_nohtml_kses($custom_css);
    }

    return $css; // phpcs:ignore WordPress.Security.EscapeOutput
  }
endif;
