<?php
/**
 * Inline Style Class.
 *
 * Generate CSS variables from Customizer theme options (colors, etc.)
 * and load them both in frontend and block editor.
 *
 * @package    Wira
 * @since      1.0.0
 */

namespace Wira;

if ( ! class_exists( 'Inline_Style' ) ) {

	/**
	 * Inline_Style Class.
	 *
	 * Handles generation of CSS custom properties (variables) from
	 * theme options and outputs them inline on frontend and block editor.
	 *
	 * @since 1.0.0
	 */
	class Inline_Style {

		/**
		 * Theme config instance.
		 *
		 * @since 1.0.0
		 * @var object
		 */
		protected $theme_config;

		/**
		 * Theme slug.
		 *
		 * @since 1.0.0
		 * @var string
		 */
		protected $theme_slug;

		/**
		 * Default color values (without labels).
		 *
		 * @since 1.0.0
		 * @var array<string,string>
		 */
		protected $color_defaults = array(
			'wira_color_primary'         => '#F98400',
			'wira_color_secondary'       => '#1090D2',
			'wira_color_secondary20'     => '#1090D233',
			'wira_color_text'            => '#6e6e6e',
			'wira_color_accent'          => '#00050a',
			'wira_color_placeholder'     => '#A0A0A0',
			'wira_color_tertiary'        => '#004da3',
			'wira_color_secondaryaccent' => '#53b1ff',
			'wira_color_primaryaccent'   => '#ffcfb2',
			'wira_color_primary20'       => '#F9840033',
			'wira_color_border'          => '#dee2e6',
			'wira_color_borderoverlay'   => '#ffffff38',
			'wira_color_whitesmoke'      => '#f4fbfe',
			'wira_color_white'           => '#ffffff',
			'wira_color_whiteoverlay'    => '#ffffffc2',
			'wira_color_white10'         => '#ffffff1a',
			'wira_color_tertiarydark'    => '#001D3D',
			'wira_color_tertiarydark90'  => '#001D3De6',
			'wira_color_tertiarydark60'  => '#001D3D99',
			'wira_color_tertiarydark20'  => '#001D3D33',
			'wira_color_transparent'     => '#ffffff00',
		);

		/**
		 * Constructor.
		 *
		 * Initializes theme config and registers hooks.
		 *
		 * @since 1.0.0
		 */
		public function __construct() {
			$this->theme_config = \ThemeConfig\Main_Theme_Config::get_instance();
			$this->theme_slug   = $this->theme_config->get_config( 'slug' );

			$this->load_hooks();
		}

		/**
		 * Register hooks to enqueue frontend and editor styles.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		private function load_hooks() {
			add_action( 'wp_enqueue_scripts', array( $this, 'frontend_styles' ), 20 );
			add_action( 'enqueue_block_editor_assets', array( $this, 'editor_styles' ) );
		}

		/**
		 * Generate CSS variables for :root selector.
		 *
		 * @since 1.0.0
		 *
		 * @param bool $is_editor Whether running in block editor.
		 * @return string CSS output with custom properties.
		 */
		private function generate_root_css( $is_editor = false ) {
			$selector = $is_editor ? ':root, body .editor-styles-wrapper' : ':root';

			$css = $selector . " {\n";

			foreach ( $this->color_defaults as $id => $default_color ) {
				$val           = get_theme_mod( $id, $default_color );
				$clean_id      = preg_replace( '/^wira_/', '', $id );
				$css_color_var = str_replace( '_', '-', $clean_id );

				$css .= "  --theme-{$css_color_var}: {$val};\n";
			}

			$css .= "}\n";

			return $css;
		}

		/**
		 * Enqueue frontend inline CSS variables.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		public function frontend_styles() {
			wp_enqueue_style(
				'wira-inline-style',
				get_template_directory_uri() . '/assets/css/inline-style.css'
			);

			wp_add_inline_style(
				'wira-inline-style',
				$this->generate_root_css()
			);
		}

		/**
		 * Enqueue block editor inline CSS variables.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		public function editor_styles() {
			wp_register_style( 'wira-editor-style', false );
			wp_enqueue_style( 'wira-editor-style' );

			wp_add_inline_style(
				'wira-editor-style',
				$this->generate_root_css( true )
			);
		}
	}
}
