<?php
/**
 * Pinterest Follow Us Block functionality
 *
 * @link       https://pinnerpress.com
 * @since      0.1.0
 *
 * @package    PinnerPress
 * @subpackage PinnerPress/includes
 */

if (!defined('WPINC')) {
    die;
}

/**
 * Register the Pinterest Follow Us block
 */
function pinnerpress_register_follow_block() {
    // This is a premium feature
    if (!pinnerpress_is_premium()) {
        return;
    }

    // Check if Gutenberg is available
    if (!function_exists('register_block_type')) {
        return;
    }

    // Register the block script
    wp_register_script(
        'pinnerpress-follow-block',
        plugin_dir_url(dirname(__FILE__)) . 'admin/js/pinnerpress-follow-block.js',
        array('wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n'),
        PINNERPRESS_VERSION,
        true
    );
    
    // Localize script with default settings
    wp_localize_script('pinnerpress-follow-block', 'pinnerpressFollowDefaults', array(
        'pinterestUrl' => get_option('pinnerpress_pinterest_url', ''),
        'buttonText' => get_option('pinnerpress_follow_button_default_text', 'Follow on Pinterest'),
        'buttonColor' => get_option('pinnerpress_follow_button_default_color', '#E60023'),
        'textColor' => get_option('pinnerpress_follow_button_default_text_color', '#ffffff')
    ));

    // Register the block editor style
    wp_register_style(
        'pinnerpress-follow-block-editor',
        plugin_dir_url(dirname(__FILE__)) . 'admin/css/pinnerpress-follow-block-editor.css',
        array('wp-edit-blocks'),
        PINNERPRESS_VERSION
    );

    // Register the block frontend style (moved to public/css)
    wp_register_style(
        'pinnerpress-follow-block',
        plugin_dir_url(dirname(__FILE__)) . 'public/css/pinnerpress-follow-block.css',
        array(),
        PINNERPRESS_VERSION
    );

    // Register the block
    register_block_type('pinnerpress/follow-button', array(
        'editor_script' => 'pinnerpress-follow-block',
        'editor_style' => 'pinnerpress-follow-block-editor',
        'style' => 'pinnerpress-follow-block',
        'render_callback' => 'pinnerpress_render_follow_block',
        'attributes' => array(
            'pinterestUrl' => array(
                'type' => 'string',
                'default' => ''
            ),
            'buttonText' => array(
                'type' => 'string',
                'default' => ''
            ),
            'buttonColor' => array(
                'type' => 'string',
                'default' => ''
            ),
            'textColor' => array(
                'type' => 'string',
                'default' => ''
            ),
            'buttonSize' => array(
                'type' => 'string',
                'default' => 'medium'
            ),
            'showIcon' => array(
                'type' => 'boolean',
                'default' => true
            ),
            'alignment' => array(
                'type' => 'string',
                'default' => 'left'
            ),
            'openInNewTab' => array(
                'type' => 'boolean',
                'default' => true
            )
        )
    ));
}

/**
 * Render the Pinterest Follow Us block on the frontend
 */
