<?php

namespace Macymed\MacymedLinkTracker\Utils;

use Macymed\MacymedLinkTracker\Utils\TrackerHelper;
use Macymed\MacymedLinkTracker\Utils\Utils;

class RedirectPage
{
    public static function init()
    {
        add_action('wp_enqueue_scripts', [RedirectPage::class, 'enqueueScripts']);

        add_action('macymed_link_tracker_redirect_page_head', [RedirectPage::class, 'renderHead'], 10);
        add_action('macymed_link_tracker_redirect_page_logo', [RedirectPage::class, 'renderLogo'], 10);
        add_action('macymed_link_tracker_redirect_page_title', [RedirectPage::class, 'renderTitle'], 10);
        add_action('macymed_link_tracker_redirect_page_message', [RedirectPage::class, 'renderMessage'], 10);
        add_action('macymed_link_tracker_redirect_page_buttons', [RedirectPage::class, 'renderButtons'], 10);

    }

    public static function enqueueScripts()
    {
        wp_enqueue_script('macymed-redirect-script', Utils::get_plugin_url() . 'dist/js/redirect.js' . '?t=' . time(), [], '1.0.0', true);
        wp_localize_script('macymed-redirect-script', 'macymedLinkTrackerScript', [
            'nonce' => wp_create_nonce('redirect_external_action'),
            'timerRefresh' => intval(get_option('macymed_tracker_redirect_page_delay', 3)),
        ]);
        wp_enqueue_style('macymed-redirect-style', Utils::get_plugin_url() . 'dist/css/redirect.css' . '?t=' . time(), [], '1.0.0');
    }

    public static function renderHead()
    {


        // Récupérer les options configurées dans WordPress
        $timeToRefresh       = 3;
        $displayTitle        = true;
        $title               = 'Redirection en cours...';


        ?>
        <meta http-equiv="refresh" content="<?php echo esc_html($timeToRefresh); ?>;url=<?php echo esc_url(self::getUrlToRedirect()); ?>">
        <?php if ($displayTitle) : ?>
            <title><?php echo esc_html($title); ?></title>
        <?php endif; ?>
        <?php
    }
    public static function renderLogo()
    {
        $displayLogo      = get_option('macymed_tracker_redirect_page_logo_link_tracker', '1') === '1';

        if($displayLogo){
        ?>
        <div class="redirect-link-tracker-logo redirect-link-tracker-image">
            
        </div>
        <?php
        }
    }

    public static function renderTitle()
    {
        $displayTitle        = true;
        $title               = 'Redirection en cours...';

        ?>
        <?php if ($displayTitle) : ?>
            <div class="redirect-title">
                <?php echo esc_html($title); ?>
            </div>
        <?php endif; ?>
        <?php
    }

    public static function renderMessage()
    {

        $timeToRefresh       = 3;
        $displayMessage      = get_option('macymed_tracker_redirect_page_display_message', '1') === '1';
        $message             = get_option('macymed_tracker_redirect_page_message', 'Vous allez être redirigé vers le site {{redirect_url}} dans {{delay}} secondes...');
        // Remplacer les variables dans le message
        $message = str_replace(
            ['{{redirect_url}}', '{{delay}}'],
            [
                '<strong>' . esc_html(self::getUrlToRedirect()) . '</strong>',
                '<span id="countdown">' . $timeToRefresh . '</span>',
            ],
            ($message)
        );
        
        ?>
        <?php if ($displayMessage) : ?>
            <div class="redirect-message">
                <?php
                echo wp_kses(
                    $message,
                    [
                                                                'table'  => [],
                                                                'tr'     => [],
                                                                'td'     => ['style' => []],
                                                                'strong' => [],
                                                                'span' => [
                                                                    'id' => []
                                                                ],
                                                                'label'  => [],
                                                                'b'      => [],
                                                                'i'      => [],
                                                            ]
                );
            ?>
            </div>
        <?php endif; ?>
        <?php
    }

    public static function getUrlToRedirect()
    {
        $url = '/';
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        if (isset($_GET['url'])) {
            // phpcs:ignore WordPress.Security.NonceVerification.Recommended
            $url = urldecode(sanitize_url(wp_unslash($_GET['url'])));
        }
        return $url;
    }
    public static function renderButtons()
    {


    }


}
