<?php

declare(strict_types=1);

namespace MyCustomizer\WooCommerce\Connector\Types;

use MyCustomizer\WooCommerce\Connector\Controller\Admin\MczrProductTypeController;
use MyCustomizer\WooCommerce\Connector\Factory\MczrFactory;
use Symfony\Component\HttpFoundation\Request;
use WC_Product;

class WC_Product_Mczr extends WC_Product
{
    private const TYPE_NAME = 'mczr';

    /** @var MczrFactory */
    private $factory;
    /** @var Request */
    private $request;
    /** @var \Twig\Environment */
    private $twig;

    public function __construct($product = null)
    {
        parent::__construct($product);

        $this->factory = new MczrFactory();
        $this->request = Request::createFromGlobals();
        $this->twig = $this->factory->getTwig();
    }

    public function init(): void
    {
        // Register Tab and panel
        \add_filter('woocommerce_product_data_tabs', [$this, 'addMczrTab']);
        \add_action('woocommerce_product_data_panels', [$this, 'addMczrTabContent']);
        // Retrieve used class
        \add_filter('woocommerce_product_class', [$this, 'mczrProductTypeClass'], 10, 2);
        // Process custom fields and save
        \add_action('woocommerce_process_product_meta_' . self::TYPE_NAME, [$this, 'processAndSaveTabMetaField'], 10, 0);
        // Additional Js for admin view
        \add_filter('product_type_selector', [$this, 'registerMczrType']);
    }

    /**
     * @param string|bool $classname
     * @param string $product_type
     *
     * @return string|bool
     */
    public function mczrProductTypeClass($classname, string $product_type)
    {
        if (self::TYPE_NAME == $product_type) {
            $classname = self::class;
        }

        return $classname;
    }

    public function registerMczrType(array $types): array
    {
        $types['mczr'] = __('Kickflip Product');
        \add_filter('product_type_selector', [$this, 'addMczrCustomizableTypeInProductTypeList']);

        return $types;
    }

    public function addMczrCustomizableTypeInProductTypeList(array $types): array
    {
        $types[$this->get_type()] = __('Kickflip Product');

        return $types;
    }

    public function get_type(): string
    {
        return self::TYPE_NAME;
    }

    public function addMczrTab(array $tabs): array
    {
        $tabs[self::TYPE_NAME] = [
            'label' => __('Kickflip', 'woocommerce'),
            'target' => self::TYPE_NAME . '_data',
            'class' => ['show_if_' . self::TYPE_NAME],
        ];

        return $tabs;
    }

    public function processAndSaveTabMetaField(): void
    {
        $controller = new MczrProductTypeController();
        $controller->processAndSaveTabMetaField();
    }

    public function addMczrTabContent(): void
    {
        $productId = ((int) $this->request->get('post'));

        $vars = [
            'mczrStartingPoint' => [
                'value' => \esc_attr(\get_post_meta($productId, 'mczrStartingPoint', true)),
                'typeKey' => $this->get_type(),
            ],
            'mczrQuantityMin' => [
                'value' => \esc_attr(\get_post_meta($productId, 'mczrQuantityMin', true)),
                'typeKey' => $this->get_type(),
            ],
            'mczrQuantityMax' => [
                'value' => \esc_attr(\get_post_meta($productId, 'mczrQuantityMax', true)),
                'typeKey' => $this->get_type(),
            ],
            'mczrQuantityStep' => [
                'value' => \esc_attr(\get_post_meta($productId, 'mczrQuantityStep', true)),
                'typeKey' => $this->get_type(),
            ],
            'mczrQuantityStart' => [
                'value' => \esc_attr(\get_post_meta($productId, 'mczrQuantityStart', true)),
                'typeKey' => $this->get_type(),
            ],
        ];

        $allowed_html = [
            'a' => [
                'href' => [],
                'target' => [],
            ],
            'br' => [],
            'button' => [
                'class' => [],
                'id' => [],
                'name' => [],
                'type' => [],
            ],
            'div' => [
                'id' => [],
                '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' => [],
                'placeholder' => [],
                'readonly' => [],
                'type' => [],
                'value' => [],
            ],
            'label' => [
                'class' => [],
                'for' => [],
            ],
            'li' => [
                'class' => [],
            ],
            'p' => [
                'class' => [],
            ],
            'textarea' => [
                'class' => [],
                'id' => [],
                'help' => [],
                'name' => [],
            ],
            'script' => [],
            'small' => [
                'class' => [],
            ],
            'strong' => [],
            'style' => [],
            'ul' => [
                'class' => [],
            ],
        ];

        echo \wp_kses($this->twig->render('Form/mczr-tab-fields.html.twig', $vars), $allowed_html);
    }
}
