<?php

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


class SMARL_SettingsPage
{
    private SMARL_CloudConfig $config;
    public function __construct(SMARL_CloudConfig $config)
    {
        $this->config = $config;
    }
    public function loadView(): void
    {
        add_action('admin_menu', [$this, 'addAdminSubMenu']);
    }

    public function addAdminSubMenu(): void
    {
        add_menu_page(
            page_title: 'Cloud Configuration', // Page title
            menu_title: 'SmartSync setting',                 // Menu title
            capability: 'manage_options',               // Capability
            menu_slug: 'smarl-smart-sync',                // Menu slug
            callback: [$this, 'renderSettingsPage'],     // Callback function
            icon_url: 'dashicons-cloud',              // Icon for the menu
            position: 75                              // Position in the menu
        );
    }

    public function renderSettingsPage(): void {
        $cloud_enabled_svg = '<svg  xmlns="http://www.w3.org/2000/svg"  width="24"  height="24"  viewBox="0 0 24 24"  fill="none"  stroke="currentColor"  stroke-width="2"  stroke-linecap="round"  stroke-linejoin="round"  class="icon icon-tabler icons-tabler-outline icon-tabler-brand-onedrive"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M18.456 10.45a6.45 6.45 0 0 0 -12 -2.151a4.857 4.857 0 0 0 -4.44 5.241a4.856 4.856 0 0 0 5.236 4.444h10.751a3.771 3.771 0 0 0 3.99 -3.54a3.772 3.772 0 0 0 -3.538 -3.992z" /></svg>';
        $cloud_disabled_svg = '<svg  xmlns="http://www.w3.org/2000/svg"  width="24"  height="24"  viewBox="0 0 24 24"  fill="none"  stroke="currentColor"  stroke-width="2"  stroke-linecap="round"  stroke-linejoin="round"  class="icon icon-tabler icons-tabler-outline icon-tabler-cloud-x"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M13 18.004h-6.343c-2.572 -.004 -4.657 -2.011 -4.657 -4.487c0 -2.475 2.085 -4.482 4.657 -4.482c.393 -1.762 1.794 -3.2 3.675 -3.773c1.88 -.572 3.956 -.193 5.444 1c1.488 1.19 2.162 3.007 1.77 4.769h.99c1.37 0 2.556 .8 3.117 1.964" /><path d="M22 22l-5 -5" /><path d="M17 22l5 -5" /></svg>';

        $deleteEnabledSvg = '<svg  xmlns="http://www.w3.org/2000/svg"  width="24"  height="24"  viewBox="0 0 24 24"  fill="none"  stroke="currentColor"  stroke-width="2"  stroke-linecap="round"  stroke-linejoin="round"  class="icon icon-tabler icons-tabler-outline icon-tabler-trash"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M4 7l16 0" /><path d="M10 11l0 6" /><path d="M14 11l0 6" /><path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12" /><path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3" /></svg>';
        $deleteDisabledSvg = '<svg  xmlns="http://www.w3.org/2000/svg"  width="24"  height="24"  viewBox="0 0 24 24"  fill="none"  stroke="currentColor"  stroke-width="2"  stroke-linecap="round"  stroke-linejoin="round"  class="icon icon-tabler icons-tabler-outline icon-tabler-trash-x"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M4 7h16" /><path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12" /><path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3" /><path d="M10 12l4 4m0 -4l-4 4" /></svg>';
        $config = $this->config;
        ?>
            <div class="smartsync-wrap">
                <div class="wrap aws-settings">
                    <h1 class="page-title"><?php esc_html_e('Cloud Settings', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></h1>
                    <!-- Plugin Compatibility Check -->
                    <?php if (!SMARL_Helper::checkPluginCompatibility()["compatible"]): ?>
                        <div class="notice notice-error">
                            <h2><?php esc_html_e('Errors Detected', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></h2>
                            <ul>
                                <?php foreach (SMARL_Helper::checkPluginCompatibility()["error"] as $error): ?>
                                    <li>
                                        <strong><?php echo esc_html($error["error_key"]); ?></strong>: <?php echo esc_html($error["error_message"]); ?>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                    <?php else: ?>
                        <?php echo wp_kses_post(SMARL_Helper::renderProModal()); ?>
                        <!-- Navigation Tabs -->
                        <div class="tabs-container">
                            <button class="tab-button active" data-tab="general"><?php esc_html_e('General', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></button>
                            <button class="tab-button" data-tab="media"><?php esc_html_e('Media', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></button>
                            <button class="tab-button" data-tab="assets"><?php esc_html_e('Assets', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></button>
                            <div class="tab-indicator"></div>
                        </div>
                        <!-- Tab Content -->
                        <div class="tabs-content">
                            <!-- General Tab -->
                            <div id="tab-general" class="tab-panel active">
                                <div class="settings-container">
                                    <!-- AWS Configuration -->
                                    <input type="hidden" value="" id="current-sync-state">
                                    <div class="settings-section">
                                        <h2><?php esc_html_e('Cloud Configuration', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></h2>
                                        <table class="settings-table">
                                            <tr>
                                                <th><?php esc_html_e('Provider:', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></th>
                                                <td><?php echo esc_html(($config->serviceProvider) ?: 'Not configured'); ?></td>
                                            </tr>
                                            <tr>
                                                <th><?php esc_html_e('Bucket:', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></th>
                                                <td><?php echo esc_html(($config->bucket) ?: 'Not configured'); ?></td>
                                            </tr>
                                            <tr>
                                                <th><?php esc_html_e('Region:', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></th>
                                                <td><?php echo esc_html(($config->region) ?: 'Not configured'); ?></td>
                                            </tr>
                                            <tr>
                                                <th><?php esc_html_e('URL:', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></th>
                                                <td><?php echo esc_html(($config->url) ?: 'Not configured'); ?></td>
                                            </tr>
                                        </table>
                                        <button id="reconfigure-aws" data-url="<?php echo esc_url(admin_url('admin.php?page=aws-configuration-wizard')); ?>" class="primary-btn"><?php esc_html_e('Reconfigure AWS', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></button>
                                        <p class="description mt-2"><?php esc_html_e('Click to re-launch the Cloud Configuration Wizard.', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></p>
                                    </div>
                                    <!-- S3 Analytics -->
                                    <div class="settings-section">
                                        <h2><?php esc_html_e('Cloud Analytics', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></h2>
                                        <ul class="analytics-list">
                                            <li><strong><?php esc_html_e('Total Files:', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></strong> <span id="total-files"><?php esc_html_e('No Data', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></span></li>
                                            <li><strong><?php esc_html_e('Total Storage Used:', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></strong> <span id="total-storage"><?php esc_html_e('No Data', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></span></li>
                                            <li><strong><?php esc_html_e('Monthly Cost:', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></strong> <span id="total-price"><?php esc_html_e('No Data', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></span></li>
                                        </ul>
                                        <button id="fetch-analytics-button" class="primary-btn"><?php esc_html_e('Fetch Analytics', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></button>
                                        <p class="description"><?php esc_html_e('Click to Fetch Analytics from Cloud', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></p>
                                    </div>
                                    <?php if ($config->serviceProvider === 'aws'): ?>
                                        <div>
                                            <button id="clear-cloudfront-cache-button" class="primary-btn"><?php esc_html_e('Clear CloudFront Cache', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></button>
                                            <p class="description"><?php esc_html_e('Click to clear the CloudFront cache.', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <!-- Media Tab -->
                            <div id="tab-media" class="tab-panel">
                                <h3><?php esc_html_e('Media Settings', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></h3>
                                <div class="mb-15">

                                    <label for="use-cloudfront-media"><?php esc_html_e('Use Cloud Storage:', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></label>
                                    <?php SMARL_Helper::renderSwitch(id: 'use-cloudfront-media', additional_attr: [
                                        'data-type' => 'media',
                                        'class' => 'use-cloudfront-button'
                                    ], checked: $config->useCdnForMedia, onSvg: $cloud_enabled_svg, offSvg: $cloud_disabled_svg); ?>

                                </div>

                                <div class="mb-15">
                                    <label for="auto-delete-media"><?php esc_html_e('Automatically delete media from local after upload?', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></label>
                                    <?php SMARL_Helper::renderSwitch(id: 'auto-delete-media', checked: $config->mediaAutoDelete, onSvg: $deleteEnabledSvg, offSvg: $deleteDisabledSvg); ?>
                                </div>

                                <div>
                                    <button id="sync-local-to-aws" data-type="media" data-direction="upload" class="primary-btn sync-button"><?php esc_html_e('Sync Local to Cloud Media', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></button>
                                    <button id="sync-aws-to-local" data-type="media" data-direction="download" class="primary-btn sync-button"><?php esc_html_e('Sync Cloud to Local', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></button>
                                </div>
                            </div>

                            <!-- Assets Tab -->
                            <div id="tab-assets" class="tab-panel">
                                <h3><?php esc_html_e('Assets Settings', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></h3>
                                <label for="use-cloudfront-asset"><?php esc_html_e('Use Cloud Storage:', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></label>
                                <div class="mb-15">
                                    <?php SMARL_Helper::renderSwitch(id: 'use-cloudfront-asset', additional_attr: [
                                        'data-type' => 'assets',
                                        'class' => 'use-cloudfront-button'
                                    ], checked: $config->useCdnForAssets, onSvg: $cloud_enabled_svg, offSvg: $cloud_disabled_svg); ?>
                                </div>
                                <div>
                                    <button id="sync-local-to-aws-plugin" data-type="assets" data-direction="upload" class="primary-btn sync-button"><?php esc_html_e('Sync Local to Cloud Assets', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></button>
                                </div>
                            </div>
                        </div>
                        <!-- Sync Logs -->
                        <div class="d-flex justify-content-space-between align-center">
                            <h2><?php esc_html_e('Logs', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></h2>
                            <div>
                                <button class="stop-button" id="stop-all-processes"><?php esc_html_e('Stop all running processes', 'smartsync-lite-media-offloader-and-assets-cdn'); ?></button>
                            </div>
                        </div>
                        <div id="sync-log-container" class="sync-log">
                            <p id="sync-log-placeholder">
                                <?php esc_html_e('No logs available.', 'smartsync-lite-media-offloader-and-assets-cdn'); ?>
                            </p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>


        <?php
    }


}
