<?php

namespace App;

// Register custom blocks
if (function_exists('acf_register_block_type')) {
  add_action('init', function () {
    $product_plural_name = get_field('product_plural_name', 'options') ?: 'Products';
    $service_plural_name = get_field('service_plural_name', 'options') ?: 'services';
    $job_plural_name = get_field('job_plural_name', 'options') ?: 'jobs';


    acf_register_block_type(array(
      'name'              => 'slider',
      'title'             => __('Slider', 'muy-lindo'),
      'description'       => __('Slider of images', 'muy-lindo'),
      'render_callback'   => 'App\\muy_blocks_callback',
      'category'          => 'muy-blocks',
      'icon'              => 'leftright',
      'keywords'          => array('slider', 'image', 'custom'),
      'supports'          => array(
        'align' => array(
          'left',
          'right',
          'full',
          'wide'
        ),
      ),
    ));

    acf_register_block_type(array(
      'name'              => 'partners',
      'title'             => __('Partners', 'muy-lindo'),
      'description'       => __('carosello logo partners', 'muy-lindo'),
      'render_callback'   => 'App\\muy_blocks_callback',
      'category'          => 'muy-blocks',
      'icon'              => 'leftright',
      'keywords'          => array('partners', 'image', 'custom'),
      'supports'          => array(
        'align' => array(
          'left',
          'right',
          'full',
          'wide'
        ),
        'mode' => false,
      ),
    ));

    acf_register_block_type(array(
      'name'              => 'posts_list',
      'title'             => __('Posts List', 'muy-lindo'),
      'description'       => __('Select posts to show', 'muy-lindo'),
      'render_callback'   => 'App\\muy_blocks_callback',
      'category'          => 'muy-blocks',
      'icon'              => 'list-view',
      'keywords'          => array('posts', 'image', 'custom'),
      'supports'          => array(
        'align' => array(
          'left',
          'right',
          'full',
          'wide'
        ),
      ),
    ));

    acf_register_block_type(array(
      'name'              => 'animations',
      'title'             => __('Animations', 'muy-lindo'),
      'description'       => __('Animations block', 'muy-lindo'),
      'render_callback'   => 'App\\muy_blocks_callback',
      'category'          => 'muy-blocks',
      'icon'              => 'controls-forward',
      'keywords'          => array('animate', 'animations', 'custom'),
      'supports'          => array(
        'align' => array(
          'left',
          'right',
          'full',
          'wide'
        ),
        'mode' => false,
        '__experimental_jsx' => true,
      ),
    ));

    acf_register_block_type(array(
      'name'              => 'carousel',
      'title'             => __('Carousel', 'muy-lindo'),
      'description'       => __('Carousel', 'muy-lindo'),
      'render_callback'   => 'App\\muy_blocks_callback',
      'category'          => 'muy-blocks',
      'icon'              => 'editor-insertmore',
      'keywords'          => array('slider', 'carousel', 'custom'),
      'supports'          => array(
        'align' => array(
          'left',
          'right',
          'full',
          'wide'
        ),
        '__experimental_jsx' => true,
      ),
    ));

    acf_register_block_type(array(
      'name'              => 'overlay',
      'title'             => __('Overlay', 'muy-lindo'),
      'description'       => __('Overlay', 'muy-lindo'),
      'render_callback'   => 'App\\muy_blocks_callback',
      'category'          => 'muy-blocks',
      'icon'              => 'editor-insertmore',
      'keywords'          => array('overlay', 'custom'),
      'supports'          => array(
        'align' => array(
          'left',
          'right',
          'full',
          'wide'
        ),
        'mode'               => false,
        '__experimental_jsx' => true,
        'multiple'           => false
      ),
    ));

    acf_register_block_type(array(
      'name'              => 'slide',
      'title'             => __('slide', 'muy-lindo'),
      'description'       => __('slide', 'muy-lindo'),
      'render_callback'   => 'App\\muy_blocks_callback',
      'category'          => 'muy-blocks',
      'icon'              => 'editor-insertmore',
      'keywords'          => array('slide', 'custom'),
      'parent'            => array('acf/carousel'),
      'supports'          => array(
        'align' => array(
          'left',
          'right',
          'full',
          'wide'
        ),
        'mode' => false,
        '__experimental_jsx' => true,
      ),
    ));

    acf_register_block_type(array(
      'name'              => 'file',
      'title'             => __('File Custom', 'muy-lindo'),
      'description'       => __('File block custom', 'muy-lindo'),
      'render_callback'   => 'App\\muy_blocks_callback',
      'category'          => 'muy-blocks',
      'icon'              => 'media-document',
      'keywords'          => array('file', 'custom'),
      'supports'          => array(
        'align' => array(
          'left',
          'right',
          'full',
          'wide'
        ),
      ),
    ));

    if (MUY_ALLOW_PRODUCTS) {
      acf_register_block_type(array(
        'name'              => 'products_list',
        'title'             => sprintf(__('%s List', 'muy-lindo'), $product_plural_name),
        'description'       => sprintf(__('Select %s to show', 'muy-lindo'), $product_plural_name),
        'render_callback'   => 'App\\muy_blocks_callback',
        'category'          => 'muy-blocks',
        'icon'              => 'tag',
        'keywords'          => array($product_plural_name, 'image', 'custom'),
        'supports'          => array(
          'align' => array(
            'left',
            'right',
            'full',
            'wide'
          ),
        ),
      ));
    }
    if (MUY_ALLOW_SERVICES) {
      acf_register_block_type(array(
        'name'              => 'services_list',
        'title'             => sprintf(__('%s List', 'muy-lindo'), $service_plural_name),
        'description'       => sprintf(__('Select %s to show', 'muy-lindo'), $service_plural_name),
        'render_callback'   => 'App\\muy_blocks_callback',
        'category'          => 'muy-blocks',
        'icon'              => 'clipboard',
        'keywords'          => array($service_plural_name, 'image', 'custom'),
        'supports'          => array(
          'align' => array(
            'left',
            'right',
            'full',
            'wide'
          ),
        ),
      ));
    }

    if (MUY_ALLOW_JOBS) {
      acf_register_block_type(array(
        'name'              => 'jobs_list',
        'title'             => sprintf(__('%s List', 'muy-lindo'), $job_plural_name),
        'description'       => sprintf(__('Select %s to show', 'muy-lindo'), $job_plural_name),
        'render_callback'   => 'App\\muy_blocks_callback',
        'category'          => 'muy-blocks',
        'icon'              => 'clipboard',
        'keywords'          => array($job_plural_name, 'image', 'custom'),
        'supports'          => array(
          'align' => array(
            'left',
            'right',
            'full',
            'wide'
          ),
        ),
      ));
    }

    acf_register_block_type(array(
      'name'              => 'accordion',
      'title'             => __('Accordion', 'muy-lindo'),
      'description'       => __('Accordion', 'muy-lindo'),
      'render_callback'   => 'App\\muy_blocks_callback',
      'category'          => 'muy-blocks',
      'icon'              => 'editor-insertmore',
      'keywords'          => array('slider', 'image', 'custom'),
      'supports'          => array(
        'align' => array(
          'left',
          'right',
          'full',
          'wide'
        ),
        'mode' => false,
        '__experimental_jsx' => true,
      ),
    ));

    acf_register_block_type(array(
      'name'              => 'map',
      'title'             => __('Map', 'muy-lindo'),
      'description'       => __('Group of Google Maps', 'muy-lindo'),
      'render_callback'   => 'App\\muy_blocks_callback',
      'category'          => 'muy-blocks',
      'icon'              => 'location-alt',
      'mode'              => 'false',
      'keywords'          => array('map', 'google', 'contact', 'location', 'geo'),
      'supports'          => array(
        'align' => array(
          'left',
          'right',
          'full',
          'wide'
        ),
      ),
    ));

    if (function_exists('tribe_get_events')) {
      acf_register_block_type(array(
        'name'              => 'events',
        'title'             => __('events', 'muy-lindo'),
        'description'       => __('blocco eventi', 'muy-lindo'),
        'render_callback'   => 'App\\muy_blocks_callback',
        'category'          => 'muy-blocks',
        'icon'              => 'tickets-alt',
        'keywords'          => array('Eventi', 'image', 'custom'),
        'supports'          => array(
          'align' => array(
            'left',
            'right',
            'full',
            'wide'
          ),
        ),
      ));
    }

  });
}

