<?php
/**
 * The CustomizerLoader class is responsible for loading and managing the customizer settings for the Brandy theme.
 *
 * @package Brandy/Customizer
 * @since 1.0.0
 */

namespace Brandy\Customizer;

use Brandy\Core\Services\BuilderService;
use Brandy\Core\Services\StringVariablesService;
use Brandy\Customizer\Elements\ElementsLoader;
use Brandy\Customizer\Layouts\FooterRowSettings;
use Brandy\Customizer\Layouts\FooterSettings;
use Brandy\Customizer\Layouts\HeaderRowSettings;
use Brandy\Customizer\Layouts\HeaderSettings;
use Brandy\Customizer\Layouts\ToggleOffCanvasSettings;
use Brandy\Customizer\Panels\FooterPanel;
use Brandy\Customizer\Panels\General\ButtonSettingsSection;
use Brandy\Customizer\Panels\General\GeneralPanel;
use Brandy\Customizer\Panels\HeaderPanel;
use Brandy\Customizer\Panels\WooCommerce\WooCommercePanel;
use Brandy\CustomizerVite;
use Brandy\I18n;
use Brandy\Niches\NicheLoader;
use Brandy\Traits\SingletonTrait;

/**
 * Declare class
 */
class CustomizerLoader {
	use SingletonTrait;

