<?php

namespace SmartSyncS3\Views;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
use SmartSyncS3\Models\SMARL_CloudConfig;


class SMARL_CustomUrl
{
    private SMARL_CloudConfig $config;

    public function __construct(SMARL_CloudConfig $config)
    {
        $this->config = $config;
    }

    public function loadView(): void
    {
        add_action('admin_menu', [$this, 'addAdminSubMenu']);
    }

    /**
     * Adds a submenu to the WordPress admin menu.
     *
     * @return void
     */
    public function addAdminSubMenu(): void
    {
        add_submenu_page(
            'smarl-smart-sync',  // Parent menu slug
            esc_html__('Custom Cloudfront URL', 'smartsync-lite-media-offloader-and-assets-cdn'),  // Page title
            esc_html__('Custom Cloudfront URL', 'smartsync-lite-media-offloader-and-assets-cdn'),  // Menu title
            'manage_options',  // Capability
            'custom-cloudfront-url',  // Submenu slug
            [$this, 'renderCustomUrlPage']  // Callback function
        );

    }

    public function renderCustomUrlPage(): void
    {
        $config = $this->config;

        ?>
        <div class="smartsync-wrap ">
            <div id="initial_certificates" class="d-none">
                <?php echo wp_json_encode($config->customUrl->toArray()); ?>
            </div>

            <div class="wrap">

                <h1 class="page-title"><?php esc_html_e('Setup Custom Cloudfront URL:', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></h1>
                <?php
                if ($config->serviceProvider != "aws") {
                    ?>
                    <div class="notice notice-error">

                        <ul>
                            <li>
                                <strong><?php esc_html_e('AWS not found', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></strong>:
                                <?php esc_html_e('Custom URL is currently only supported in AWS. Please configure AWS to use this feature.', 'smartsync-lite-media-offloader-and-assets-cdn'); ?>
                            </li>

                        </ul>
                    </div>
                    <?php
                    return;
                } elseif (!$config->useCloudFront) {
                    ?>
                    <div class="notice notice-error">
                        <h2><?php esc_html_e('Errors Detected', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></h2>
                        <ul>
                            <li>
                                <strong><?php esc_html_e('Cloudfront not configured', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></strong>:
                                <?php esc_html_e('Please configure Cloudfront to use custom URL.', 'smartsync-lite-media-offloader-and-assets-cdn'); ?>
                            </li>

                        </ul>
                    </div>
                    <?php
                    return;
                }
                ?>
                <p>
                    <?php esc_html_e('Do you want to use a custom domain for your CDN? Using this will automatically generate an SSL certificate from AWS Certificate Manager.', 'smartsync-lite-media-offloader-and-assets-cdn'); ?>
                </p>
                <div id="custom-domain-container">
                    <div class="mb-1">
                        <label for="custom-domain"><?php esc_html_e('Custom Domain Name', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></label>
                        <input
                                type="text"
                                id="custom-domain"
                                placeholder="<?php esc_attresc_html_e('your-cdn.your-domain.com', 'smartsync-lite-media-offloader-and-assets-cdn'); ?>"
                                value="<?php echo esc_attr($config->customUrl->domain); ?>">
                    </div>

                    <button id="generate-certificate" class="button">
                        <?php esc_html_e('Generate Certificate', 'smartsync-lite-media-offloader-and-assets-cdn'); ?>
                    </button>
                    <button class="button" id="cloudfront-attachment-button" disabled>
                        <?php esc_html_e('Attach to Cloudfront', 'smartsync-lite-media-offloader-and-assets-cdn'); ?>
                    </button>
                    <div id="records">
                        <div id="record-wrap">

                        </div>
                    </div>
                </div>
            </div>
        </div>

        <?php
    }
}
