<?php
// phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment

// Don't load directly
if (!defined('ABSPATH')) { exit; }

if (class_exists('R34ICSPro') && !class_exists('R34ICSProCSS')) {

	class R34ICSProCSS extends R34ICSPro {
		
		public $element_fields = array();
		public $fonts = array();
		
		public $palette_base = array(
				'white' => '#ffffff',
				'whitesmoke' => '#f5f5f5',
				'gainsboro' => '#dcdcdc',
				'darkgray' => '#a9a9a9',
				'gray' => '#808080',
				'dimgray' => '#696969',
				'black' => '#000000',
		);

		
		public function __construct() {

			// These methods involve translation, so must be called on init or later
			add_action('init', function() {
				$this->element_fields = $this->get_element_fields();
				$this->fonts = $this->get_fonts();
			}, 1);

		}
		
		
		public function get_element_groups() {
			$groups = array();
			foreach ((array)@$this->element_fields as $item) {
				if (!in_array($item['group'], $groups)) {
					$groups[] = $item['group'];
				}
				sort($groups);
			}
			return $groups;
		}
		
		
		public function get_element_fields() {
			$this->element_fields = array(
				'--fc-list-event-hover-bg-color' => array(
					'label' => __('List hover background', 'ics-calendar-pro'),
					'group' => __('Full View', 'ics-calendar-pro'),
					'default' => 'rgba(102,102,102,0.3)',
				),
				'--fc-now-indicator-color' => array(
					'label' => __('"Now" indicator', 'ics-calendar-pro'),
					'group' => __('Full View', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--ics-red)',
				),
				'--r34ics--element--basic--date--background' => array(
					'label' => __('Date background', 'ics-calendar-pro'),
					'group' => __('Basic View', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--trans10)',
				),
				'--r34ics--element--basic--date--border-color' => array(
					'label' => __('Date border', 'ics-calendar-pro'),
					'group' => __('Basic View', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--trans40)',
				),
				'--r34ics--element--basic--date--color' => array(
					'label' => __('Date text color', 'ics-calendar-pro'),
					'group' => __('Basic View', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--black)',
				),
				'--r34ics--element--button--background' => array(
					'label' => __('Background', 'ics-calendar-pro'),
					'group' => __('Buttons', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--gray)',
				),
				'--r34ics--element--button--background-hover' => array(
					'label' => __('Hover background', 'ics-calendar-pro'),
					'group' => __('Buttons', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--darkgray)',
				),
				'--r34ics--element--button--border-color' => array(
					'label' => __('Border', 'ics-calendar-pro'),
					'group' => __('Buttons', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--gray)',
				),
				'--r34ics--element--button--border-color-hover' => array(
					'label' => __('Hover border', 'ics-calendar-pro'),
					'group' => __('Buttons', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--darkgray)',
				),
				'--r34ics--element--button--color' => array(
					'label' => __('Text color', 'ics-calendar-pro'),
					'group' => __('Buttons', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--white)',
				),
				'--r34ics--element--button--color-hover' => array(
					'label' => __('Hover text color', 'ics-calendar-pro'),
					'group' => __('Buttons', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--white)',
				),
				'--r34ics--element--descloc-feed-label--border-bottom-color' => array(
					'label' => __('Feed label bottom border', 'ics-calendar-pro'),
					'group' => __('Description/Location', 'ics-calendar-pro'),
					'description' => sprintf(__('Only applies when %1$s is turned on for the calendar.', 'ics-calendar-pro'), '<strong>' . __('Show calendar feed label for each event', 'ics-calendar-pro') . '</strong>'),
					'default' => 'var(--r34ics--color--trans30)',
				),
	
				'--r34ics--element--descloc-feed-label--border-left-color' => array(
					'label' => __('Feed label left border', 'ics-calendar-pro'),
					'group' => __('Description/Location', 'ics-calendar-pro'),
					'description' => sprintf(__('Only applies when %1$s is turned on for the calendar.', 'ics-calendar-pro'), '<strong>' . __('Show calendar feed label for each event', 'ics-calendar-pro') . '</strong>'),
					'default' => 'var(--r34ics--color--trans30)',
				),
				'--r34ics--element--descloc-feed-label-before--background' => array(
					'label' => __('Feed label background', 'ics-calendar-pro'),
					'group' => __('Description/Location', 'ics-calendar-pro'),
					'description' => sprintf(__('Only applies when %1$s is turned on for the calendar.', 'ics-calendar-pro'), '<strong>' . __('Show calendar feed label for each event', 'ics-calendar-pro') . '</strong>'),
					'default' => 'var(--r34ics--color--trans30)',
				),
				'--r34ics--element--events--accent--background' => array(
					'label' => __('Accent background', 'ics-calendar-pro'),
					'group' => __('Events', 'ics-calendar-pro'),
					'description' => __('May be overridden when using color coding on individual calendar feeds.', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--gray)',
				),
				'--r34ics--element--events--accent--border-color' => array(
					'label' => __('Accent border', 'ics-calendar-pro'),
					'group' => __('Events', 'ics-calendar-pro'),
					'description' => __('May be overridden when using color coding on individual calendar feeds.', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--gray)',
				),
				'--r34ics--element--events--accent--color' => array(
					'label' => __('Accent text color', 'ics-calendar-pro'),
					'group' => __('Events', 'ics-calendar-pro'),
					'description' => __('May be overridden when using color coding on individual calendar feeds.', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--white)',
				),
				'--r34ics--element--events--color' => array(
					'label' => __('Primary text color', 'ics-calendar-pro'),
					'group' => __('Events', 'ics-calendar-pro'),
					'description' => __('May be overridden when using color coding on individual calendar feeds.', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--black)',
				),
				'--r34ics--element--events--color--link' => array(
					'label' => __('Link color', 'ics-calendar-pro'),
					'group' => __('Events', 'ics-calendar-pro'),
					'description' => __('May be overridden when using color coding on individual calendar feeds.', 'ics-calendar-pro'),
					'default' => 'inherit',
				),
				'--r34ics--element--events--color--link-hover' => array(
					'label' => __('Link hover color', 'ics-calendar-pro'),
					'group' => __('Events', 'ics-calendar-pro'),
					'description' => __('May be overridden when using color coding on individual calendar feeds.', 'ics-calendar-pro'),
					'default' => 'inherit',
				),
				'--r34ics--element--hover-block--background' => array(
					'label' => __('Background', 'ics-calendar-pro'),
					'group' => __('Hover Block', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--white)',
				),
				'--r34ics--element--hover-block--border-color' => array(
					'label' => __('Border', 'ics-calendar-pro'),
					'group' => __('Hover Block', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--darkgray)',
				),
				'--r34ics--element--lightbox--background' => array(
					'label' => __('Background', 'ics-calendar-pro'),
					'group' => __('Lightbox', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--trans50)',
				),
				'--r34ics--element--lightbox-close--background' => array(
					'label' => __('Close button background', 'ics-calendar-pro'),
					'group' => __('Lightbox', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--black)',
				),
				'--r34ics--element--lightbox-close--color' => array(
					'label' => __('Close button X color', 'ics-calendar-pro'),
					'group' => __('Lightbox', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--white)',
				),
				'--r34ics--element--lightbox-content--background' => array(
					'label' => __('Content background', 'ics-calendar-pro'),
					'group' => __('Lightbox', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--white)',
				),
				'--r34ics--element--lightbox-content--color' => array(
					'label' => __('Content color', 'ics-calendar-pro'),
					'group' => __('Lightbox', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--black)',
				),
				'--r34ics--element--month--background' => array(
					'label' => __('Background', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--white)',
				),
				'--r34ics--element--month--day--background' => array(
					'label' => __('Day header background', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--whitesmoke)',
				),
				'--r34ics--element--month--day--color' => array(
					'label' => __('Day header text color', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--black)',
				),
				'--r34ics--element--month--events-li--border-bottom-color' => array(
					'label' => __('Event list item bottom border', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--gainsboro)',
				),
				'--r34ics--element--month-label--color' => array(
					'label' => __('Month label text color', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'default' => 'inherit',
				),
				'--r34ics--element--month--off--background' => array(
					'label' => __('"Off" day background', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'description' => __('"Off" is used for dates outside of the current month or range.', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--whitesmoke)',
				),
				'--r34ics--element--month--off--color' => array(
					'label' => __('"Off day text color', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'description' => __('"Off" is used for dates outside of the current month or range.', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--gray)',
				),
				'--r34ics--element--month--th--background' => array(
					'label' => __('Column header background', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--gainsboro)',
				),
				'--r34ics--element--month--th--color' => array(
					'label' => __('Column header text color', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--black)',
				),
				'--r34ics--element--month--th-td--border-color' => array(
					'label' => __('Border', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--gainsboro)',
				),
				'--r34ics--element--month--today-day--background' => array(
					'label' => __('"Today" background', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--gray)',
				),
				'--r34ics--element--month--today-day--color' => array(
					'label' => __('"Today" text color', 'ics-calendar-pro'),
					'group' => __('Month/Table Views', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--white)',
				),
				'--r34ics--element--print--background' => array(
					'label' => __('Background', 'ics-calendar-pro'),
					'group' => __('Print', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--white)',
				),
				'--r34ics--element--print--border-color' => array(
					'label' => __('Border', 'ics-calendar-pro'),
					'group' => __('Print', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--gainsboro)',
				),
				'--r34ics--element--print--color' => array(
					'label' => __('Text color', 'ics-calendar-pro'),
					'group' => __('Print', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--black)',
				),
				'--r34ics--element--select--background' => array(
					'label' => __('Background', 'ics-calendar-pro'),
					'group' => __('Dropdown Menus', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--whitesmoke)',
				),
				'--r34ics--element--select--color' => array(
					'label' => __('Text color', 'ics-calendar-pro'),
					'group' => __('Dropdown Menus', 'ics-calendar-pro'),
					'default' => 'var(--r34ics--color--black)',
				),
			);
			
			// Assign current values
			$current_css_values = get_theme_mod('r34icspro_css_var');
			if (is_array($current_css_values)) {
				foreach ((array)$this->element_fields as $key => $item) {
					// Fallback is blank string so palette values will cascade down!
					$this->element_fields[$key]['value'] = $current_css_values[$key] ?? '';
				}
			}
			
			// Allow external filtering of values
			$this->element_fields = apply_filters('r34icspro_css_element_fields', $this->element_fields);
			
			return $this->element_fields;
		}
		
		
		public function get_fonts() {
			$this->fonts = array(
				'default' => array(
					'folder' => false,
					'family' => false,
					'label' => '[' . __('Theme fonts', 'ics-calendar-pro') . ']',
				),
				'system' => array(
					'folder' => false,
					'family' => '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif',
					'label' => '[' . __('System font', 'ics-calendar-pro') . ']',
				),
				'serif' => array(
					'folder' => false,
					'family' => 'serif',
				),
				'sans-serif' => array(
					'folder' => false,
					'family' => 'sans-serif',
				),
				'monospace' => array(
					'folder' => false,
					'family' => 'monospace',
				),
				'Asap Condensed' => array(
					'folder' => 'asap-condensed',
				),
				'Barlow' => array(
					'folder' => 'barlow',
				),
				'Barlow Condensed' => array(
					'folder' => 'barlow-condensed',
				),
				'BenchNine' => array(
					'folder' => 'benchnine',
				),
				'Figtree' => array(
					'folder' => 'figtree',
				),
				'IBM Plex Sans Condensed' => array(
					'folder' => 'ibm-plex-sans-condensed',
				),
				'Lato' => array(
					'folder' => 'lato',
				),
				'Libre Franklin' => array(
					'folder' => 'libre franklin',
				),
				'Mulish' => array(
					'folder' => 'mulish',
				),
				'Noto Sans' => array(
					'folder' => 'noto-sans',
				),
				'Noto Serif' => array(
					'folder' => 'noto-serif',
				),
				'Open Sans' => array(
					'folder' => 'open-sans',
				),
				'Poppins' => array(
					'folder' => 'poppins',
				),
				'Pragati Narrow' => array(
					'folder' => 'pragati-narrow',
				),
				'Roboto' => array(
					'folder' => 'roboto',
				),
				'Roboto Condensed' => array(
					'folder' => 'roboto-condensed',
				),
				'Rubik' => array(
					'folder' => 'rubik',
				),
				'Sofia Sans Extra Condensed' => array(
					'folder' => 'sofia-sans-extra-condensed',
				),
				'Source Code Pro' => array(
					'folder' => 'source-code-pro',
				),
				'Source Sans Pro' => array(
					'folder' => 'source-sans-pro',
				),
				'Source Serif Pro' => array(
					'folder' => 'source-serif-pro',
				),
			);

			// Allow external filtering of values
			$this->fonts = apply_filters('r34icspro_css_fonts', $this->fonts);
			
			return $this->fonts;
		}
		
		
		public function get_palette() {
		
			// Set base palette name
			$palette['base'] = get_theme_mod('r34icspro_color_palette', 'custom');
			
			// Set palette colors
			$css_vars = get_theme_mod('r34icspro_css_var');
			$palette['colors'] = array();
			$prefix = '--r34ics--color--';
			foreach (array_keys((array)$this->palette_base) as $key) {
				$palette['colors'][$key] = $css_vars[$prefix . $key] ?? $this->palette_base[$key];
			}
		
			// Allow external filtering of values
			$palette = apply_filters('r34icspro_css_palette_vars', $palette);
			
			return $palette;
		}
		
		
		public function get_css() {
			$css = '';
			
			// TYPOGRAPHY
			
			// Font family
			// Note: This MUST come first so @import works
			if ($font = get_theme_mod('r34icspro_font')) {
				if (in_array($font, array('default', ''))) {
					// Do nothing
				}
				elseif (in_array($font, array('serif', 'sans-serif', 'monospace'))) {
					// Yes, this scope is different (.r34ics_lightbox_content vs. .r34ics_lightbox)
					$css .= '.ics-calendar *:not(.fc-icon), .r34ics_lightbox_content { font-family: ' . esc_attr($font) . '; }';
				}
				else {
					$font_folder = str_replace(' ', '-', strtolower($font));
					$css .= "\n" . '@import url("' . plugin_dir_url(dirname(__FILE__)) . 'assets/fonts/' . $font_folder . '/style.css");' . "\n";
					// Yes, this scope is different (.r34ics_lightbox_content vs. .r34ics_lightbox)
					$css .= '.ics-calendar *:not(.fc-icon), .r34ics_lightbox_content { font-family: "' . esc_attr($font) . '", sans-serif; }';
				}
			}
			
			// Full view text scaling
			if ($text_scaling_full = get_theme_mod('r34icspro_text_scaling_full')) {
				$css .= '.ics-calendar .fc { --r34ics--fc--font--size: ' . wp_kses($text_scaling_full, 'data') . '; }';
			}
			
			// Month/table view text scaling
			if ($text_scaling_table = get_theme_mod('r34icspro_text_scaling_table')) {
				$css .= '.ics-calendar-month-grid, .ics-calendar-widget-grid { font-size: ' . wp_kses($text_scaling_table, 'data') . '; }';
			}
			
			// List/basic view text scaling
			if ($text_scaling_list = get_theme_mod('r34icspro_text_scaling_list')) {
				$css .= '.ics-calendar-list-wrapper, .ics-calendar-basic-wrapper, .ics-calendar-sidebar, .ics-calendar-day-details { font-size: ' . wp_kses($text_scaling_list, 'data') . '; }';
			}
			
			// Hyphenation
			if ($hyphenation_off = get_theme_mod('r34icspro_hyphenation_off')) {
				switch ($hyphenation_off) {
					case 1:
						$css .= '.ics-calendar .event *, .ics-calendar .fc td * { word-break: normal; hyphens: none; }';
						break;
					case 2:
						$css .= '.ics-calendar .event *, .ics-calendar .fc td * { word-break: normal; hyphens: auto; }';
						break;
					case 0:
					default:
						$css .= '.ics-calendar .event *, .ics-calendar .fc td * { word-break: break-word; hyphens: auto; }';
						break;
				}
			}
			
			// COLORS
			
			// CSS variables
			if ($css_vars = get_theme_mod('r34icspro_css_var')) {
				$css .= '.ics-calendar, .ics-calendar .fc, .ics-calendar.darkmode, .r34ics_lightbox {';
				
				foreach ((array)$css_vars as $key => $value) {
					if (!empty($value)) {
						$css .= wp_kses($key, 'data') . ':' . wp_kses($value, 'data') . ';';
					}
				}
				
				$css .= '}';
			}
			
			// LAYOUT
			
			// Hide weekends
			if (get_theme_mod('r34icspro_hide_weekends')) {
				$css .= '.ics-calendar tr > *[data-dow="0"], .ics-calendar tr > *[data-dow="6"] { display: none !important; }';
			}
			
			// Legend style
			if ($legend_style = get_theme_mod('r34icspro_legend_style')) {
				switch ($legend_style) {
					case 1:
						$css .= '.ics-calendar-color-key { display: block; } .ics-calendar-color-key input[type=checkbox] { display: none; }';
						break;
					case 2:
						$css .= '.ics-calendar-color-key { display: none; } .ics-calendar-color-key input[type=checkbox] { display: none; }';
						break;
					case 0:
					default:
						$css .= '.ics-calendar-color-key { display: block; } .ics-calendar-color-key input[type=checkbox] { display: inline; }';
						break;
				}
			}
			
			// Sticky sidebar top
			if ($sticky_sidebar_top = get_theme_mod('r34icspro_sticky_sidebar_top')) {
				$css .= '.ics-calendar-sidebar { top:' . wp_kses($sticky_sidebar_top, 'data') . '; }';
			}
						
			return $css;
		}

	}

}