<?php
/**
 *
 * @package productive-commerce
 */

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


function productive_commerce_register_blocks_init_action_wishlist() {
    productive_commerce_register_block_init_wishlist();
}
add_action( 'init', 'productive_commerce_register_blocks_init_action_wishlist' );


function productive_commerce_register_block_init_wishlist() {
    
    global $productive_commerce_plugin_version;
    
    $asset_file = include( PRODUCTIVE_COMMERCE_PLUGIN_PATH . 'standard/gutenberg/blocks/wishlist-page/build/index.asset.php');
    
    wp_register_script(
        'productive-commerce-wishlist-page-script',
        PRODUCTIVE_COMMERCE_PLUGIN_URI . 'standard/gutenberg/blocks/wishlist-page/build/index.js',
        $asset_file['dependencies'],
        $asset_file['version']
    );
    
    wp_register_style(
        'productive-commerce-wishlist-page-style',
        PRODUCTIVE_COMMERCE_PLUGIN_URI . 'standard/gutenberg/blocks/wishlist-page/build/style-index.css',
        array(),
        $productive_commerce_plugin_version
    );
    
    $block_metadata = PRODUCTIVE_COMMERCE_PLUGIN_PATH . 'standard/gutenberg/blocks/wishlist-page/build';
    $args = array(
        'api_version' => 3,
        'version' => $productive_commerce_plugin_version,
        'render_callback' => 'productive_commerce_register_block_render_callback_wishlist',
        'editor_script' => 'productive-commerce-wishlist-page-script',
        'style' => 'productive-commerce-wishlist-page-style',
    );
    register_block_type( $block_metadata, $args );
    
}
function productive_commerce_register_block_render_callback_wishlist( $attributes, $content ) {
    
    foreach ($attributes as $key => $attribute) {
        if( 'align' == $key && 'full' == $attribute ) {
            $attributes[$key] = 'alignfull';
        } else if( 'align' == $key && 'wide' == $attribute ) {
            $attributes[$key] = 'alignwide';
        }
    }
    
    $section_gtbg_align = '';
    if( isset($attributes['align']) ) {
        $section_gtbg_align = $attributes['align'];
    }
    
    $cpt_section_args = array(
        'section_gtbg_align'                                    => $section_gtbg_align,
    );
    
    
    $section_gtbg_align_none = 'align_page_none';
    $attributes['section_bg_color_scheme'] = 'section_with_themed_bg';
    $attributes['section_block_max_width'] = 'siteMaxWidth_Std';
    $attributes['section_block_spacing'] = 'section_spacing_none';
    $attributes['section_content_media_item_shape'] = 'shapeable-image-default';
    
    $section_bg_color_scheme        = 'productive-paddable-section page_main_section_container ' . $section_gtbg_align_none 
            . ' ' . $attributes['section_bg_color_scheme'] . ' ' . $attributes['section_block_max_width'] . ' ' . $attributes['section_block_spacing'] . ' ' . $attributes['section_content_media_item_shape'];
    
    $section_content_content_box_shape = '';
    if( isset($attributes['section_content_content_box_shape']) ) {
        $section_content_content_box_shape = $attributes['section_content_content_box_shape'];
    }
    $section_content_icon_size = '';
    if( isset($attributes['section_content_icon_size']) ) {
        $section_content_icon_size = $attributes['section_content_icon_size'];
    }
    $section_content_icon_alignment_h = '';
    if( isset($attributes['section_content_icon_alignment_h']) ) {
        $section_content_icon_alignment_h = $attributes['section_content_icon_alignment_h'];
    }
    $section_content_content_box_alignment_h = '';
    if( isset($attributes['section_content_content_box_alignment_h']) ) {
        $section_content_content_box_alignment_h = $attributes['section_content_content_box_alignment_h'];
    }
    $section_content_show_url_button_shape = '';
    if( isset($attributes['section_content_show_url_button_shape']) ) {
        $section_content_show_url_button_shape = $attributes['section_content_show_url_button_shape'];
    }
    $section_content_show_url_button_width = '';
    if( isset($attributes['section_content_show_url_button_width']) ) {
        $section_content_show_url_button_width = $attributes['section_content_show_url_button_width'];
    }
    $section_content_items_css_classes = ' ' . $section_content_content_box_shape . ' ' . $section_content_icon_size
            . ' ' . $section_content_icon_alignment_h . ' ' . $section_content_content_box_alignment_h . ' ' . $section_content_show_url_button_shape . ' ' . $section_content_show_url_button_width;
    
    ob_start();
    
    do_action( 'productive_global_render_content_wrapper_full_full_top', $section_bg_color_scheme );
        do_action( 'productive_global_render_content_wrapper_full_top', $section_content_items_css_classes );
            productive_commerce_render_user_wishlist( $cpt_section_args );
        do_action('productive_global_render_content_wrapper_full_bottom');
    do_action('productive_global_render_content_wrapper_full_full_bottom');
    
    $content_to_render = ob_get_clean();
    
    return $content_to_render;
}
