<?php

namespace Brandy\Core;

use Brandy\Customizer\Panels\General\ButtonSettingsSection;
use Brandy\Traits\SingletonTrait;
use Brandy\Utils\Helpers;
use Brandy\Utils\MenuHelper;

class ThemeSetup {
	use SingletonTrait;

	protected function __construct() {
		add_action( 'after_setup_theme', array( $this, 'add_theme_supports' ) );

		add_action( 'init', array( $this, 'register_menus' ) );
		add_action( 'init', array( $this, 'register_block_patterns_and_categories' ) );
		add_action( 'init', array( $this, 'unregister_wc_patterns_when_necessary' ) );
		add_action( 'init', array( $this, 'unregister_patterns_on_wordpress_org' ) );
		add_action( 'init', array( $this, 'unregister_brandy_blocks_patterns_when_necessary' ) );

		add_filter( 'get_block_templates', array( $this, 'remove_wc_templates_when_necessary' ) );

		// Remove !important from WP preset classes (.has-*-color, .has-*-background-color, etc.)
		// so theme variation styles can override them without specificity wars
		// add_action( 'wp_enqueue_scripts', array( $this, 'strip_preset_important' ), PHP_INT_MAX );
		// add_action( 'enqueue_block_editor_assets', array( $this, 'strip_preset_important' ), PHP_INT_MAX );

		// Enable "Show template" in editor by default for new users
		add_action( 'enqueue_block_editor_assets', array( $this, 'set_default_editor_preferences' ) );

		$this->hjack_setup();
	}

	/**
	 * Enable "Show template" in the post editor by default.
	 * Sets renderingModes.brandy.page to "template-locked" if not already set.
	 */
	public function set_default_editor_preferences() {
		$user_id = get_current_user_id();
		if ( ! $user_id ) {
			return;
		}

		$prefs = get_user_meta( $user_id, 'wp_persisted_preferences', true );
		if ( ! is_array( $prefs ) ) {
			$prefs = array();
		}

		// Only set if user hasn't explicitly changed the rendering mode
		if ( ! isset( $prefs['core']['renderingModes']['brandy']['page'] ) ) {
			$prefs['core']['renderingModes']['brandy']['page'] = 'template-locked';
			update_user_meta( $user_id, 'wp_persisted_preferences', $prefs );
		}
	}

	/**
	 * Strip !important from WP global stylesheet preset classes.
	 * Runs at PHP_INT_MAX priority so the inline CSS is already registered.
	 */
	public function strip_preset_important() {
		global $wp_styles;
		if ( ! isset( $wp_styles->registered['global-styles'] ) ) {
			return;
		}
		$style = $wp_styles->registered['global-styles'];
		if ( ! empty( $style->extra['after'] ) ) {
			$style->extra['after'] = array_map( function( $css ) {
				return str_replace( ') !important', ')', $css );
			}, $style->extra['after'] );
		}
	}

	public function add_theme_supports() {
		add_theme_support( 'title-tag' );
		add_theme_support( 'customize-selective-refresh-widgets' );
		add_theme_support( 'responsive-embeds' );
		add_theme_support(
			'woocommerce',
			array(
				'thumbnail_image_width' => 800,
				'single_image_width'    => 800,
			)
		);
		add_theme_support( 'wc-product-gallery-zoom' );
		add_theme_support( 'wc-product-gallery-lightbox' );
		add_theme_support( 'wc-product-gallery-slider' );

		add_theme_support( 'title-tag' );
		add_theme_support( 'automatic-feed-links' );
		add_theme_support( 'align-wide' );
		add_theme_support( 'custom-background', array() );
		add_theme_support( 'custom-header', array() );
		add_theme_support( 'custom-logo', array() );
		add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption', 'style', 'script' ) );
		add_theme_support( 'wp-block-styles' );
		add_theme_support( 'post-thumbnails' );

	}

	public function register_menus() {
		register_nav_menus( MenuHelper::get_locations() );
	}

	public function register_block_patterns_and_categories() {
		register_block_pattern_category( 'brandy', array( 'label' => __( 'Brandy', 'brandy' ) ) );
	}

	public function unregister_wc_patterns_when_necessary() {

		if ( is_wc_installed() ) {
			return;
		}

		$themes   = array();
		$theme    = wp_get_theme();
		$themes[] = $theme;
		if ( $theme->parent() ) {
			$themes[] = $theme->parent();
		}
		$patterns = array();

		foreach ( $themes as $theme ) {
			$patterns = array_merge( $patterns, $theme->get_block_patterns() );
		}

		foreach ( $patterns as $pattern_name => $pattern ) {
			if ( empty( $pattern['categories'] ) ) {
				continue;
			}
			if ( in_array( 'woocommerce', $pattern['categories'], true ) ) {
				unregister_block_pattern( $pattern['slug'] );
			}
		}

	}

