<?php

namespace Rtc\PlantShopWC\Frontend\Controllers;

use Automattic\WooCommerce\StoreApi\Schemas\V1\CartItemSchema;
use Rtc\PlantShopWC\Admin\Models\GeneralSettingsModel;
use Rtc\PlantShopWC\Admin\Modules\BotanicalName;
use Rtc\PlantShopWC\Frontend\Models\CartModel;
use Rtc\PlantShopWC\Frontend\Views\CartView;
use WC_Product;

class CartBlocksController {


    protected $model;
    protected $view;

    protected $botanical_name_checked;

    public function __construct() {
        $this->model = new CartModel();
        $this->view  = new CartView();

        $opts                         = get_option( GeneralSettingsModel::get_option_key(), array() );
        $botanical_name_checked       = isset( $opts['botanical_name_replacement'] ) ? (string) $opts['botanical_name_replacement'] : '';
        $this->botanical_name_checked = ( $botanical_name_checked === '1' );
    }

    public function register_hooks(): void {

        add_action( 'enqueue_block_assets', array( $this, 'enqueue_blocks_cart_script' ) );

        // Change the Product Title to Botanical Name if enabled
        // Also runs on checkout
        if ( $this->botanical_name_checked ) {

            add_action( 'enqueue_block_assets', array( $this, 'enqueue_additional_block_scripts' ) );
            add_action( 'init', array( $this, 'register_custom_endpoint_data' ), 20 );

        }
    }

    public function enqueue_blocks_cart_script(): void {

        if ( ! function_exists( 'is_cart' ) || ! is_cart() ) {
            return;
        }

        $cart = WC()->cart;

        $contents = $cart->get_cart();
        if ( empty( $contents ) ) {
            return;
        }

        $neighbors = $this->model->get_neighbors( $contents, $this->botanical_name_checked );
        if ( empty( $neighbors ) ) {
            return;
        }

        $html = $this->view->render_neighbors( $neighbors );

        wp_register_script(
            'plantshop-wc-cart-block-enhance',
            PLANTSHOP_WC_PUBLIC_URL . 'frontend/js/cart-block-enhance.js',
            array(),
            defined( 'PLANTSHOP_WC_PLUGIN_VERSION' ) ? PLANTSHOP_WC_PLUGIN_VERSION : null,
            true
        );

        wp_localize_script(
            'plantshop-wc-cart-block-enhance',
            'plantshop_wc_cart_info',
            array(
                'html' => $html,
            )
        );

        wp_enqueue_script( 'plantshop-wc-cart-block-enhance' );

        wp_enqueue_style( 'plantshop-wc-cart', PLANTSHOP_WC_PUBLIC_URL . 'frontend/css/cart.css', array(), PLANTSHOP_WC_PLUGIN_VERSION );
    }

    public function enqueue_additional_block_scripts() {

        if ( is_admin() ) {
            return; }

        wp_enqueue_script(
            'plantshop-wc-checkout-filters',
            PLANTSHOP_WC_PUBLIC_URL . 'frontend/js/product-title-block-enhance.js',
            array(),
            defined( 'PLANTSHOP_WC_PLUGIN_VERSION' ) ? PLANTSHOP_WC_PLUGIN_VERSION : null,
            true
        );
    }

    public function register_custom_endpoint_data() {

        if ( ! function_exists( 'woocommerce_store_api_register_endpoint_data' ) ) {
            return;
        }

        woocommerce_store_api_register_endpoint_data(
            array(
                'endpoint'        => CartItemSchema::IDENTIFIER,
                'namespace'       => 'plantshop',
                'data_callback'   => function ( $cart_item ) {
                    $product = $cart_item['data'] ?? null;
                    if ( ! $product instanceof WC_Product || ! GeneralSettingsModel::is_product_included( $product ) ) {
                        return array();
                    }
                    $orig = $product->get_name();
                    return array(
                        'displayTitle' => array(
                            'html' => ( new BotanicalName() )->build( $product, $orig, true ),
                        ),
                    );
                },
                'schema_callback' => function () {
                    return array(
                        'properties' => array(
                            'displayTitle' => array(
                                'type'       => 'object',
                                'properties' => array(
                                    'html' => array( 'type' => 'string' ),
                                ),
                            ),
                        ),
                    );
                },
            )
        );
    }
}
