<?php

namespace WKO\Controllers;

use WKO\Controllers\Gateways\Pix;
use WKO\Controllers\Gateways\Billet;
use WKO\Controllers\Gateways\Credit;
use WKO\Controllers\Orders\EditOrders;
use WKO\Controllers\Orders\KoinOrders;
use WKO\Controllers\Orders\ViewOrders;
use WKO\Controllers\ProductInstallmentRules;
use WKO\Controllers\Woocommerce\CheckoutBanner;
use WKO\Controllers\Woocommerce\CheckoutObserver;
use WKO\Controllers\Checkout\CheckoutFieldManager;
use WKO\Controllers\Checkout\PaymentGatewayManager;
use WKO\Controllers\Gateways\Blocks\Pix as BlocksPix;
use WKO\Controllers\Checkout\BlockCheckoutFieldManager;
use WKO\Controllers\Gateways\Blocks\Billet as BlocksBillet;
use WKO\Controllers\Gateways\Blocks\Credit as BlocksCredit;

/**
 * Name: Woocommerce
 * Intance Woocommerce classes
 * @package Controllers
 * @since 1.0.0
 */
class Woocommerce
{
    public function initialize()
    {
        $this->instance_main_controllers();
        $this->instance_order_controllers();
    }

    public function registerGatewayUpdateTotal()
    {
        if (function_exists('woocommerce_store_api_register_update_callback')) {
            woocommerce_store_api_register_update_callback(
                array(
                    'namespace' => 'wc-koin-gateway-update-total',
                    'callback'  => [$this, 'gatewayUpdateTotal'],
                )
            );
        }
    }

    public function gatewayUpdateTotal($data)
    {
        WC()->session->set('chosen_payment_method', $data['payment_method']);
        WC()->cart->calculate_totals();
    }

    public function loadWooCommerceBlocks(): void
    {
        add_action('woocommerce_blocks_checkout_block_registration', [$this, 'registerCustomWooCommerceBlocks'], 10, 1);
        add_action('woocommerce_blocks_payment_method_type_registration', [$this, 'registerBlockGateway'], 10, 1);
    }

    public function registerPaymentGateways(array $gateways): array
    {
        $gateways[] = Billet::class;
        $gateways[] = Credit::class;
        if (get_woocommerce_currency() === 'BRL') {
            $gateways[] = Pix::class;
        }

        return $gateways;
    }

    public function registerBlockGateway(object $paymentMethodRegistry): void
    {
        $paymentMethodRegistry->register(new BlocksBillet());
        $paymentMethodRegistry->register(new BlocksCredit());
        if (get_woocommerce_currency() === 'BRL') {
            $paymentMethodRegistry->register(new BlocksPix());
        }
    }

    public function registerCustomWooCommerceBlocks($integration_registry): void
    {
        $integration_registry->register(new CheckoutObserver());
        $integration_registry->register(new CheckoutBanner());
    }


    /**
     * Call main controllers classes
     * @since 1.0.0
     * @return void
     */
    private function instance_main_controllers()
    {
        new Webhooks;
        new Status;
        new Emails;
        new CheckoutFieldManager;
        new BlockCheckoutFieldManager;
        new PaymentGatewayManager;
        new ProductInstallmentRules();
    }

    /**
     * Call order controllers classes
     * @since 1.0.0
     * @return void
     */
    private function instance_order_controllers()
    {
        new EditOrders;
        new KoinOrders;
        new ViewOrders;
    }
}
