<?php

namespace HulkPlugins\Woo\ProductConditionManager\Controllers;

use HulkPlugins\Woo\ProductConditionManager\Models\SettingsModel;
use HulkPlugins\Woo\ProductConditionManager\Services\FrontendService;
use HulkPlugins\Woo\ProductConditionManager\Traits\SingletonTrait;
use WC_Product;
use WP_Block;
use WP_Post;
use const HulkPlugins\Woo\ProductConditionManager\PLUGIN_DIR_PATH;
use const HulkPlugins\Woo\ProductConditionManager\VERSION;

defined( 'ABSPATH' ) || exit;

class FrontendController {
	use SingletonTrait;

	private array $cache = [];
	private SettingsModel $settingsModel;
	private FrontendService $service;

	public function __construct() {
		$this->settingsModel = SettingsModel::getInstance();
		$this->service = FrontendService::getInstance();
	}

	public function init_hooks(): void {
		add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
		add_filter( 'render_block_woocommerce/product-button', [ $this, 'render_block_product_button' ], 10, 3 );
		add_action( 'woocommerce_after_shop_loop_item', [ $this, 'render_classic_product_button' ] );
		add_action( 'woocommerce_product_meta_end', [ $this, 'product_meta_end' ] );
		add_filter( 'woocommerce_structured_data_product_offer', [ $this, 'structured_data_product_offer' ], 10, 2 );
	}

	/**
	 * Frontend scripts
	 * @return void
	 */
	public function enqueue_scripts(): void {
		if (
			did_filter( 'render_block_woocommerce/product-button' ) ||
			did_action( 'woocommerce_after_shop_loop_item' ) ||
			did_action( 'woocommerce_product_meta_end' )
		) {
			wp_register_style( 'hulk_woo_pcm_frontend', '', [], VERSION );
			$settings = $this->settingsModel->getSettings();
			if ( ! empty( $settings['productConditionCustomCss'] ) ) {
				wp_add_inline_style( 'hulk_woo_pcm_frontend', esc_html( $settings['productConditionCustomCss'] ) );
			}
		}
	}

	/**
	 * Helper function to enqueue the style
	 * @return void
	 */
	protected function enqueue_cache_style(): void {
		if ( isset( $this->cache['enqueue_style'] ) ) {
			return;
		}

        wp_enqueue_style( 'hulk_woo_pcm_frontend' );
		$this->cache['enqueue_style'] = true;
	}

	/**
	 * Product condition labels added to items on the block shop page.
	 *
	 * @param $block_content
	 * @param $parsed_block
	 * @param WP_Block $block
	 *
	 * @return mixed|string
	 */
	public function render_block_product_button( $block_content, $parsed_block, WP_Block $block ) {

		$settings = $this->settingsModel->getSettings();

		// Show/Hide based on global setting
		if ( empty( $settings['showOnShopPage'] ) ) {
			return $block_content;
		}

		// Hide classic render markup from block shop page.
		remove_action( 'woocommerce_after_shop_loop_item', [ $this, 'render_classic_product_button' ] );

		// Enqueue style
		$this->enqueue_cache_style();

		$post_id = $block->context['postId'] ?? '';
		$isShopOrTaxonomy = true;
		$class = 'hulk_woo_pcm_shop';
		$type = 'block';
		ob_start();
		require PLUGIN_DIR_PATH . 'includes/views/frontend/product-condition-view.php';
		$prependContent = ob_get_clean();
		return $prependContent . $block_content;
	}

	/**
	 * Product condition labels added to items on the classic shop page.
	 * @return void
	 */
	public function render_classic_product_button(): void {
		global $post;
		/** @var WP_Post $post */
		$post_id = $post->ID;

		$settings = $this->settingsModel->getSettings();

		// Show/Hide based on global setting
		if ( empty( $settings['showOnShopPage'] ) ) {
			return;
		}

		// Enqueue style
		$this->enqueue_cache_style();

		$isShopOrTaxonomy = true;
		$class = 'hulk_woo_pcm_shop';
		$type = 'classic';
		require PLUGIN_DIR_PATH . 'includes/views/frontend/product-condition-view.php';
	}

	/**
	 * Product condition displayed on the single product page.
	 * @return void
	 */
	public function product_meta_end(): void {
		global $post;
		/** @var WP_Post $post */
		$post_id = $post->ID;

		// Enqueue style
		$this->enqueue_cache_style();

		$isShopOrTaxonomy = false;
		$class   = 'hulk_woo_pcm_single_product';
		$type    = 'single';
		require PLUGIN_DIR_PATH . 'includes/views/frontend/product-condition-view.php';
	}

	/**
	 * Add valid itemCondition in product structured data.
	 * @param $markup_offer
	 * @param $product
	 *
	 * @return mixed
	 */
	public function structured_data_product_offer( $markup_offer, $product ) {

		/** @var WC_Product $product */
		$post_id  = $product->get_id();
		$productCondition = $this->service->getProductCondition( $post_id );

		if ( ! empty( $productCondition['group'] ) ) {
            $markup_offer['itemCondition'] = $productCondition['group'];
		}

		return $markup_offer;
	}
}
