<?php
/**
 * Customizer class
 *
 * @package wpstream-theme
 */

/**
 * Customizer class for managing theme customizations.
 */
class Wpstream_Customizer {
	/**
	 * Icons array.
	 *
	 * @var array
	 */
	protected $icons;
	/**
	 * Customizer groups array.
	 *
	 * @var array
	 */
	protected $customizer_groups;
	/**
	 * Path to the directory containing customizer icons.
	 *
	 * @var string
	 */
	public $icons_path;

	/**
	 * Construct
	 */
	public function __construct() {
		$this->icons_path = get_template_directory() . '/img/customizer/';
		$this->set_icons();
		$this->set_customizer_groups();

		add_action( 'customize_controls_enqueue_scripts', array( $this, 'wpstream_customize_enqueue_scripts' ) );
	}

	/**
	 * Set icons
	 */
	private function set_icons() {
		$this->icons = array(
			// general_settings.
			'title_tagline'                          => $this->get_svg_icon( 'site-identity.svg' ),
			'wpstream_logos'                         => $this->get_svg_icon( 'default.svg' ),
			'nav_menus'                              => $this->get_svg_icon( 'menus.svg' ),
			'widgets'                                => $this->get_svg_icon( 'widgets.svg' ),
			'static_front_page'                      => $this->get_svg_icon( 'homepage.svg' ),

			// streaming_controls.
			'wpstream_streaming'                     => $this->get_svg_icon( 'streaming-settings.svg' ),

			// template_parts.
			'wpstream_theme_header_panel'            => $this->get_svg_icon( 'header.svg' ),
			'wpstream_sidebar_settings'              => $this->get_svg_icon( 'sidebar.svg' ),
			'wpstream_theme_footer_panel'            => $this->get_svg_icon( 'footer.svg' ),

			// theme_styles.
			'wpstream_layout'                        => $this->get_svg_icon( 'appearance.svg' ),
			'wpstream_appearance'                    => $this->get_svg_icon( 'appearance.svg' ),
			'wpstream_general_design'                => $this->get_svg_icon( 'colors.svg' ),
			'wpstream_typography'                    => $this->get_svg_icon( 'typo.svg' ),

			// specific_pages.
			'wpstream_blog_post_settings'            => $this->get_svg_icon( 'blog.svg' ),
			'wpstream_blog_post_card_settings'  => $this->get_svg_icon( 'blog-card.svg' ),
			'wpstream_video_on_demand_settings'      => $this->get_svg_icon( 'vod.svg' ),
			'wpstream_free_to_view_live_settings'    => $this->get_svg_icon( 'live.svg' ),
			'wpstream_product_details_page_settings' => $this->get_svg_icon( 'products.svg' ),
			'wpstream_unit_card_settings'            => $this->get_svg_icon( 'videocard.svg' ),
			'wpstream_page_settings'            	 => $this->get_svg_icon( 'page.svg' ),
			'wpstream_collection_free_to_view_settings' => $this->get_svg_icon( 'collection.svg' ),
			'wpstream_archive_page_settings'            => $this->get_svg_icon( 'archive.svg' ),
		);
	}



	/**
	 * Groups customizer
	 */
	private function set_customizer_groups() {
		$this->customizer_groups = array(
			'general_settings'   => array(
				'title_tagline',
				'wpstream_logos',
				'nav_menus',
				'widgets',
				'static_front_page',
			),
			'streaming_controls' => array(
				'wpstream_streaming',
			),
			'template_parts'     => array(
				'wpstream_theme_header_panel',
				'wpstream_sidebar_settings',
				'wpstream_theme_footer_panel',
			),
			'theme_styles'       => array(
				'wpstream_layout',
				'wpstream_appearance',
				'wpstream_general_design',
				'wpstream_typography',
			),
			'specific_pages'     => array(
				'wpstream_page_settings',
				'wpstream_blog_post_settings',
				'wpstream_video_on_demand_settings',
				'wpstream_free_to_view_live_settings',
				'wpstream_product_details_page_settings',
				'wpstream_collection_free_to_view_settings',
				'wpstream_archive_page_settings',
				'wpstream_unit_card_settings',
			),
		);
	}

	/**
	 * Customizer enqueue scripts
	 */
	public function wpstream_customize_enqueue_scripts() {
		wp_enqueue_style( 'wpstream-customizer', get_template_directory_uri() . '/css/customizer.css', '', '1.0' );
		wp_enqueue_script( 'wpstream-customizer', get_template_directory_uri() . '/js/customizer.js', array( 'jquery' ), '1.0', true );

		wp_localize_script(
			'wpstream-customizer',
			'wpstream_customizer_data',
			array(
				'customizer_icons'  => apply_filters( 'wpstream_customizer_icons', array_filter( $this->icons ) ),
				'customizer_groups' => apply_filters( 'wpstream_customizer_groups', $this->customizer_groups ),
			)
		);
	}

	/**
	 * Retrieve the SVG icon content based on its name.
	 *
	 * @param string $name The name of the SVG icon file.
	 * @return string The content of the SVG icon file.
	 */
	protected function get_svg_icon( $name ) {
	
		global $wp_filesystem;

			// Initialize the WP_Filesystem if not already done.
			if ( empty( $wp_filesystem ) ) {
				require_once( ABSPATH . 'wp-admin/includes/file.php' );
				WP_Filesystem();
			}

			$path = $this->icons_path . $name;

			if ( $wp_filesystem->exists( $path ) ) {
				return $wp_filesystem->get_contents( $path );
			}

			return '';
	}
}
( new Wpstream_Customizer() );
