<?php
/**
 * BigBlocks functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package BigBlocks
 * @subpackage BigBlocks
 * @since BigBlocks 1.0
 */

add_theme_support( 'editor-styles' );
add_editor_style( array( 'style.css' ) );

// Adds theme support for post formats.
if ( ! function_exists( 'bigblocks_post_format_setup' ) ) :
	/**
	 * Adds theme support for post formats.
	 *
	 * @since BigBlocks 1.0
	 *
	 * @return void
	 */
	function bigblocks_post_format_setup() {
		add_theme_support( 'post-formats', array( 'aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video' ) );
	}
endif;
add_action( 'after_setup_theme', 'bigblocks_post_format_setup' );

// Enqueues editor-style.css in the editors.
if ( ! function_exists( 'bigblocks_editor_style' ) ) :
	/**
	 * Enqueues editor-style.css in the editors.
	 *
	 * @since BigBlocks 1.0
	 *
	 * @return void
	 */
	function bigblocks_editor_style() {
		add_editor_style( get_parent_theme_file_uri( 'assets/css/editor-style.css' ) );
	}
endif;
add_action( 'after_setup_theme', 'bigblocks_editor_style' );

// Enqueues style.css on the front.
if ( ! function_exists( 'bigblocks_enqueue_styles' ) ) :
	/**
	 * Enqueues style.css on the front.
	 *
	 * @since BigBlocks 1.0
	 *
	 * @return void
	 */
	function bigblocks_enqueue_styles() {
		wp_enqueue_style(
			'bigblocks-style',
			get_parent_theme_file_uri( 'style.css' ),
			array(),
			wp_get_theme()->get( 'Version' )
		);
	}
endif;
add_action( 'wp_enqueue_scripts', 'bigblocks_enqueue_styles' );

// Registers custom block styles.
if ( ! function_exists( 'bigblocks_block_styles' ) ) :
	/**
	 * Registers custom block styles.
	 *
	 * @since BigBlocks 1.0
	 *
	 * @return void
	 */
	function bigblocks_block_styles() {
		register_block_style(
			'core/list',
			array(
				'name'         => 'checkmark-list',
				'label'        => esc_html__( 'Checkmark', 'bigblocks' ),
				'inline_style' => '
				ul.is-style-checkmark-list {
					list-style-type: "\2713";
				}

				ul.is-style-checkmark-list li {
					padding-inline-start: 1ch;
				}',
			)
		);
	}
endif;
add_action( 'init', 'bigblocks_block_styles' );

// Registers pattern categories.
if ( ! function_exists( 'bigblocks_pattern_categories' ) ) :
	/**
	 * Registers pattern categories.
	 *
	 * @since BigBlocks 1.0
	 *
	 * @return void
	 */
	function bigblocks_pattern_categories() {

		register_block_pattern_category(
			'bigblocks_page',
			array(
				'label'       => esc_html__( 'Pages', 'bigblocks' ),
				'description' => esc_html__( 'A collection of full page layouts.', 'bigblocks' ),
			)
		);

		register_block_pattern_category(
			'bigblocks_post-format',
			array(
				'label'       => esc_html__( 'Post formats', 'bigblocks' ),
				'description' => esc_html__( 'A collection of post format patterns.', 'bigblocks' ),
			)
		);
	}
endif;
add_action( 'init', 'bigblocks_pattern_categories' );

/**
 * Registers pattern categories.
 *
 * @since BigBlocks 1.0
 *
 * @return void
 */
function bigblocks_register_pattern_category() {

	$patterns = array();

	$block_pattern_categories = array(
		'bigblocks' => array( 'label' => esc_html__( 'BigBlocks Theme', 'bigblocks' ) )
	);

	$block_pattern_categories = apply_filters( 'bigblocks_block_pattern_categories', $block_pattern_categories );

	foreach ( $block_pattern_categories as $name => $properties ) {
		if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( $name ) ) {
			register_block_pattern_category( $name, $properties );
		}
	}
}
add_action( 'init', 'bigblocks_register_pattern_category', 9 );


// Registers block binding sources.
if ( ! function_exists( 'bigblocks_register_block_bindings' ) ) :
	/**
	 * Registers the post format block binding source.
	 *
	 * @since BigBlocks 1.0
	 *
	 * @return void
	 */
	function bigblocks_register_block_bindings() {
		register_block_bindings_source(
			'bigblocks/format',
			array(
				'label'              => _x( 'Post format name', 'Label for the block binding placeholder in the editor', 'bigblocks' ),
				'get_value_callback' => 'bigblocks_format_binding',
			)
		);
	}
