<?php

declare(strict_types=1);

namespace MyCustomizer\WooCommerce\Connector\Controller;

use MyCustomizer\WooCommerce\Connector\Config\MczrConfig;
use MyCustomizer\WooCommerce\Connector\Libs\MczrSettings;
use Symfony\Component\HttpFoundation\Request;

class MczrSocialController
{
    /** @var Request */
    private $request;
    /** @var MczrSettings */
    private $settings;

    public function __construct()
    {
        $this->request = Request::createFromGlobals();
        $this->settings = new MczrSettings();
    }

    public function init(): void
    {
        \add_action('wp_head', [$this, 'getOpenGraphMetas'], 10, 0);
    }

    private function buildMetas(array $metas): string
    {
        $return = PHP_EOL;
        $return .= '<!-- MCZR socials metas -->' . PHP_EOL;
        foreach ($metas as $name => $content) {
            $return .= '<meta property="' . $name . '" content="' . $content . '" />' . PHP_EOL;
        }

        return $return;
    }

    public function getOpenGraphMetas(): void
    {
        $designId = $this->request->query->get('designId');
        $product = \wc_get_product();

        if (!$product instanceof \WC_Product) {
            return;
        }

        if (!$product->is_type('mczr')) {
            return;
        }

        $productId = ($product->get_id());
        $metas = [
            'og:url' => \get_permalink($productId),
            'og:type' => 'product',
            'og:title' => 'Customizable product',
            'og:description' => 'This is a customizable product',
        ];
        $settings = $this->settings->getAll();
        $apiBaseUrl = MczrConfig::getConfig()['apiBaseUrl'];

        if ($designId) {
            $url = "{$apiBaseUrl}/brands/{$settings['brand']}/designs/$designId/image";
            $metas['og:image'] = $url;
        }
        $builtMetas = $this->buildMetas($metas);
        $allowed_html = [
            'a' => [
                'href' => [],
                'target' => [],
            ],
            'br' => [],
            'button' => [
                'class' => [],
                'id' => [],
                'name' => [],
                'type' => [],
            ],
            'div' => [
                'class' => [],
            ],
            'fieldset' => [],
            'form' => [
                'name' => [],
                'method' => [],
            ],
            'h1' => [],
            'h2' => [],
            'h3' => [],
            'h4' => [],
            'hr' => [],
            'iframe' => [
                'id' => [],
                'class' => [],
                'data-product-id' => [],
                'height' => [],
                'frameBorder' => [],
                'width' => [],
                'src' => [],
            ],
            'img' => [
                'alt' => [],
                'class' => [],
                'src' => [],
                'width' => [],
            ],
            'input' => [
                'id' => [],
                'class' => [],
                'name' => [],
                'type' => [],
                'readonly' => [],
                'value' => [],
            ],
            'label' => [
                'class' => [],
                'for' => [],
            ],
            'li' => [
                'class' => [],
            ],
            'meta' => [
                'content' => [],
                'property' => [],
            ],
            'textarea' => [
                'class' => [],
                'id' => [],
                'help' => [],
                'name' => [],
            ],
            'script' => [],
            'small' => [
                'class' => [],
            ],
            'strong' => [],
            'style' => [],
            'ul' => [
                'class' => [],
            ],
        ];

        echo \wp_kses($builtMetas, $allowed_html);
    }
}
