<?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);
$upvimeo_db_helper = isset($args['db_helper']) ? $args['db_helper'] : (isset($upvimeo_db_helper) ? $upvimeo_db_helper : null);
$upvimeo_settings_helper = isset($args['settings_helper']) ? $args['settings_helper'] : (isset($upvimeo_settings_helper) ? $upvimeo_settings_helper : null);

/* @var UpVimeo_Db_Helper $upvimeo_db_helper */
/* @var UpVimeo_Settings_Helper $upvimeo_settings_helper */

use Vimeify\Vimeo\Exceptions\VimeoRequestException;

// Verify Nonce
if ( ! isset( $_GET['upvimeo_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['upvimeo_nonce'] ), 'upvimeo_action' ) ) {
    wp_die( esc_html__( 'Security check failed. Please try again.', 'upvimeo-direct-uploads' ) );
}

// Capability Check
if ( ! current_user_can( 'upload_files' ) ) {
    wp_die( esc_html__( 'You do not have permission to manage videos.', 'upvimeo-direct-uploads' ) );
}

$upvimeo_video_id = isset($_GET['id']) ? sanitize_text_field(wp_unslash($_GET['id'])) : null;
$upvimeo_vimeo_id = $upvimeo_db_helper->get_vimeo_id($upvimeo_video_id);
$upvimeo_permalink = get_permalink($upvimeo_video_id);
$upvimeo_front_pages = (int) $upvimeo_settings_helper->get('upvimeo_enable_single_pages');
$upvimeo_vimeo_link = $upvimeo_db_helper->get_vimeo_link($upvimeo_video_id);

$upvimeo_folders_management = (int) $upvimeo_settings_helper->get('upvimeo_enable_folders_management');
$upvimeo_embed_presets_management = (int) $upvimeo_settings_helper->get('upvimeo_enable_embed_presets_management');
$upvimeo_embed_privacy_management = (int) $upvimeo_settings_helper->get('upvimeo_enable_embed_privacy_management');

// Gather data
$upvimeo_vimeo = array();
$upvimeo_view_privacy_opts = array();
$upvimeo_embed_preset_uri = null;
$upvimeo_folder_uri = null;

if ($upvimeo_vimeo_helper->is_connected && $upvimeo_vimeo_helper->can_edit()) {
    try {
        $upvimeo_vimeo = $upvimeo_vimeo_helper->get_video_by_local_id($upvimeo_video_id, array(
            'uri',
            'name',
            'description',
            'link',
            'duration',
            'width',
            'height',
            'is_playable',
            'privacy',
            'embed',
            'parent_folder',
            'upload'
        ));
    } catch (\Exception $e) {
    }

    $upvimeo_view_privacy_opts = $upvimeo_vimeo_helper->get_view_privacy_options_for_forms('admin');

    $upvimeo_embed_preset_uri = isset($upvimeo_vimeo['body']['embed']['uri']) && !empty($upvimeo_vimeo['body']['embed']['uri']) ? $upvimeo_vimeo['body']['embed']['uri'] : null; //eg. /presets/120554271
    $upvimeo_folder_uri = isset($upvimeo_vimeo['body']['parent_folder']['uri']) && !empty($upvimeo_vimeo['body']['parent_folder']['uri']) ? $upvimeo_vimeo['body']['parent_folder']['uri'] : null; //eg. /users/120624714/projects/2801250
}
?>

<div class="upvimeo-edit-page-wrapper">

    <!-- Header -->
    <div class="upvimeo-edit-header">
        <div class="upvimeo-header-left">
            <a href="<?php echo esc_url(admin_url('admin.php?page=upvimeo-library')); ?>" class="upvimeo-back-link"><span
                    class="dashicons dashicons-arrow-left-alt2"></span> <?php esc_html_e('All videos', 'upvimeo-direct-uploads'); ?></a>
            <h1 class="upvimeo-page-title">
                <?php
                $upvimeo_page_title = isset($upvimeo_vimeo['body']['name']) ? $upvimeo_vimeo['body']['name'] : get_the_title($upvimeo_video_id);
                echo esc_html($upvimeo_page_title);
                ?>
            </h1>
            <?php if ($upvimeo_front_pages): ?>
                <div class="upvimeo-permalink-wrapper">
                    <a href="<?php echo esc_url($upvimeo_permalink); ?>" target="_blank"
                        class="upvimeo-permalink"><?php echo esc_html($upvimeo_permalink); ?> <span
                            class="dashicons dashicons-external"></span></a>
                </div>
            <?php endif; ?>
        </div>
        <div class="upvimeo-header-right">
            <a target="_blank" href="<?php echo esc_url($upvimeo_vimeo_link); ?>"
                class="button"><?php esc_html_e('View on Vimeo', 'upvimeo-direct-uploads'); ?> <span
                    class="dashicons dashicons-external"></span></a>
        </div>
    </div>


    <?php if (!$upvimeo_vimeo_helper->is_connected): ?>
        <div class="notice notice-error inline">
            <p><?php esc_html_e('Please enter valid api credentails.', 'upvimeo-direct-uploads'); ?></p>
        </div>
    <?php elseif (!$upvimeo_vimeo_helper->can_edit()): ?>
        <div class="notice notice-warning inline">
            <p><?php esc_html_e('Edit scope is missing. Please request Edit scope for your access token in the Vimeo Developer Tools in order to be able to edit videos', 'upvimeo-direct-uploads'); ?>
            </p>
        </div>
    <?php else: ?>

        <!-- Player (Full Width) -->
        <div class="upvimeo-player-full-width">
            <?php
            if (isset($upvimeo_vimeo['body']['embed']['html'])) {
                // Use the exact embed code provided by Vimeo API (often needed for private/unlisted videos)
                echo '<div class="upvimeo-embed-container">' . wp_kses($upvimeo_vimeo['body']['embed']['html'], array(
                    'iframe' => array(
                        'src'             => true,
                        'width'           => true,
                        'height'          => true,
                        'frameborder'     => true,
                        'allow'           => true,
                        'allowfullscreen' => true,
                        'title'           => true,
                        'style'           => true,
                    )
                )) . '</div>';
            } else {
                echo do_shortcode('[UPVIMEO_vimeo_video id="' . esc_attr($upvimeo_vimeo_id) . '"]');
            }
            ?>
        </div>

        <div class="upvimeo-edit-grid">
            <!-- Main Content -->
            <div class="upvimeo-main-content">

                <div class="wvv-notices-wrapper"></div>

                <!-- Basic Info Form -->
                <div class="upvimeo-card">
                    <form id="upvimeo-video-save-basic" action="<?php echo esc_url(admin_url('admin-ajax.php')); ?>" method="post">
                        <div class="upvimeo-form-group">
                            <label for="name"><?php esc_html_e('Title', 'upvimeo-direct-uploads'); ?></label>
                            <input type="text" name="name" id="name" value="<?php echo esc_attr(isset($upvimeo_vimeo['body']['name']) ? $upvimeo_vimeo['body']['name'] : ''); ?>"
                                autocomplete="off" class="upvimeo-input-lg">
                        </div>
                        <div class="upvimeo-form-group">
                            <label for="description"><?php esc_html_e('Description', 'upvimeo-direct-uploads'); ?></label>
                            <textarea name="description" id="description" rows="5"
                                class="upvimeo-textarea"><?php echo esc_textarea(isset($upvimeo_vimeo['body']['description']) ? $upvimeo_vimeo['body']['description'] : ''); ?></textarea>
                        </div>
                        <input type="hidden" name="uri" value="<?php echo esc_attr(isset($upvimeo_vimeo['body']['uri']) ? $upvimeo_vimeo['body']['uri'] : ''); ?>">
                        <div class="upvimeo-form-actions">
                            <button type="submit"
                                class="button button-primary button-hero"><?php esc_html_e('Save', 'upvimeo-direct-uploads'); ?></button>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Sidebar -->
            <div class="upvimeo-sidebar">

                <!-- View Privacy -->
                <div class="upvimeo-card">
                    <h3 class="upvimeo-card-title"><?php esc_html_e('Privacy', 'upvimeo-direct-uploads'); ?></h3>
                    <form id="upvimeo-video-save-view-privacy" action="<?php echo esc_url(admin_url('admin-ajax.php')); ?>"
                        method="post">
                        <div class="upvimeo-form-group">
                            <select name="view_privacy" id="view_privacy" class="upvimeo-select-full">
                                <?php foreach ($upvimeo_view_privacy_opts as $upvimeo_key => $upvimeo_option): ?>
                                    <?php
                                    $upvimeo_is_selected = isset($upvimeo_vimeo['body']['privacy']['view']) && $upvimeo_vimeo['body']['privacy']['view'] === $upvimeo_key;
                                    $upvimeo_is_disabled = !$upvimeo_option['available'];
                                    ?>
                                    <option value="<?php echo esc_attr($upvimeo_key); ?>" <?php selected($upvimeo_is_selected, true); ?><?php echo esc_attr($upvimeo_is_disabled ? ' disabled' : ''); ?>>
                                        <?php echo esc_html($upvimeo_option['name']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="upvimeo-form-actions">
                            <input type="hidden" name="uri" value="<?php echo esc_attr(isset($upvimeo_vimeo['body']['uri']) ? $upvimeo_vimeo['body']['uri'] : ''); ?>">
                            <button type="submit"
                                class="button button-primary upvimeo-btn-block"><?php esc_html_e('Update privacy', 'upvimeo-direct-uploads'); ?></button>
                        </div>
                    </form>
                </div>

                <?php if ($upvimeo_embed_privacy_management): ?>
                    <!-- Embed Privacy -->
                    <div class="upvimeo-card">
                        <h3 class="upvimeo-card-title"><?php esc_html_e('Embed', 'upvimeo-direct-uploads'); ?></h3>
                        <form id="upvimeo-video-save-embed-privacy" action="<?php echo esc_url(admin_url('admin-ajax.php')); ?>"
                            method="post">
                            <div class="upvimeo-form-group">
                                <label for="privacy_embed"
                                    class="screen-reader-text"><?php esc_html_e('Embed privacy type', 'upvimeo-direct-uploads'); ?></label>
                                <select id="privacy_embed" name="privacy_embed" data-target=".upvimeo-privacy-whitelist-domains"
                                    data-show-target-if-value="whitelist" class="upvimeo-conditional-field upvimeo-select-full">
                                    <option value="public" <?php selected(isset($upvimeo_vimeo['body']['privacy']['embed']) ? $upvimeo_vimeo['body']['privacy']['embed'] : '', 'public'); ?>>
                                        <?php esc_html_e('Anywhere', 'upvimeo-direct-uploads'); ?>
                                    </option>
                                    <option value="whitelist" <?php selected(isset($upvimeo_vimeo['body']['privacy']['embed']) ? $upvimeo_vimeo['body']['privacy']['embed'] : '', 'whitelist'); ?>><?php esc_html_e('Specific domains', 'upvimeo-direct-uploads'); ?></option>
                                </select>
                            </div>

                            <div class="upvimeo-form-group upvimeo-privacy-whitelist-domains"
                                style="<?php echo (isset($upvimeo_vimeo['body']['privacy']['embed']) && $upvimeo_vimeo['body']['privacy']['embed'] !== 'whitelist') ? 'display:none;' : ''; ?>">
                                <div class="upvimeo-input-group">
                                    <input type="text" name="privacy_embed_domain" id="privacy_embed_domain"
                                        placeholder="example.com" />
                                    <button type="submit" name="admin_action" value="add_domain" class="button button-secondary"
                                        disabled><?php esc_html_e('Add', 'upvimeo-direct-uploads'); ?></button>
                                </div>

                                <ul class="privacy-embed-whitelisted-domains-list">
                                    <?php
                                    try {
                                        if (isset($upvimeo_vimeo['body']['uri'])) {
                                            $upvimeo_domains = $upvimeo_vimeo_helper->get_whitelisted_domains($upvimeo_vimeo['body']['uri']);

                                            if ($upvimeo_domains['status'] === 200) {
                                                foreach ($upvimeo_domains['body']['data'] as $upvimeo_domain) {
                                                    echo '<li>' . esc_html($upvimeo_domain['domain']) . ' <a href="#" class="upvimeo-delete-domain" data-uri="' . esc_attr($upvimeo_vimeo['body']['uri']) . '" data-domain="' . esc_attr($upvimeo_domain['domain']) . '"><span class="dashicons dashicons-no-alt"></span></a> </li>';
                                                }
                                            }
                                        }
                                    } catch (VimeoRequestException $upvimeo_e) {
                                        echo "<li class='error'>" . esc_html($upvimeo_e->getMessage()) . "</li>";
                                    }
                                    ?>
                                </ul>
                            </div>

                            <div class="upvimeo-form-actions">
                                <input type="hidden" name="uri" value="<?php echo esc_attr(isset($upvimeo_vimeo['body']['uri']) ? $upvimeo_vimeo['body']['uri'] : ''); ?>">
                                <button type="submit" class="button button-primary upvimeo-btn-block" name="action" id="save"
                                    value="1"><?php esc_html_e('Save', 'upvimeo-direct-uploads'); ?></button>
                            </div>
                        </form>
                    </div>
                <?php endif; ?>

                <?php if ($upvimeo_embed_presets_management): ?>
                    <!-- Embed Presets -->
                    <div class="upvimeo-card">
                        <h3 class="upvimeo-card-title"><?php esc_html_e('Embed Preset', 'upvimeo-direct-uploads'); ?></h3>
                        <?php if (!$upvimeo_vimeo_helper->supports_embed_presets()): ?>
                            <div class="upvimeo-upgrade-notice">
                                <p><?php esc_html_e('Requires Vimeo PRO or higher.', 'upvimeo-direct-uploads'); ?></p>
                                <a href="https://vimeo.com/upgrade" target="_blank"
                                    class="button button-secondary"><?php esc_html_e('Upgrade', 'upvimeo-direct-uploads'); ?></a>
                            </div>
                        <?php else: ?>
                            <?php
                            $upvimeo_presets = $upvimeo_vimeo_helper->get_embed_presets_options();
                            $upvimeo_current_preset_uri = empty($upvimeo_embed_preset_uri) ? 'default' : upvimeo_embed_preset_uri_to_id($upvimeo_embed_preset_uri);
                            ?>
                            <form id="upvimeo-video-save-embed-preset" action="<?php echo esc_url(admin_url('admin-ajax.php')); ?>"
                                method="post">
                                <div class="upvimeo-form-group">
                                    <select id="embed_preset_uri" name="embed_preset_uri" class="upvimeo-select-full">
                                        <?php foreach ($upvimeo_presets as $upvimeo_preset_uri => $upvimeo_preset): ?>
                                            <?php $upvimeo_preset_uri = upvimeo_embed_preset_uri_to_id($upvimeo_preset_uri); ?>
                                            <option <?php selected($upvimeo_current_preset_uri, $upvimeo_preset_uri); ?>value="<?php echo esc_attr($upvimeo_preset_uri); ?>"><?php echo esc_html($upvimeo_preset); ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="upvimeo-form-actions">
                                    <input type="hidden" name="video_uri" value="<?php echo esc_attr(isset($upvimeo_vimeo['body']['uri']) ? $upvimeo_vimeo['body']['uri'] : ''); ?>">
                                    <button type="submit" class="button button-primary upvimeo-btn-block" name="action" id="save"
                                        value="1"><?php esc_html_e('Save', 'upvimeo-direct-uploads'); ?></button>
                                </div>
                            </form>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>

                <!-- Folders -->
                <?php if ($upvimeo_folders_management): ?>
                    <div class="upvimeo-card">
                        <h3 class="upvimeo-card-title"><?php esc_html_e('Folder', 'upvimeo-direct-uploads'); ?></h3>
                        <?php if (!$upvimeo_vimeo_helper->supports_folders()): ?>
                            <div class="upvimeo-upgrade-notice">
                                <p><?php echo wp_kses_post(__('Missing <strong>interact</strong> scope.', 'upvimeo-direct-uploads')); ?></p>
                            </div>
                        <?php else: ?> <?php
                                        $upvimeo_current_folder_uri = empty($upvimeo_folder_uri) ? 'default' : $upvimeo_folder_uri;
                                        $upvimeo_current_folder_name = !empty($upvimeo_current_folder_uri) && ('default' != $upvimeo_current_folder_uri) ? $upvimeo_vimeo_helper->get_folder_name($upvimeo_current_folder_uri) : esc_html__('Default (no folder)', 'upvimeo-direct-uploads');
                                        ?>
                            <form id="upvimeo-video-save-folders" action="<?php echo esc_url(admin_url('admin-ajax.php')); ?>" method="post">
                                <div class="upvimeo-form-group">
                                    <select id="folder_uri" name="folder_uri" class="upvimeo-select2 upvimeo-select-full"
                                        data-action="UPVIMEO_folder_search"
                                        data-placeholder="<?php esc_attr_e('Select folder...', 'upvimeo-direct-uploads'); ?>">
                                        <option value="default" <?php selected('default', $upvimeo_current_folder_uri); ?>>
                                            <?php esc_html_e('Default (no folder)', 'upvimeo-direct-uploads'); ?>
                                        </option>
                                        <?php if (!empty($upvimeo_current_folder_uri)): ?>
                                            <option selected value="<?php echo esc_attr($upvimeo_current_folder_uri); ?>">
                                                <?php echo esc_html($upvimeo_current_folder_name); ?>
                                            </option>
                                        <?php endif; ?>
                                    </select>
                                </div>
                                <div class="upvimeo-form-actions">
                                    <input type="hidden" name="video_uri" value="<?php echo esc_attr(isset($upvimeo_vimeo['body']['uri']) ? $upvimeo_vimeo['body']['uri'] : ''); ?>">
                                    <button type="submit" class="button button-primary upvimeo-btn-block" name="action" id="save"
                                        value="1"><?php esc_html_e('Save', 'upvimeo-direct-uploads'); ?></button>
                                </div>
                            </form>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>

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

<?php endif; ?>