<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package VisualMag
 */

/**
 * Add customizer default values.
 *
 * @param array $default_options
 * @return array
 */
function visualmag_customizer_add_defaults( $default_options) {
	$defaults = array(
		// Excerpt Options
		'visualmag_excerpt_length'    => 20,
	);


	$updated_defaults = wp_parse_args( $defaults, $default_options );

	return $updated_defaults;
}
add_filter( 'visualmag_customizer_defaults', 'visualmag_customizer_add_defaults' );

/**
 * Returns theme mod value saved for option merging with default option if available.
 * @since 1.0
 */
function visualmag_gtm( $option ) {
	// Get our Customizer defaults
	$defaults = apply_filters( 'visualmag_customizer_defaults', true );

	return isset( $defaults[ $option ] ) ? get_theme_mod( $option, $defaults[ $option ] ) : get_theme_mod( $option );
}

if ( ! function_exists( 'visualmag_excerpt_length' ) ) :
	/**
	 * Sets the post excerpt length to n words.
	 *
	 * function tied to the excerpt_length filter hook.
	 * @uses filter excerpt_length
	 */
	function visualmag_excerpt_length( $length ) {
		if ( is_admin() ) {
			return $length;
		}

		// Getting data from Theme Options
		$length	= visualmag_gtm( 'visualmag_excerpt_length' );

		return absint( $length );
	} // visualmag_excerpt_length.
endif;
add_filter( 'excerpt_length', 'visualmag_excerpt_length', 999 );

/*
 * Remove parentheses from categories widget
 */
function visualmagcategories_postcount_filter ($variable) {
   $variable = str_replace('(', '<span class="post-count"> ', $variable);
   $variable = str_replace(')', ' </span>', $variable);
   return $variable;
}
add_filter('wp_list_categories','visualmagcategories_postcount_filter');

/**
 * Customize excerpt more.
 */
if ( ! function_exists( 'visualmag_excerpt_more' ) ) :

function visualmag_excerpt_more( $more ) {
    if ( is_admin() ) {
        return $more;
    } else {
        return '... ';
    }
}
add_filter( 'excerpt_more', 'visualmag_excerpt_more' );

endif;

/**
 * Customize archive title.
 */
add_filter('get_the_archive_title', function ($title) {
    if (is_category()) {
        $title = single_cat_title('', false);
    } elseif (is_tag()) {
        $title = single_tag_title('', false);
    } elseif (is_author()) {
        $title = '<span class="vcard">' . get_the_author() . '</span>';
    } elseif (is_tax()) { //for custom post types
        $title = single_term_title('', false);
    } elseif (is_post_type_archive()) {
        $title = post_type_archive_title('', false);
    }
    return $title;
});

/**
 * Set the default image if none exists.
 *
 * @param string $html              The post thumbnail HTML.
 * @param int    $post_id           The post ID.
 * @param int    $post_thumbnail_id The post thumbnail ID.
 * @return html
 */
function visualmag_fallback_post_thumbnail_html( $html, $post_id, $post_thumbnail_id ) {
    if ( empty( $html ) ) {
        $html = '<img class="default-thumbnail" src="' . get_template_directory_uri() . '/assets/images/featured_default.png' . '" alt="' . get_the_title( $post_id ) . '" />';
    }

    return $html;
}
add_filter( 'post_thumbnail_html', 'visualmag_fallback_post_thumbnail_html', 5, 3 );

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

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

            <div class="visualmag-pro-header">
                <span class="dashicons dashicons-star-filled"></span>
                <strong class="visualmag-pro-title">
                    <?php esc_html_e( 'Upgrade to VisualMag Pro', 'visualmag' ); ?>
                </strong>
            </div>
            <p class="visualmag-pro-desc">
                <?php esc_html_e( 'You are currently using the free version of the VisualMag theme. Upgrade to the Pro version to unlock additional features:', 'visualmag' ); ?>
            </p>
            <ul class="visualmag-pro-features">
                <li><?php esc_html_e( 'Theme Options Panel', 'visualmag' ); ?></li>
                <li><?php esc_html_e( 'One Click Demo Import', 'visualmag' ); ?></li>
                <li><?php esc_html_e( '1000+ Google Fonts', 'visualmag' ); ?></li>
                <li><?php esc_html_e( '17+ Block Patterns', 'visualmag' ); ?></li>
                <li><?php esc_html_e( 'Single Post Options', 'visualmag' ); ?></li>
                <li><?php esc_html_e( 'Priority Theme Support and More', 'visualmag' ); ?></li>                
            </ul>

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

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

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

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

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

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

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

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

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

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

        .visualmag-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 visualmagBorderPulse {
            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); }
        }
        '
    );
});