endif;
add_action( 'init', 'bigblocks_register_block_bindings' );

// Registers block binding callback function for the post format name.
if ( ! function_exists( 'bigblocks_format_binding' ) ) :
	/**
	 * Callback function for the post format name block binding source.
	 *
	 * @since BigBlocks 1.0
	 *
	 * @return string|void Post format name, or nothing if the format is 'standard'.
	 */
	function bigblocks_format_binding() {
		$post_format_slug = get_post_format();

		if ( $post_format_slug && 'standard' !== $post_format_slug ) {
			return get_post_format_string( $post_format_slug );
		}
	}
endif;


/**
 * Pro upgrade notice
 */
function bigblocks_pro_upgrade_admin_notice() {

    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    ?>
    <div class="notice notice-info is-dismissible bigblocks-pro-notice bigblocks-pro-animate">
        <div class="">

            <div class="bigblocks-pro-header">
                <span class="dashicons dashicons-star-filled"></span>
                <strong class="bigblocks-pro-title">
                    <?php esc_html_e( 'Upgrade to BigBlocksPro', 'bigblocks' ); ?>
                </strong>
            </div>
            <p class="bigblocks-pro-desc">
                <?php esc_html_e( 'You are currently using the free version of the BigBlocks theme. Upgrade to the Pro version to unlock additional features:', 'bigblocks' ); ?>
            </p>
            <ul class="bigblocks-pro-features">
                <li><?php esc_html_e( 'One Click Demo Content Import', 'bigblocks' ); ?></li>
                <li><?php esc_html_e( '20+ Custom Design Patterns', 'bigblocks' ); ?></li>
                <li><?php esc_html_e( '6+ Custom Pages (About, Services, Pricing, Team, Blog and Contact Page)', 'bigblocks' ); ?></li>
                <li><?php esc_html_e( 'Priority Theme Support', 'bigblocks' ); ?></li>                
                <li><?php esc_html_e( 'Automatic Theme Updates', 'bigblocks' ); ?></li>                
            </ul>

            <div class="cta-button">
                <?php 
                    $theme = wp_get_theme();
                ?>
                <a href="<?php echo esc_url( $theme->get( 'AuthorURI' ) . '/themes/bigblockspro' ); ?>"
                   class="button button-primary bigblocks-pro-btn"
                   target="_blank"
                   rel="noopener noreferrer">
                    <?php esc_html_e( 'Upgrade to BigBlocksPro', 'bigblocks' ); ?>
                </a>
            </div>

        </div>
    </div>
    <?php
}
add_action( 'admin_notices', 'bigblocks_pro_upgrade_admin_notice' );

add_action( 'admin_enqueue_scripts', function () {
    wp_add_inline_style(
        'wp-admin',
        '
        .bigblocks-pro-notice {
            padding: 0;
            border-left: none;
            background: transparent;
        }

        .bigblocks-pro-animate {
            position: relative;
            padding: 14px 18px 16px;
            background: #fffaf3;
            border-radius: 4px;
        }

        .bigblocks-pro-animate::before {
            content: "";
            position: absolute;
            inset: 0;
            border-left: 6px solid #ff9800;
            animation: bigblocksBorderPulse 2.2s ease-in-out infinite;
        }

        .bigblocks-pro-header {
            display: flex;
            align-items: center;
            gap: 8px;
            margin-bottom: 6px;
            position: relative;
            z-index: 1;
        }

        .bigblocks-pro-header .dashicons {
            color: #ff9800;
            font-size: 22px;
        }

        .bigblocks-pro-title {
            font-size: 14px;
        }

        /* Clean list styling */
        .bigblocks-pro-features {
            list-style: none;
            margin: 0 0 10px;
            padding: 0;
            position: relative;
            z-index: 1;
        }

        .bigblocks-pro-features li {
            font-size: 13px;
            color: #555;
            padding-left: 20px;
            margin: 2px 0;
            position: relative;
        }

        .bigblocks-pro-features li::before {
            content: "\f147"; /* dashicons-yes */
            font-family: dashicons;
            position: absolute;
            left: 0;
            top: 0;
            color: #46b450;
            font-size: 16px;
        }

        .cta-button {
            position: relative;
            z-index: 1;
        }

        @keyframes bigblocksBorderPulse {
            0%   { box-shadow: 0 0 0 0 rgba(255,152,0,.35); }
            70%  { box-shadow: 0 0 0 10px rgba(255,152,0,0); }
            100% { box-shadow: 0 0 0 0 rgba(255,152,0,0); }
        }
        '
    );
});

