<?php
/** Winners Table Block
 *
 * @package Bw-Winners-Network
 */

namespace BwWinnersNetwork;

if ( ! class_exists( __NAMESPACE__ . '\Winners_Table' ) ) {

	/**
	 * Register Winners Table Block
	 */
	class Winners_Table {

		/**
		 * Adds hooks.
		 */
		public function __construct() {
			add_action( 'init', [ $this, 'register_assets' ] );
		}

		/**
		 * Registers styles and scripts.
		 */
		public function register_assets() {

			// Winners Table Block

			$test = register_block_type(
				plugin_dir_path( PLUGIN_PATH ) . 'build/blocks/winners-table',
				[
					'render_callback' => [ $this, 'block_render' ],
				]
			);


			// Winners Table Block View


			$asset_file = plugin_dir_path( PLUGIN_PATH ) . 'build/blocks/winners-table/view/index.asset.php';
			
			if ( ! file_exists( $asset_file ) ) {
				error_log( 'Plugin assets not built: ' . $asset_file );
				return;
			}

			$settings = include $asset_file;

			wp_register_script(
				'bw-winner-table',
				plugins_url( 'build/blocks/winners-table/view/index.js', PLUGIN_PATH ),
				$settings['dependencies'],
				$settings['version'],
				[
					'in_footer' => true
				]
			);
			
			wp_register_style(
				'bw-winner-table',
				plugins_url( 'build/blocks/winners-table/view/index.css', PLUGIN_PATH ),
				[],
				$settings['version']
			);
		}

		function block_render ( $block_attributes, $content ) {
			if ( ! isset( $block_attributes['blockId'] ) ) return '';
			$id = str_replace( '-', '', $block_attributes['blockId'] );


			$post_id = get_the_ID();
			$block_attributes['post_id'] = $post_id;

			if ( ! empty( $block_attributes['filterCurrentPost'] ) ) {

				if ( is_plugin_active( 'bw-winners-global-site/bw-winners-global-site.php' ) ) {

					global $bw_winners_global_site;

					$post_type = get_post_type( $post_id );

					switch ($post_type) {
						case 'bw-product':
							if ( $product = $bw_winners_global_site->entities->get_product( $post_id ) ) {
								$brand = $bw_winners_global_site->entities->get_brand( $product['brand_id'] );
							}
							break;
						case 'brand':
							$brand = $bw_winners_global_site->entities->get_brand( $post_id );
							break;						
						default:
							break;
					}


					if ( ! isset( $block_attributes['args'] ) ) {
						$block_attributes['args'] = [];
					}

					if ( ! isset( $block_attributes['args']['filters'] ) ) {
						$block_attributes['args']['filters'] = [];
					}

					if ( ! empty( $product ) ) {
						// $block_attributes['args']['filters']['product_name'] = [ $product['name'] ];
						$block_attributes['args']['filters']['product_id'] = $product['id'];
					}

					if ( ! empty( $brand ) ) {
						// $block_attributes['args']['filters']['brand_name'] = [ $brand['name'] ];
						$block_attributes['args']['filters']['brand_id'] = $brand['id'];
					}
				}
			}

			wp_enqueue_script( 'bw-winner-table' );
			wp_enqueue_style( 'bw-winner-table' );
			wp_localize_script( 'bw-winner-table', 'bw_block_' . $id, $block_attributes );
			return sprintf('<div id="%s" class="bw-winner-winners-table-block"></div>', $id );

		}
	}
}
