<?php

declare(strict_types=1);

namespace MyCustomizer\WooCommerce\Connector;

use MyCustomizer\WooCommerce\Connector\Config\MczrConfig;
use MyCustomizer\WooCommerce\Connector\Libs\MczrConnect;
use MyCustomizer\WooCommerce\Connector\Libs\MczrSecurity;
use MyCustomizer\WooCommerce\Connector\Libs\MczrSettings;

class MczrPlugin
{
    public function init(): void
    {
        $plugin = basename(\plugin_dir_path(dirname(__FILE__)));
        \add_action('wp_enqueue_scripts', [$this, 'addCustomScripts']);
        \add_filter("plugin_action_links_$plugin/kickflip-product-configurators.php", [$this, 'addDashboardLink']);
    }

    public function onActivate(): void
    {
        $security = new MczrSecurity();
        $settings = new MczrSettings();

        if (empty($settings->get('authorizationKey'))) {
            $settings->updateOne('authorizationKey', $security->generateKey());
        }

        if (empty($settings->get('iframeHook'))) {
            $settings->updateOne('iframeHook', 'woocommerce_after_single_product_summary');
        }

        if (empty($settings->get('iframeHookPriority'))) {
            $settings->updateOne('iframeHookPriority', 0);
        }

        if (empty($settings->get('iframeWidth'))) {
            $settings->updateOne('iframeWidth', '100%');
        }

        if (empty($settings->get('iframeHeight'))) {
            $settings->updateOne('iframeHeight', '700px');
        }

        if (empty($settings->get('productCss'))) {
            $settings->updateOne('productCss', '#primary{width:100%;} #mczrMainIframe{border: none;}');
        }

        $brand = $settings->get('brand');
        $shopId = $settings->get('shopId');

        if (!empty($brand)) {
            $mczrConnect = new MczrConnect();
            $mczrConnect->connect($brand, $shopId);
        }
    }

    public function addCustomScripts(): void
    {
        \wp_enqueue_script('js', \plugin_dir_url(__FILE__) . 'Resources/public/js/mczr.js', [], 1);
        \wp_localize_script('js', 'ajaxFrontObj', ['ajax_url' => \admin_url('admin-ajax.php')]);
    }

    public function addDashboardLink(array $links): array
    {
        $settings = new MczrSettings();

        $brand = $settings->get('brand');

        if (empty($brand)) {
            $href = MczrConfig::getConfig()['registerUrl'] . '?eCommerce=woocommerce&shop=' . \get_site_url() . '&woocommerceToken=' . $settings->get('authorizationKey');
        } else {
            $href = str_replace('{{brand}}', $brand, MczrConfig::getConfig()['dashboardUrlPattern']);
        }

        $settings_link = '<a href="' . $href . '" target="blank">Dashboard</a>';
        array_unshift($links, $settings_link);

        return $links;
    }
}