function pinnerpress_render_follow_block($attributes) {
    // Get defaults from settings
    $default_pinterest_url = get_option('pinnerpress_pinterest_url', '');
    $default_button_text = get_option('pinnerpress_follow_button_default_text', 'Follow on Pinterest');
    $default_button_color = get_option('pinnerpress_follow_button_default_color', '#E60023');
    $default_text_color = get_option('pinnerpress_follow_button_default_text_color', '#ffffff');
    
    // Use block attributes or fall back to defaults
    $pinterest_url = !empty($attributes['pinterestUrl']) ? esc_url($attributes['pinterestUrl']) : $default_pinterest_url;
    $button_text = !empty($attributes['buttonText']) ? esc_html($attributes['buttonText']) : $default_button_text;
    $button_color = !empty($attributes['buttonColor']) ? esc_attr($attributes['buttonColor']) : $default_button_color;
    $text_color = !empty($attributes['textColor']) ? esc_attr($attributes['textColor']) : $default_text_color;
    $button_size = !empty($attributes['buttonSize']) ? esc_attr($attributes['buttonSize']) : 'medium';
    $show_icon = isset($attributes['showIcon']) ? $attributes['showIcon'] : true;
    $alignment = !empty($attributes['alignment']) ? esc_attr($attributes['alignment']) : 'left';
    $open_in_new_tab = isset($attributes['openInNewTab']) ? $attributes['openInNewTab'] : true;

    // If no Pinterest URL is provided, don't render anything
    if (empty($pinterest_url)) {
        return '';
    }

    // Ensure the URL is properly formatted
    if (!preg_match('~^https?://~i', $pinterest_url)) {
        $pinterest_url = 'https://' . $pinterest_url;
    }
    
    // Target attribute
    $target = $open_in_new_tab ? ' target="_blank" rel="noopener noreferrer"' : '';
    
    // Size classes
    $size_class = 'pinnerpress-follow-btn-' . $button_size;
    
    // Alignment classes
    $alignment_class = 'pinnerpress-follow-align-' . $alignment;
    
    // Pinterest icon SVG
    $pinterest_icon = '';
    if ($show_icon) {
        $pinterest_icon = '<svg class="pinnerpress-follow-icon" width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
            <path d="M12 0C5.373 0 0 5.372 0 12c0 5.084 3.163 9.426 7.627 11.174-.105-.949-.2-2.405.042-3.441.219-.937 1.407-5.965 1.407-5.965s-.359-.719-.359-1.782c0-1.668.967-2.914 2.171-2.914 1.023 0 1.518.769 1.518 1.69 0 1.029-.655 2.568-.994 3.995-.283 1.194.599 2.169 1.777 2.169 2.133 0 3.772-2.249 3.772-5.495 0-2.873-2.064-4.882-5.012-4.882-3.414 0-5.418 2.561-5.418 5.207 0 1.031.397 2.138.893 2.738.098.119.112.224.083.345l-.333 1.36c-.053.22-.174.267-.402.161-1.499-.698-2.436-2.888-2.436-4.649 0-3.785 2.75-7.262 7.929-7.262 4.163 0 7.398 2.967 7.398 6.931 0 4.136-2.607 7.464-6.227 7.464-1.216 0-2.357-.631-2.75-1.378l-.748 2.853c-.271 1.043-1.002 2.35-1.492 3.146C9.57 23.812 10.763 24 12.001 24c6.624 0 11.999-5.373 11.999-12C24 5.372 18.626.001 12.001.001z"/>
        </svg>';
    }

    // Build the button HTML
    $button_html = sprintf(
        '<div class="pinnerpress-follow-button-wrapper %s">
            <a href="%s" class="pinnerpress-follow-button %s" style="background-color: %s; color: %s;" data-pin-do="buttonFollow"%s>
                %s
                <span class="pinnerpress-follow-text">%s</span>
            </a>
        </div>',
        $alignment_class,
        esc_url($pinterest_url),
        $size_class,
        $button_color,
        $text_color,
        $target,
        $pinterest_icon,
        $button_text
    );

    return $button_html;
}

/**
 * Hook to register the block
 */
add_action('init', 'pinnerpress_register_follow_block');

/**
 * Add block category for PinnerPress blocks
 */
function pinnerpress_add_block_category($categories) {
    return array_merge(
        $categories,
        array(
            array(
                'slug' => 'pinnerpress',
                'title' => __('PinnerPress', 'pinnerpress'),
                'icon' => 'pinterest',
            ),
        )
    );
}

// Add the block category
if (version_compare(get_bloginfo('version'), '5.8', '>=')) {
    add_filter('block_categories_all', 'pinnerpress_add_block_category');
} else {
    add_filter('block_categories', 'pinnerpress_add_block_category');
} 