<?php
/**
 * Eronex Customizer configuration.
 *
 * Boots Kirki panels, sections, field files, Pro extensions, and frontend
 * inline CSS generated from Customizer settings.
 *
 * @package Eronex
 * @since   0.0.1
 */

use Kirki\Panel;
use Kirki\Section;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Kirki' ) ) {
	return;
}

if ( ! class_exists( 'Eronex_Customizer_Config' ) ) {

	/**
	 * Registers Eronex Customizer panels, sections, fields, and CSS output.
	 *
	 * Singleton that wires Kirki setup on `after_setup_theme` and prints
	 * Customizer-driven inline styles on `wp_enqueue_scripts`.
	 *
	 * @package Eronex
	 * @since   0.0.1
	 */
	class Eronex_Customizer_Config {

		/**
		 * Singleton instance.
		 *
		 * @since 0.0.1
		 * @var   Eronex_Customizer_Config|null
		 */
		private static $instance;

		/**
		 * Returns the singleton instance.
		 *
		 * @since 0.0.1
		 *
		 * @return Eronex_Customizer_Config Class instance.
		 */
		public static function get_instance() {
			if ( ! isset( self::$instance ) ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * Hooks Customizer init and frontend CSS output.
		 *
		 * @since 0.0.1
		 */
		public function __construct() {
			//Add fields
			add_action( 'after_setup_theme', array( $this, 'init' ) );

			//Frontend Inline CSS
			add_action( 'wp_enqueue_scripts', array( $this, 'frontend_inline_css' ) );
		}

		/**
		 * Loads Kirki extensions, panels/sections, and field definitions.
		 *
		 * @since 0.0.1
		 */
		public function init() {

			//Extensions
			$this->kirki_extensions();

			//Register Panels/Sections
			$this->register_settings();

			//Register Fields
			$this->register_fields();
		}

		/**
		 * Registers Customizer panels and sections.
		 *
		 * Defines Kirki panels (General, Header, Hamburger, Banner, Layout)
		 * and their child sections, including WooCommerce layout sections when
		 * WooCommerce is active. Also defines `KIRKI_NO_OUTPUT` so CSS is
		 * handled by the theme instead of Kirki's default output.
		 *
		 * @since 0.0.1
		 */
		public function register_settings() {

			if ( ! defined( 'KIRKI_NO_OUTPUT' ) ) {
				define( 'KIRKI_NO_OUTPUT', true );
			}

			/**
			 * Register panels.
			 *
			 * @since 0.0.1
			 */
			new Panel(
				'eronex_general',
				array(
					'title'    => esc_html__( 'General', 'eronex' ),
					'priority' => 21,
				)
			);
			new Panel(
				'eronex_header',
				array(
					'title'    => esc_html__( 'Header', 'eronex' ),
					'priority' => 22,
				)
			);
			new Panel(
				'eronex_hamburger',
				array(
					'title'    => esc_html__( 'Hamburger', 'eronex' ),
					'priority' => 23,
				)
			);
			new Panel(
				'eronex_banner',
				array(
					'title'    => esc_html__( 'Banner', 'eronex' ),
					'priority' => 101,
				)
			);
			new Panel(
				'eronex_layout',
				array(
					'title'    => esc_html__( 'Layout', 'eronex' ),
					'priority' => 102,
				)
			);

			/**
			 * Register sections.
			 *
			 * @since 0.0.1
			 */
			new Section(
				'eronex_general_layout',
				array(
					'title' => esc_html__( 'Layout', 'eronex' ),
					'panel' => 'eronex_general',
				)
			);
			new Section(
				'eronex_general_typo',
				array(
					'title' => esc_html__( 'Font', 'eronex' ),
					'panel' => 'eronex_general',
				)
			);
			new Section(
				'eronex_general_color',
				array(
					'title' => esc_html__( 'Color', 'eronex' ),
					'panel' => 'eronex_general',
				)
			);
			new Section(
				'eronex_general_button',
				array(
					'title' => esc_html__( 'Button', 'eronex' ),
					'panel' => 'eronex_general',
				)
			);
			new Section(
				'eronex_general_pagination',
				array(
					'title' => esc_html__( 'Pagination', 'eronex' ),
					'panel' => 'eronex_general',
				)
			);
			new Section(
				'eronex_general_preloader',
				array(
					'title' => esc_html__( 'Preloader', 'eronex' ),
					'panel' => 'eronex_general',
				)
			);
			new Section(
				'eronex_general_scroll_top',
				array(
					'title' => esc_html__( 'Scroll Top', 'eronex' ),
					'panel' => 'eronex_general',
				)
			);

			//Header
			new Section(
				'eronex_header_layout',
				array(
					'title' => esc_html__( 'Layout', 'eronex' ),
					'panel' => 'eronex_header',
				)
			);
			new Section(
				'eronex_header_logo',
				array(
					'title' => esc_html__( 'Logo', 'eronex' ),
					'panel' => 'eronex_header',
				)
			);
			new Section(
				'eronex_header_menu',
				array(
					'title' => esc_html__( 'Menu', 'eronex' ),
					'panel' => 'eronex_header',
				)
			);
			new Section(
				'eronex_header_submenu',
				array(
					'title' => esc_html__( 'Submenu', 'eronex' ),
					'panel' => 'eronex_header',
				)
			);
			new Section(
				'eronex_header_search',
				array(
					'title' => esc_html__( 'Search', 'eronex' ),
					'panel' => 'eronex_header',
				)
			);
			new Section(
				'eronex_header_button',
				array(
					'title' => esc_html__( 'Button', 'eronex' ),
					'panel' => 'eronex_header',
				)
			);

			//Hamburger
			new Section(
				'eronex_hamburger_toggle',
				array(
					'title' => esc_html__( 'Toggle', 'eronex' ),
					'panel' => 'eronex_hamburger',
				)
			);
			new Section(
				'eronex_hamburger_layout',
				array(
					'title' => esc_html__( 'Layout', 'eronex' ),
					'panel' => 'eronex_hamburger',
				)
			);

			new Section(
				'eronex_hamburger_close',
				array(
					'title' => esc_html__( 'Close', 'eronex' ),
					'panel' => 'eronex_hamburger',
				)
			);

			new Section(
				'eronex_hamburger_logo',
				array(
					'title' => esc_html__( 'Logo', 'eronex' ),
					'panel' => 'eronex_hamburger',
				)
			);

			new Section(
				'eronex_hamburger_menu',
				array(
					'title' => esc_html__( 'Menu', 'eronex' ),
					'panel' => 'eronex_hamburger',
				)
			);

			new Section(
				'eronex_hamburger_submenu_icon',
				array(
					'title' => esc_html__( 'Submenu Icon', 'eronex' ),
					'panel' => 'eronex_hamburger',
				)
			);

			new Section(
				'eronex_hamburger_back',
				array(
					'title' => esc_html__( 'Back Menu', 'eronex' ),
					'panel' => 'eronex_hamburger',
				)
			);

			//Banner
			new Section(
				'eronex_banner_layout',
				array(
					'title' => esc_html__( 'Layout', 'eronex' ),
					'panel' => 'eronex_banner',
				)
			);
			new Section(
				'eronex_banner_title',
				array(
					'title' => esc_html__( 'Title', 'eronex' ),
					'panel' => 'eronex_banner',
				)
			);
			new Section(
				'eronex_breadcrumb',
				array(
					'title' => esc_html__( 'Breadcrumb', 'eronex' ),
					'panel' => 'eronex_banner',
				)
			);

			//Layout
			new Section(
				'eronex_layout_archive',
				array(
					'title' => esc_html__( 'Blog / Archive', 'eronex' ),
					'panel' => 'eronex_layout',
				)
			);

			new Section(
				'eronex_layout_page',
				array(
					'title' => esc_html__( 'Single Page', 'eronex' ),
					'panel' => 'eronex_layout',
				)
			);

			new Section(
				'eronex_layout_single',
				array(
					'title' => esc_html__( 'Single Post', 'eronex' ),
					'panel' => 'eronex_layout',
				)
			);

			if ( class_exists( 'WooCommerce' ) ) {
				new Section(
					'eronex_layout_shop_archive',
					array(
						'title' => esc_html__( 'Product Archive', 'eronex' ),
						'panel' => 'eronex_layout',
					)
				);

				new Section(
					'eronex_layout_product_single',
					array(
						'title' => esc_html__( 'Single Product', 'eronex' ),
						'panel' => 'eronex_layout',
					)
				);
			}

			new Section(
				'eronex_no_content',
				array(
					'title' => esc_html__( 'No Content', 'eronex' ),
					'panel' => 'eronex_layout',
				)
			);
			new Section(
				'eronex_not_found',
				array(
					'title' => esc_html__( 'Not Found', 'eronex' ),
					'panel' => 'eronex_layout',
				)
			);

			//Footer
			new Section(
				'eronex_footer_general',
				array(
					'title' => esc_html__( 'Footer', 'eronex' ),
					'panel' => '',
					'priority'   => 103,
				)
			);
		}

		/**
		 * Loads Customizer field definition files.
		 *
		 * Resolves responsive media-query breakpoints from theme options, then
		 * requires field files for General, Header, Hamburger, Banner, Layout
		 * (including WooCommerce when available), and Footer sections.
		 *
		 * @since 0.0.1
		 */
		public function register_fields() {

			$tablet_breakpoint_option = eronex_option( 'eronex_tablet_breakpoint', 1025 );
			$mobile_breakpoint_option = eronex_option( 'eronex_mobile_breakpoint', 767 );

			$tablet_breakpoint = ! empty( $tablet_breakpoint_option ) ? $tablet_breakpoint_option : 1025;
			$mobile_breakpoint = ! empty( $mobile_breakpoint_option ) ? $mobile_breakpoint_option : 767;

			$desktop_media_query = '@media all';
			$tablet_media_query  = "@media (max-width: {$tablet_breakpoint}px)";
			$mobile_media_query  = "@media (max-width: {$mobile_breakpoint}px)";

			$separator_color = '#cacacac2';

			//General Fields
			require ERONEX_DIR . 'includes/customizer/fields/general/layout.php';
			require ERONEX_DIR . 'includes/customizer/fields/general/fonts.php';
			require ERONEX_DIR . 'includes/customizer/fields/general/colors.php';
			require ERONEX_DIR . 'includes/customizer/fields/general/button.php';
			require ERONEX_DIR . 'includes/customizer/fields/general/pagination.php';
			require ERONEX_DIR . 'includes/customizer/fields/general/scroll-top.php';
			require ERONEX_DIR . 'includes/customizer/fields/general/preloader.php';

			//Header Fields
			require ERONEX_DIR . 'includes/customizer/fields/header/layout.php';
			require ERONEX_DIR . 'includes/customizer/fields/header/logo.php';
			require ERONEX_DIR . 'includes/customizer/fields/header/menu.php';
			require ERONEX_DIR . 'includes/customizer/fields/header/submenu.php';
			require ERONEX_DIR . 'includes/customizer/fields/header/search.php';
			require ERONEX_DIR . 'includes/customizer/fields/header/button.php';

			//Hamburger Fields
			require ERONEX_DIR . 'includes/customizer/fields/hamburger/toggle.php';
			require ERONEX_DIR . 'includes/customizer/fields/hamburger/layout.php';
			require ERONEX_DIR . 'includes/customizer/fields/hamburger/close.php';
			require ERONEX_DIR . 'includes/customizer/fields/hamburger/logo.php';
			require ERONEX_DIR . 'includes/customizer/fields/hamburger/menu.php';
			require ERONEX_DIR . 'includes/customizer/fields/hamburger/submenu-icon.php';
			require ERONEX_DIR . 'includes/customizer/fields/hamburger/back.php';

			//Banner Fields
			require ERONEX_DIR . 'includes/customizer/fields/banner/layout.php';
			require ERONEX_DIR . 'includes/customizer/fields/banner/title.php';
			require ERONEX_DIR . 'includes/customizer/fields/banner/breadcrumb.php';

			//Layout
			require ERONEX_DIR . 'includes/customizer/fields/layout/archive.php';
			require ERONEX_DIR . 'includes/customizer/fields/layout/page.php';
			require ERONEX_DIR . 'includes/customizer/fields/layout/single.php';

			if ( class_exists( 'WooCommerce' ) ) {
				require ERONEX_DIR . 'includes/customizer/fields/layout/shop-archive.php';
				require ERONEX_DIR . 'includes/customizer/fields/layout/product-single.php';
			}

			require ERONEX_DIR . 'includes/customizer/fields/layout/no-content.php';
			require ERONEX_DIR . 'includes/customizer/fields/layout/not-found.php';

			//Footer
			require ERONEX_DIR . 'includes/customizer/fields/footer/general.php';
		}

		/**
		 * Bootstraps Kirki Pro control extensions used by Eronex fields.
		 *
		 * Loads Headline Divider, Input Slider, Margin/Padding, Responsive,
		 * and Tabs packages when they are not already registered.
		 *
		 * @since 0.0.1
		 */
		public function kirki_extensions() {
			//Headline Diver
			if ( ! class_exists( '\Kirki\Pro\HeadlineDivider\Init' ) ) {
				require ERONEX_DIR . 'vendor/kirki/pro-src/packages/kirki-pro-headline-divider/vendor/autoload.php';
				new Kirki\Pro\HeadlineDivider\Init();
			}
			//Input Control
			if ( ! class_exists( '\Kirki\Pro\InputSlider\Init' ) ) {
				require ERONEX_DIR . 'vendor/kirki/pro-src/packages/kirki-pro-input-slider/vendor/autoload.php';
				new Kirki\Pro\InputSlider\Init();
			}
			//Margin/Padding
			if ( ! class_exists( '\Kirki\Pro\MarginPadding\Init' ) ) {
				require ERONEX_DIR . 'vendor/kirki/pro-src/packages/kirki-pro-margin-padding/vendor/autoload.php';
				new Kirki\Pro\MarginPadding\Init();
			}
			//Responsive
			if ( ! class_exists( '\Kirki\Pro\Responsive\Init' ) ) {
				require ERONEX_DIR . 'vendor/kirki/pro-src/packages/kirki-pro-responsive/vendor/autoload.php';
				new Kirki\Pro\Responsive\Init();
			}
			//Tabs
			if ( ! class_exists( '\Kirki\Pro\Tabs\Init' ) ) {
				require ERONEX_DIR . 'vendor/kirki/pro-src/packages/kirki-pro-tabs/vendor/autoload.php';
				new Kirki\Pro\Tabs\Init();
			}
		}

		/**
		 * Builds and enqueues Customizer-driven frontend inline CSS.
		 *
		 * Captures output from `print-css.php`, allows filtering, minifies the
		 * result, and attaches it to the `eronex-theme` stylesheet handle.
		 *
		 * @since 0.0.1
		 */
		public function frontend_inline_css() {
			$output_css = '';
			ob_start();
			require_once ERONEX_DIR . 'includes/customizer/output/print-css.php';
			$output_css = ob_get_contents();
			ob_end_clean();

			/**
			 * Filters the theme Customizer inline CSS before minification.
			 *
			 * @since 0.0.1
			 *
			 * @param string $output_css Generated CSS from Customizer settings
			 *                           and `print-css.php`.
			 */
			$output_css = apply_filters( 'eronex_theme_inline_css', $output_css );
			$output_css = preg_replace( '#/\*.*?\*/#s', '', $output_css );
			$output_css = preg_replace( '/\s*([{}|:;,])\s+/', '$1', $output_css );
			$output_css = preg_replace( '/\s\s+(.*)/', '$1', $output_css );
			wp_add_inline_style( 'eronex-theme', $output_css );
		}
	}
}

//Initiated Class
Eronex_Customizer_Config::get_instance();
