<?php
if (!defined('ABSPATH')) {
    exit;
}
/* @var UpVimeo_Api_Helper $upvimeo_vimeo_helper */

$upvimeo_vimeo_helper = isset($args['upvimeo_helper']) ? $args['upvimeo_helper'] : (isset($upvimeo_vimeo_helper) ? $upvimeo_vimeo_helper : null);

?>

<div class="vimeo-api-info">
    <table>
        <tr>
            <th style="width: 25%">
                <?php esc_html_e('Status', 'upvimeo-direct-uploads'); ?>
            </th>
            <td>
                <?php if ($upvimeo_vimeo_helper->is_connected && $upvimeo_vimeo_helper->is_authenticated_connection): ?>
                    <p class="wvv-status-green"><?php esc_html_e('Connected', 'upvimeo-direct-uploads'); ?></p>
                <?php elseif ($upvimeo_vimeo_helper->is_connected && !$upvimeo_vimeo_helper->is_authenticated_connection): ?>
                    <p class="wvv-status-yellow"><?php esc_html_e('Connected (Unauthenticated)', 'upvimeo-direct-uploads'); ?></p>
                <?php else: ?>
                    <p class="wvv-status-red"><?php esc_html_e('Not Connected', 'upvimeo-direct-uploads'); ?></p>
                <?php endif; ?>
            </td>
        </tr>
        <?php if ($upvimeo_vimeo_helper->is_connected): ?>

            <?php if ($upvimeo_vimeo_helper->is_authenticated_connection): ?>
                <tr>
                    <th>
                        <?php esc_html_e('User', 'upvimeo-direct-uploads'); ?>
                    </th>
                    <td>
                        <a href="<?php echo esc_url($upvimeo_vimeo_helper->user_link); ?>"
                            target="_blank"><?php echo esc_html($upvimeo_vimeo_helper->user_name); ?></a>
                    </td>
                </tr>
            <?php endif; ?>

            <?php if ($upvimeo_vimeo_helper->is_authenticated_connection): ?>
                <tr>
                    <th>
                        <?php esc_html_e('Plan', 'upvimeo-direct-uploads'); ?>
                    </th>
                    <td>
                        <?php echo esc_html($upvimeo_vimeo_helper->get_plan(true)); ?>
                    </td>
                </tr>
            <?php endif; ?>

            <tr>
                <th>
                    <?php esc_html_e('App', 'upvimeo-direct-uploads'); ?>
                </th>
                <td>
                    <?php echo esc_html($upvimeo_vimeo_helper->app_name); ?>
                </td>
            </tr>
            <tr>
                <th>
                    <?php esc_html_e('Scopes', 'upvimeo-direct-uploads'); ?>
                </th>
                <td>
                    <?php
                    if (!empty($upvimeo_vimeo_helper->scopes)) {
                        echo esc_html(implode(', ', $upvimeo_vimeo_helper->scopes));
                    } else {
                        echo esc_html__('No scopes found', 'upvimeo-direct-uploads');
                    }
                    ?>
                </td>
            </tr>
            <?php
            ?>     <?php if (!empty($upvimeo_vimeo_helper->upload_quota)): ?>
                <tr>
                    <th>
                        <?php esc_html_e('Quota', 'upvimeo-direct-uploads'); ?>
                    </th>
                    <td>
                        <?php
                        switch ($upvimeo_vimeo_helper->get_current_quota_type()) {
                            case UpVimeo_Api_Helper::QUOTA_TYPE_VIDEOS_COUNT:
                                $upvimeo_used = $upvimeo_vimeo_helper->get_current_used_quota();
                                $upvimeo_max = $upvimeo_vimeo_helper->get_current_max_quota();
                                $upvimeo_reset = $upvimeo_vimeo_helper->get_quota_reset_date();
                                if ($upvimeo_reset) {
                                    /* translators: 1: Used quota, 2: Max quota, 3: Reset date */
                                    /* translators: %1$s: [Used space], %2$s: [Max space], %3$s: [Reset date] */
                                    echo sprintf(esc_html__('%1$s / %2$s (resets on %3$s)', 'upvimeo-direct-uploads'), esc_html($upvimeo_used), esc_html($upvimeo_max), esc_html($upvimeo_reset));
                                } else {
                                    /* translators: 1: Used quota, 2: Max quota */
                                    /* translators: %1$s: [Used space], %2$s: [Max space] */
                                    echo sprintf(esc_html__('%1$s / %2$s', 'upvimeo-direct-uploads'), esc_html($upvimeo_used), esc_html($upvimeo_max));
                                }
                                break;
                            case UpVimeo_Api_Helper::QUOTA_TYPE_VIDEOS_SIZE:
                                $upvimeo_used = upvimeo_format_bytes((int) $upvimeo_vimeo_helper->get_current_used_quota(), 2);
                                $upvimeo_max = upvimeo_format_bytes((int) $upvimeo_vimeo_helper->get_current_max_quota(), 2);
                                $upvimeo_reset = upvimeo_format_datetime_tz($upvimeo_vimeo_helper->get_quota_reset_date());
                                if ($upvimeo_reset) {
                                    /* translators: 1: Used quota, 2: Max quota, 3: Reset date */
                                    /* translators: %1$s: [Used size], %2$s: [Max size], %3$s: [Reset date] */
                                    echo sprintf(esc_html__('%1$s / %2$s (resets on %3$s)', 'upvimeo-direct-uploads'), esc_html($upvimeo_used), esc_html($upvimeo_max), esc_html($upvimeo_reset));
                                } else {
                                    /* translators: 1: Used quota, 2: Max quota */
                                    /* translators: %1$s: [Used size], %2$s: [Max size] */
                                    echo sprintf(esc_html__('%1$s / %2$s', 'upvimeo-direct-uploads'), esc_html($upvimeo_used), esc_html($upvimeo_max));
                                }
                                break;
                            default:
                                echo esc_html__('Unsupported account quota type.', 'upvimeo-direct-uploads');
                                break;
                        }
                        ?>
                    </td>
                </tr>
            <?php endif; ?>
            <?php if (isset($upvimeo_vimeo_helper->headers['x-ratelimit-limit']) && is_numeric($upvimeo_vimeo_helper->headers['x-ratelimit-limit'])): ?>
                <tr>
                    <th>
                        <?php esc_html_e('Rate Limits', 'upvimeo-direct-uploads'); ?>
                    </th>
                    <td>
                        <?php
                        $upvimeo_used = $upvimeo_vimeo_helper->headers['x-ratelimit-limit'] - $upvimeo_vimeo_helper->headers['x-ratelimit-remaining'];
                        $upvimeo_max = $upvimeo_vimeo_helper->headers['x-ratelimit-limit'];
                        $upvimeo_reset = upvimeo_format_datetime_tz($upvimeo_vimeo_helper->headers['x-ratelimit-reset']);
                        /* translators: 1: Used rate limit, 2: Max rate limit, 3: Reset time */
                        /* translators: %1$s: [Used rate], %2$s: [Max rate], %3$s: [Reset date] */
                        echo sprintf(esc_html__('%1$s / %2$s per minute (resets on %3$s)', 'upvimeo-direct-uploads'), esc_html($upvimeo_used), esc_html($upvimeo_max), esc_html($upvimeo_reset));
                        ?>
                    </td>
                </tr>
            <?php endif; ?><?php endif; ?>
        <tr>
            <th>
                <?php esc_html_e('PHP Version', 'upvimeo-direct-uploads'); ?>
            </th>
            <td>
                <?php echo esc_html(PHP_VERSION); ?>
            </td>
        </tr>
        <?php if (isset($_SERVER['SERVER_SOFTWARE'])): ?>
            <tr>
                <th>
                    <?php esc_html_e('Web Server', 'upvimeo-direct-uploads'); ?>
                </th>
                <td>
                    <?php echo esc_html( sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) ); ?>
                </td>
            </tr>
        <?php endif; ?>
        <tr>
            <th>
                <?php esc_html_e('Max Upload Size', 'upvimeo-direct-uploads'); ?>
            </th>
            <td>
                <?php echo esc_html(ini_get('upload_max_filesize')); ?>
            </td>
        </tr>
        <tr>
            <th>
                <?php esc_html_e('Max Post Size', 'upvimeo-direct-uploads'); ?>
            </th>
            <td>
                <?php echo esc_html(ini_get('post_max_size')); ?>
            </td>
        </tr>
        <tr>
            <th>
                <?php esc_html_e('Max Exec Time', 'upvimeo-direct-uploads'); ?>
            </th>
            <td>
                <?php echo esc_html(ini_get('max_execution_time')); ?>
            </td>
        </tr>
        <tr>
            <th>
                <?php esc_html_e('Max Input Time', 'upvimeo-direct-uploads'); ?>
            </th>
            <td>
                <?php echo esc_html(ini_get('max_input_time')); ?>
            </td>
        </tr>

        <?php
        // Detect problems
        $upvimeo_problems = $upvimeo_vimeo_helper->find_problems();
        ?>

        <?php if (!empty($upvimeo_problems) && count($upvimeo_problems) > 0): ?>
            <tr class="wvv-problems">
                <th>
                    <?php esc_html_e('Detected Problems', 'upvimeo-direct-uploads'); ?>
                </th>
                <td>
                    <p class="wvv-problem-head">
                        <?php esc_html_e('Fix the following problems to ensure proper function:', 'upvimeo-direct-uploads'); ?>
                    </p>
                    <ol>
                        <?php foreach ($upvimeo_problems as $upvimeo_problem): ?>
                            <li>
                                <div class="wvv-problem-wrapper">
                                    <div class="wvv-problem--info">
                                        <p><?php echo wp_kses_post($upvimeo_problem['info']); ?></p>
                                        <p><a class="wvv-problem-fix-trigger"
                                                href="#"><?php esc_html_e('Fix prolbem', 'upvimeo-direct-uploads'); ?></a></p>
                                    </div>
                                    <div class="wvv-problem--fix" style="display: none;">
                                        <?php echo wp_kses_post($upvimeo_problem['fix']); ?>
                                    </div>
                                </div>
                            </li>
                        <?php endforeach; ?>
                    </ol>
                </td>
            </tr>
        <?php endif; ?>

    </table>
</div>