	public function unregister_patterns_on_wordpress_org() {

		if ( ! brandy_is_wordpress_playground() ) {
			return;
		}

		$themes   = array();
		$theme    = wp_get_theme();
		$themes[] = $theme;
		if ( $theme->parent() ) {
			$themes[] = $theme->parent();
		}
		$patterns = array();

		foreach ( $themes as $theme ) {
			$patterns = array_merge( $patterns, $theme->get_block_patterns() );
		}

		foreach ( $patterns as $pattern_name => $pattern ) {
			if ( ! empty( $pattern['keywords'] ) && in_array( 'brandy-demo', $pattern['keywords'], true ) ) {
				continue;
			}
			unregister_block_pattern( $pattern['slug'] );
		}

	}

	public function unregister_brandy_blocks_patterns_when_necessary() {

		if ( is_brandy_blocks_installed() ) {
			return;
		}

		$themes   = array();
		$theme    = wp_get_theme();
		$themes[] = $theme;
		if ( $theme->parent() ) {
			$themes[] = $theme->parent();
		}
		$patterns = array();

		foreach ( $themes as $theme ) {
			$patterns = array_merge( $patterns, $theme->get_block_patterns() );
		}

		foreach ( $patterns as $pattern_name => $pattern ) {
			if ( empty( $pattern['categories'] ) ) {
				continue;
			}
			if ( in_array( 'brandy-blocks', $pattern['categories'], true ) ) {
				unregister_block_pattern( $pattern['slug'] );
			}
			if ( 'brandy/simple-contact-form' === $pattern['slug'] ) {
				unregister_block_pattern( $pattern['slug'] );
			}
		}

	}

	public function remove_wc_templates_when_necessary( $query_result ) {
		if ( is_wc_installed() ) {
			return $query_result;
		}

		$removal_template_slugs = array(
			'archive-product',
			'order-confirmation',
			'page-cart',
			'page-checkout',
			'page-my-account',
			'product-search-results',
			'single-product',
		);

		$query_result = array_filter(
			$query_result,
			function( $template ) use ( $removal_template_slugs ) {
				if ( false === strpos( $template->theme, 'brandy' ) && false === strpos( $template->id, 'brandy' ) ) {
					return true;
				}
				return ! in_array( $template->slug, $removal_template_slugs, true );
			}
		);

		return $query_result;
	}

	public function hjack_setup() {

		// Trick to add outline style for button element
		add_action(
			'init',
			function() {
				$parsed_block = array(
					'attrs'     => array(
						'className' => 'wp-block-button is-style-outline',
					),
					'blockName' => 'core/button',
				);
				if ( function_exists( '\wp_render_block_style_variation_support_styles' ) ) {
					\wp_render_block_style_variation_support_styles( $parsed_block );
				}
			}
		);

		add_filter(
			'wp_prepare_themes_for_js',
			function( $themes ) {
				if ( isset( $themes['brandy']['actions']['customize'] ) ) {
					$themes['brandy']['actions']['customize'] = wp_customize_url( 'brandy' );
				}
				return $themes;
			}
		);

		add_filter(
			'script_loader_tag',
			function( $tag, $handle ) {
				// Skip if already has defer or async
				if ( strpos( $tag, ' defer' ) !== false || strpos( $tag, ' async' ) !== false ) {
					return $tag;
				}
				// Apply defer to brandy scripts for non-blocking load
				if ( strpos( $handle, 'brandy' ) !== false ) {
					return str_replace( ' src=', ' defer src=', $tag );
				}
				return $tag;
			},
			10,
			2
		);

		add_filter(
			'theme_scandir_exclusions',
			function( $exclusions ) {
				if ( ! isset( $_POST['themename'] ) || 'brandy' !== $_POST['themename'] ) {
					return $exclusions;
				}
				if ( ! check_admin_referer( 'themecheck-nonce' ) ) {
					return $exclusions;
				}
				$exclusions[] = 'icons';
				$exclusions[] = 'Elements';
				return $exclusions;
			}
		);

		// Disable WordPress emoji scripts and styles
        if ( apply_filters( 'brandy_should_disable_wp_emoji_scripts_and_styles', true ) ) {
            remove_action('wp_head', 'print_emoji_detection_script', 7);
            remove_action('admin_print_scripts', 'print_emoji_detection_script');
            remove_action('wp_print_styles', 'print_emoji_styles');
            remove_action('admin_print_styles', 'print_emoji_styles');
            remove_filter('the_content_feed', 'wp_staticize_emoji');
            remove_filter('comment_text_rss', 'wp_staticize_emoji');
            remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
        }
	}

}

ThemeSetup::get_instance();