// function muy_blocks_callback($block)
// {
//   $slug = str_replace('acf/', '', $block['name']);
//   $block['slug'] = $slug;
//   $block['classes'] = implode(' ', [$block['slug'], $block['className'], 'align' . $block['align']]);
//   $template = null;
//   $data = collect(get_body_class())->reduce(function ($data, $class) use ($template) {
//     return apply_filters("sage/template/{$class}/data", $data, $template);
//   }, []);

//   $data['block'] = $block;
//   echo \App\template("blocks/${slug}", $data);
// }

function muy_blocks_callback($block)
{
  $slug = str_replace('acf/', '', $block['name']);
  $block['slug'] = $slug;

  // Verifica se 'className' è definito, altrimenti imposta una stringa vuota
  $class_name = isset($block['className']) ? $block['className'] : '';

  $block['classes'] = implode(' ', [$block['slug'], $class_name, 'align' . $block['align']]);
  $template = null;
  $data = collect(get_body_class())->reduce(function ($data, $class) use ($template) {
    return apply_filters("sage/template/{$class}/data", $data, $template);
  }, []);

  $data['block'] = $block;
  echo \App\template("blocks/${slug}", $data);
}

//Create custom Gutenberg category
add_filter('block_categories', function ($categories, $post) {
  return array_merge(
    array(
      array(
        'slug' => 'muy-blocks',
        'title' => __('Muy Lindo Blocks', 'muy-lindo'),
      ),
    ),
    $categories
  );
}, 10, 2);

// add_filter( 'allowed_block_types', function ( $allowed_blocks, $post ) {  
//   $allowed_blocks = array(
// 		'core/image',
// 		'core/paragraph',
// 		'core/heading',
// 		'core/list'
// 	);

//   return $allowed_blocks;
// }, 10, 2);