	/**
	 * Constructor
	 */
	protected function __construct() {

		$this->load_classes();

		add_action( 'customize_controls_print_scripts', array( $this, 'controls_enqueue_scripts' ) );
		add_action(
			'customize_save_after',
			function() {
				if ( ! isset( $_POST['nonce'] ) ) {
					return;
				}

				if ( ! wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ), 'save-customize_' . wp_get_theme()->get_stylesheet() ) ) {
					return;
				}
				if ( isset( $_POST['header_settings'] ) ) {
					brandy_save_header_settings( json_decode( wp_unslash( $_POST['header_settings'] ), true ) );
				}
				if ( isset( $_POST['footer_settings'] ) ) {
					brandy_save_footer_settings( json_decode( wp_unslash( $_POST['footer_settings'] ), true ) );
				}
				if ( isset( $_POST['button_global_styles'] ) ) {
					self::save_button_to_global_styles(
						json_decode( wp_unslash( $_POST['button_global_styles'] ), true )
					);
				}
			}
		);

		add_filter(
			'gettext',
			function( $translation, $text ) {
				if ( 'Shift-click to edit this element.' === $text ) {
					return '';
				}
				return $translation;
			},
			PHP_INT_MAX,
			2
		);

		add_action( 'customize_register', array( $this, 'remove_unused_customizer_panel' ) );
	}

	private function load_classes() {
		/**
		 * Require partials process
		 */
		PartialsLoader::get_instance();

		/**
		 * Register panels.
		 */
		HeaderPanel::get_instance();
		FooterPanel::get_instance();
		GeneralPanel::get_instance();
		WooCommercePanel::get_instance();

		do_action( 'brandy_register_customizer_panels' );

		do_action( 'brandy_after_register_customizer_panels' );

		/**
		 * Require element process class
		 */
		ElementsLoader::get_instance();

		/**
		 * Require loading settings
		 */
		HeaderSettings::get_instance();
		HeaderRowSettings::get_instance();
		FooterSettings::get_instance();
		FooterRowSettings::get_instance();
		ToggleOffCanvasSettings::get_instance();
	}

	private function get_localize_data() {
		$exclude_pages = function_exists( 'wc_get_page_id' ) ? array(
			\wc_get_page_id( 'cart' ),
			\wc_get_page_id( 'checkout' ),
			\wc_get_page_id( 'myaccount' ),
		) : array();
		$pages         = get_pages(
			array(
				'post_type'   => 'page',
				'post_status' => 'publish,private,draft',
				'child_of'    => 0,
				'parent'      => -1,
				'exclude'     => $exclude_pages,
				'sort_order'  => 'asc',
				'sort_column' => 'post_title',
			)
		);
		$page_choices  = array(
			'' => array(
				'title' => __( 'No page set', 'brandy' ),//phpcs:ignore
				'link'  => '#',
			),
		)
		+ array_combine(
			array_map( 'strval', wp_list_pluck( $pages, 'ID' ) ),
			array_map(
				function( $p ) {
					return array(
						'title' => $p->post_title,
						'link'  => get_page_link( $p->ID ),
					);
				},
				$pages
			)
		);
		if ( function_exists( 'wc_get_products' ) ) {
			$sample_products = \wc_get_products( array( 'limit' => 1 ) );
		}

		$data = array(
			'ajax'              => array(
				'path'   => admin_url( 'admin-ajax.php' ),
				'nonces' => array(),
			),
			'rtl'               => is_rtl(),
			'paths'             => array(
				'nav_menu'               => admin_url( 'nav-menus.php' ),
				'wc_checkout_page'       => brandy_get_checkout_page_url(),
				'wc_cart_page'           => brandy_get_cart_page_url(),
				'wc_shop_page'           => brandy_get_shop_page_url(),
				'wc_single_product_page' => empty( $sample_products ) ? '#' : $sample_products[0]->get_permalink(),
				'yay_currency_manage'    => admin_url( 'admin.php?page=yay_currency' ),
			),
			'colors'            => array(
				'icon' => array(
					'normal' => BRANDY_ICON_COLOR_NORMAL,
				),
			),
			'content_variables' => StringVariablesService::get_data(),
			'pages'             => $page_choices,
			'i18n'              => I18n::get_translations(),
			'theme_settings'    => wp_get_global_settings(),
			'extra_panels'      => apply_filters( 'brandy_customizer_extra_panels', array() ),
			'defaults'          => array(),
			'others'            => array(
				'site_icon' => get_site_icon_url(),
				// 'execution_unique_id' => wp_unique_id(),
			),
			'wp_global_styles'        => wp_get_global_styles(),
			'theme_default_styles'    => wp_get_global_styles( array(), array( 'origin' => 'theme' ) ),
			'theme_default_settings'  => wp_get_global_settings( array(), array( 'origin' => 'theme' ) ),
		);

		$default_header = apply_filters( 'brandy_default_header_settings', HeaderPanel::get_default_template() );
		if ( empty( $default_header['elements']['logo_1']['settings']['logo']['url'] ) ) {
			$default_header['elements']['logo_1']['settings']['logo']['url'] = get_site_icon_url();
		}
		$data['defaults']['header'] = $default_header;

		$default_footer = apply_filters( 'brandy_default_footer_settings', FooterPanel::get_default_template() );
		if ( empty( $default_footer['elements']['logo_2']['settings']['logo']['url'] ) ) {
			$default_footer['elements']['logo_2']['settings']['logo']['url'] = get_site_icon_url();
		}
		$data['defaults']['footer'] = $default_footer;

		return apply_filters( 'brandy_extra_localize', $data );
	}

	/**
	 * Enqueue scripts/styles for controller in customize page
	 */
	public function controls_enqueue_scripts() {
		// wp_enqueue_style( 'dashicons' );
		wp_enqueue_media();
		wp_enqueue_editor();
		if ( is_admin() ) {
			\_WP_Editors::print_default_editor_scripts();
		}
		wp_add_inline_script(
			'wp-customize-widgets',
			'var customizeWidgetInitialize = wp.customizeWidgets.initialize;' .
			'wp.customizeWidgets = {initialize: function (a, b) {
				window.brandyWidgetsEditorName = a
				window.brandyWidgetsBlockEditorSettings = b

				customizeWidgetInitialize(a, b)
			}}'
		);
		CustomizerVite::enqueue_vite( 'main.tsx', '3003' );
		wp_localize_script( 'module/brandy/main.tsx', 'brandyCustomizerData', $this->get_localize_data() );
		wp_localize_script(
			'module/brandy/main.tsx',
			'brandyHeaderPresets',
			HeaderPanel::get_preset_settings()
		);
		wp_localize_script(
			'module/brandy/main.tsx',
			'brandyFooterPresets',
			FooterPanel::get_preset_settings()
		);

		$all_niches    = NicheLoader::get_instance()->get_niches();
		$niche_headers = array();
		$niche_footers = array();
		foreach ( $all_niches as $niche ) {
			foreach ( $niche['template_data']['headers'] ?? array() as $header_file ) {
				if ( ! file_exists( $header_file ) ) {
					continue;
				}

				$template_data = json_decode( file_get_contents( $header_file ), true ); //phpcs:ignore
				$niche_headers[] = $template_data;
			}
			foreach ( $niche['template_data']['footers'] ?? array() as $footer_file ) {
				if ( ! file_exists( $footer_file ) ) {
					continue;
				}

				$template_data = json_decode( file_get_contents( $footer_file ), true ); //phpcs:ignore
				$niche_footers[] = $template_data;
			}
		}
		wp_localize_script(
			'module/brandy/main.tsx',
			'brandyNichesHeaders',
			$niche_headers
		);
		wp_localize_script(
			'module/brandy/main.tsx',
			'brandyNichesFooters',
			$niche_footers
		);
		$header_settings = brandy_get_header_settings();
		$footer_settings = brandy_get_footer_settings();
		wp_localize_script(
			'module/brandy/main.tsx',
			'brandyHeaderSettings',
			$header_settings
		);
		wp_localize_script(
			'module/brandy/main.tsx',
			'brandyFooterSettings',
			$footer_settings
		);
		wp_localize_script(
			'module/brandy/main.tsx',
			'brandySettingsLayouts',
			BuilderService::get_settings_layouts()
		);
		wp_localize_script(
			'module/brandy/main.tsx',
			'brandyDefaultHeaderStyles',
			apply_filters(
				'brandy_default_header_menu_styles',
				[]
			)
		);
		wp_localize_script(
			'module/brandy/main.tsx',
			'brandyElements',
			apply_filters( 'brandy_all_elements', array() )
		);
		brandy_update_unsaved_header_settings( $header_settings );
		brandy_update_unsaved_footer_settings( $footer_settings );
	}

	/**
	 * Save button colors from Customizer directly to global styles post.
	 * Merges into existing styles without overwriting other settings.
	 */
	private static function save_button_to_global_styles( $data ) {
		if ( empty( $data ) || ! is_array( $data ) ) {
			return;
		}

		$post_id = \WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
		$post    = get_post( $post_id );
		if ( ! $post ) {
			return;
		}

		$content = json_decode( $post->post_content, true ) ?: array();

		// Ensure nested structure exists
		if ( ! isset( $content['styles'] ) ) {
			$content['styles'] = array();
		}
		if ( ! isset( $content['styles']['elements'] ) ) {
			$content['styles']['elements'] = array();
		}
		if ( ! isset( $content['styles']['elements']['button'] ) ) {
			$content['styles']['elements']['button'] = array();
		}
		if ( ! isset( $content['styles']['elements']['button']['color'] ) ) {
			$content['styles']['elements']['button']['color'] = array();
		}
		if ( ! isset( $content['styles']['elements']['button'][':hover'] ) ) {
			$content['styles']['elements']['button'][':hover'] = array();
		}
		if ( ! isset( $content['styles']['elements']['button'][':hover']['color'] ) ) {
			$content['styles']['elements']['button'][':hover']['color'] = array();
		}

		// Map customizer keys to global styles structure
		if ( ! empty( $data['buttonBg'] ) ) {
			$content['styles']['elements']['button']['color']['background'] = $data['buttonBg'];
		}
		if ( ! empty( $data['buttonText'] ) ) {
			$content['styles']['elements']['button']['color']['text'] = $data['buttonText'];
		}
		if ( ! empty( $data['hoverBg'] ) ) {
			$content['styles']['elements']['button'][':hover']['color']['background'] = $data['hoverBg'];
		}
		if ( ! empty( $data['hoverText'] ) ) {
			$content['styles']['elements']['button'][':hover']['color']['text'] = $data['hoverText'];
		}

		// Save outline button settings if present
		if ( ! empty( $data['outline'] ) && is_array( $data['outline'] ) ) {
			$outline = $data['outline'];

			// Ensure nested structures exist
			if ( ! isset( $content['styles']['blocks'] ) ) {
				$content['styles']['blocks'] = array();
			}
			if ( ! isset( $content['styles']['blocks']['core/button'] ) ) {
				$content['styles']['blocks']['core/button'] = array();
			}
			if ( ! isset( $content['styles']['blocks']['core/button']['variations'] ) ) {
				$content['styles']['blocks']['core/button']['variations'] = array();
			}
			if ( ! isset( $content['styles']['blocks']['core/button']['variations']['outline'] ) ) {
				$content['styles']['blocks']['core/button']['variations']['outline'] = array();
			}

			$outline_ref = &$content['styles']['blocks']['core/button']['variations']['outline'];

			// Main color → variation text color (drives border + text)
			if ( ! empty( $outline['mainColor'] ) ) {
				$outline_ref['color'] = array(
					'text'       => $outline['mainColor'],
					'background' => '#ffffff00',
				);
			}

			// Hover CSS based on style mode
			$hover_style = $outline['hoverStyle'] ?? 'solid';
			if ( 'tinted' === $hover_style ) {
				// Convert main color to 8-digit hex with 10% opacity (0x1A)
				$main_hex = ltrim( $outline['mainColor'] ?? '', '#' );
				$main_hex = substr( $main_hex, 0, 6 );
				$tinted   = $main_hex ? '#' . $main_hex . '1a' : 'transparent';
				$outline_ref['css'] = '&:hover{background:' . $tinted . ';}';
				// Native :hover key for WP 7.0+ (ignored on < 7.0)
				$outline_ref[':hover'] = array(
					'color' => array(
						'background' => $tinted,
					),
				);
			} else {
				$outline_ref['css'] = '&:hover{color:var(--wp--custom--color--button--outline--hover-foreground);background:var(--wp--custom--color--button--outline--hover-background);border-color:var(--wp--custom--color--button--outline--hover-border);}';
				// Native :hover key for WP 7.0+ (ignored on < 7.0)
				$outline_ref[':hover'] = array(
					'color'  => array(
						'text'       => 'var(--wp--custom--color--button--outline--hover-foreground)',
						'background' => 'var(--wp--custom--color--button--outline--hover-background)',
					),
					'border' => array(
						'color' => 'var(--wp--custom--color--button--outline--hover-border)',
					),
				);
			}

			// Custom settings for hover colors (used by solid mode CSS vars)
			if ( ! isset( $content['settings'] ) ) {
				$content['settings'] = array();
			}
			if ( ! isset( $content['settings']['custom'] ) ) {
				$content['settings']['custom'] = array();
			}
			if ( ! isset( $content['settings']['custom']['color'] ) ) {
				$content['settings']['custom']['color'] = array();
			}
			if ( ! isset( $content['settings']['custom']['color']['button'] ) ) {
				$content['settings']['custom']['color']['button'] = array();
			}
			if ( ! isset( $content['settings']['custom']['color']['button']['outline'] ) ) {
				$content['settings']['custom']['color']['button']['outline'] = array();
			}

			$custom_ref = &$content['settings']['custom']['color']['button']['outline'];
			$custom_ref['hover-style'] = $hover_style;

			if ( ! empty( $outline['hoverBg'] ) ) {
				$custom_ref['hover-background'] = $outline['hoverBg'];
				$custom_ref['hover-border']     = $outline['hoverBg'];
			}
			if ( ! empty( $outline['hoverText'] ) ) {
				$custom_ref['hover-foreground'] = $outline['hoverText'];
			}
		}

		// Save secondary button settings if present
		if ( ! empty( $data['secondary'] ) && is_array( $data['secondary'] ) ) {
			$secondary = $data['secondary'];

			// Ensure variation structure exists
			if ( ! isset( $content['styles']['blocks'] ) ) {
				$content['styles']['blocks'] = array();
			}
			if ( ! isset( $content['styles']['blocks']['core/button'] ) ) {
				$content['styles']['blocks']['core/button'] = array();
			}
			if ( ! isset( $content['styles']['blocks']['core/button']['variations'] ) ) {
				$content['styles']['blocks']['core/button']['variations'] = array();
			}
			if ( ! isset( $content['styles']['blocks']['core/button']['variations']['secondary'] ) ) {
				$content['styles']['blocks']['core/button']['variations']['secondary'] = array();
			}

			$secondary_ref = &$content['styles']['blocks']['core/button']['variations']['secondary'];

			// Variation color
			if ( ! empty( $secondary['bg'] ) || ! empty( $secondary['text'] ) ) {
				$secondary_ref['color'] = array(
					'background' => $secondary['bg'] ?? '#ededed',
					'text'       => $secondary['text'] ?? '',
				);
			}

			// Hover CSS uses custom setting variable
			$secondary_ref['css'] = '&:hover{background:var(--wp--custom--color--button--secondary--hover-background);}';
			// Native :hover key for WP 7.0+ (ignored on < 7.0)
			$secondary_ref[':hover'] = array(
				'color' => array(
					'background' => 'var(--wp--custom--color--button--secondary--hover-background)',
				),
			);

			// Custom setting for hover bg
			if ( ! isset( $content['settings'] ) ) {
				$content['settings'] = array();
			}
			if ( ! isset( $content['settings']['custom'] ) ) {
				$content['settings']['custom'] = array();
			}
			if ( ! isset( $content['settings']['custom']['color'] ) ) {
				$content['settings']['custom']['color'] = array();
			}
			if ( ! isset( $content['settings']['custom']['color']['button'] ) ) {
				$content['settings']['custom']['color']['button'] = array();
			}
			if ( ! isset( $content['settings']['custom']['color']['button']['secondary'] ) ) {
				$content['settings']['custom']['color']['button']['secondary'] = array();
			}

			if ( ! empty( $secondary['hoverBg'] ) ) {
				$content['settings']['custom']['color']['button']['secondary']['hover-background'] = $secondary['hoverBg'];
			}
		}

		wp_update_post( array(
			'ID'           => $post_id,
			'post_content' => wp_json_encode( $content ),
		) );

		// Invalidate global styles cache
		delete_transient( 'global_styles' );
		delete_transient( 'global_styles_' . get_stylesheet() );
		delete_transient( 'wp_get_global_settings' );
	}

	public function remove_unused_customizer_panel( $manager ) {
		$remove_sections = array( 'colors', 'header_image', 'background_image' );
		foreach ( $remove_sections as $section_id ) {
			$manager->remove_section( $section_id );
		}
	}

}

CustomizerLoader::get_instance();
