<?php

namespace BwWinner;

class Bw_Winner {
	public function __construct () {
		add_action( 'init', array($this, 'register_assets') );
		add_filter( 'wpmu_drop_tables', array($this, 'site_drop_tables') );
		
		if ( is_multisite() ) {
			add_action( 'network_admin_menu', array( $this, 'network_flush_rewrite' ) );
		}

		require_once PLUGIN_PATH . 'includes/options.php';
		require_once PLUGIN_PATH . 'includes/class-admin-pages.php';
		require_once PLUGIN_PATH . 'includes/class-custom-post-types.php';
		require_once PLUGIN_PATH . 'includes/class-api.php';
		require_once PLUGIN_PATH . 'includes/winners.php';
		require_once PLUGIN_PATH . 'includes/winners-all-sites.php';
		require_once PLUGIN_PATH . 'includes/class-events.php';
		require_once PLUGIN_PATH . 'includes/class-winners-metaboxes.php';
		require_once PLUGIN_PATH . 'includes/database/class-database.php';

		require_once PLUGIN_PATH . 'includes/class-product-update.php';
		require_once PLUGIN_PATH . 'includes/class-product-update-form.php';
		require_once PLUGIN_PATH . 'includes/class-admin-product-list.php';
		require_once PLUGIN_PATH . 'includes/class-product-shortcodes.php';

		require_once PLUGIN_PATH . 'includes/class-brand-update.php';
		require_once PLUGIN_PATH . 'includes/class-brand-update-form.php';
		require_once PLUGIN_PATH . 'includes/class-admin-brand-list.php';
		require_once PLUGIN_PATH . 'includes/class-brand-shortcodes.php';

		require_once PLUGIN_PATH . 'includes/class-entity-manager.php';

		new Database\Database();
		new Admin_Pages();
		new Custom_Post_Types();
		new API\API();
		new Events();
		new Winners_Metaboxes();

		new Product_Update();
		new Product_Update_Form();
		new Admin_Product_List();
		new Product_Shortcodes();

		new Brand_Update();
		new Brand_Update_Form();
		new Admin_Brand_List();
		new Brand_Shortcodes();

		global $bw_winners;
		$bw_winners = new \stdClass();
		$bw_winners->entities = new Entity_Manager();

	}

	public function register_assets () {
		wp_register_script(
			'swiper',
			PLUGIN_URL . 'assets/js/swiper-bundle.min.js',
			array(),
			'11.0.5',
			array(
				'in_footer' => true
			)
		);
		wp_register_style(
			'swiper',
			PLUGIN_URL . 'assets/css/swiper-bundle.min.css',
			array(),
			'11.0.5',
		);

		$asset_settings = include PLUGIN_PATH . 'build/blocks/winners-table/view/index.asset.php';
		wp_register_script(
			'bw-winner-table',
			PLUGIN_URL . 'build/blocks/winners-table/view/index.js',
			$asset_settings['dependencies'],
			$asset_settings['version'],
			array(
				'in_footer' => true
			)
		);
		wp_register_style(
			'bw-winner-table',
			PLUGIN_URL . 'build/blocks/winners-table/view/index.css',
			array(),
			$asset_settings['version']
		);
		$asset_settings = include PLUGIN_PATH . 'build/blocks/winners-table/index.asset.php';
		register_block_type(
			PLUGIN_PATH . 'build/blocks/winners-table',
			array(
				'render_callback' => array( $this, 'block_render_callback' ),
			)
		);
		$asset_settings = include PLUGIN_PATH . 'build/meta-boxes/brand-select/index.asset.php';
		wp_enqueue_script(
			'bw-winner-brand-select',
			PLUGIN_URL . 'build/meta-boxes/brand-select/index.js',
			$asset_settings['dependencies'],
			$asset_settings['version'],
			true
		);
	}

	public function block_render_callback ( $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'] ) ) {

			global $bw_winners;

			$post_type = get_post_type( $post_id );

			switch ($post_type) {
				case 'bw-product':
					if ( $product = $bw_winners->entities->get_product( $post_id ) ) {
						$brand = $bw_winners->entities->get_brand( $product['brand_id'] );
						$company = $bw_winners->entities->get_company( $product['company_id'] );
					}
					break;
				case 'brand':
					if ( $brand = $bw_winners->entities->get_brand( $post_id ) ) {
						$company = $bw_winners->entities->get_company( $brand['company_id'] );
					}
					break;
				case 'company':
					$company = $bw_winners->entities->get_company( $post_id );
					break;
				
				default:
					break;
			}


			if ( ! isset( $block_attributes['args'] ) ) {
				$block_attributes['args'] = array();
			}

			if ( ! isset( $block_attributes['args']['filters'] ) ) {
				$block_attributes['args']['filters'] = array();
			}

			if ( ! empty( $product ) ) {
				$block_attributes['args']['filters']['product_name'] = array( $product['name'] );
			}

			if ( ! empty( $brand ) ) {
				$block_attributes['args']['filters']['brand_name'] = array( $brand['name'] );
			}

			if ( ! empty( $company ) ) {
				$block_attributes['args']['filters']['company_name'] = array( $company['name'] );
			}
		}

		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 );

	}

	public function site_drop_tables ( $tables ) {
		global $wpdb;
		array_unshift(
			$tables,
			$wpdb->prefix . 'bw_winners_entries',
			$wpdb->prefix . 'bw_winners_products',
			$wpdb->prefix . 'bw_winners_brands',
			$wpdb->prefix . 'bw_winners_companies'
		);
		return $tables;
	}

	public function network_flush_rewrite () {
		add_menu_page(
			'Flush Rewrite Rules',
			'Flush Rewrite Rules',
			'manage_network',
			'flush-rewrite-rules',
			function () {
				if (isset($_POST['flush_rules'])) {
					global $wpdb;

					// Get all site IDs in the network
					$sites = $wpdb->get_col("SELECT blog_id FROM {$wpdb->blogs}");

					foreach ($sites as $site_id) {
						switch_to_blog($site_id);

						// Flush rewrite rules for the current site
						flush_rewrite_rules();

						restore_current_blog();
					}

					echo '<div class="updated"><p>Rewrite rules flushed for all sites!</p></div>';
				}

				echo '<form method="post">';
				echo '<input type="hidden" name="flush_rules" value="1">';
				echo '<button type="submit" class="button button-primary">Flush Rewrite Rules</button>';
				echo '</form>';
			}
		);
	}
}
