<?php

/**
 * Accordion: Import Options.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

$appearance_settings = aiopod_get_option( 'aiopod_appearance_settings' );

$rss_feed_url            = isset( $post_meta['rss_feed_url'] ) ? $post_meta['rss_feed_url'][0] : '';
$set_featured_image      = isset( $post_meta['set_featured_image'] ) ? $post_meta['set_featured_image'][0] : 1;
$update_existing_content = isset( $post_meta['update_existing_content'] ) ? $post_meta['update_existing_content'][0] : 0;
$auto_update_episodes    = isset( $post_meta['auto_update_episodes'] ) ? $post_meta['auto_update_episodes'][0] : 0;			
$auto_update_frequency   = isset( $post_meta['auto_update_frequency'] ) ? $post_meta['auto_update_frequency'][0] : 0;
$import_log              = aiopod_get_import_statistics_text( $post->ID );
?>
<div class="aiopod-form-controls">
    <div class="aiopod-notice aiopod-notice-info">
        <?php esc_html_e( 'This podcast is being imported from the RSS feed below. Use the settings below to control how and when episodes are updated.', 'aio-podcast-studio' ); ?>
    </div>
    
    <!-- Field: Imported Feed URL -->
    <div id="aiopod-form-control-rss_feed_url" class="aiopod-form-control">
        <label for="aiopod-field-rss_feed_url" class="aiopod-form-label">
            <?php esc_html_e( 'Imported Feed URL', 'aio-podcast-studio' ); ?>
        </label>
        
        <div class="aiopod-form-field">
            <input type="hidden" name="rss_feed_url" id="aiopod-field-rss_feed_url" value="<?php echo esc_url( $rss_feed_url ); ?>" />
            <a href="<?php echo esc_url( $rss_feed_url ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $rss_feed_url ); ?></a>
        </div>
    </div>

    <!-- Field: Set Featured Image? -->
    <?php if ( ! empty( $appearance_settings['enable_featured_images'] ) && ! empty( $appearance_settings['download_external_images'] ) ) : ?>
        <div id="aiopod-form-control-set_featured_image" class="aiopod-form-control">
            <label for="aiopod-field-set_featured_image" class="aiopod-form-label">
                <?php esc_html_e( 'Set Featured Image?', 'aio-podcast-studio' ); ?>
            </label>
            
            <label class="aiopod-form-field">
                <input type="checkbox" id="aiopod-field-set_featured_image" class="aiopod-sync-featured-image" value="1" <?php checked( $set_featured_image, 1 ); ?>/>
                <?php esc_html_e( 'Enable to automatically download and set featured images from the RSS feed.', 'aio-podcast-studio' ); ?>
            </label>

            <p class="description">
                <?php esc_html_e( 'Note: This may slow down the import process for podcasts with many episodes or large images.', 'aio-podcast-studio' ); ?>
            </p>
        </div>
    <?php endif; ?>

    <!-- Field: Update Existing Content? -->
    <div id="aiopod-form-control-update_existing_content" class="aiopod-form-control">
        <label for="aiopod-field-update_existing_content" class="aiopod-form-label">
            <?php esc_html_e( 'Update Existing Content?', 'aio-podcast-studio' ); ?>
        </label>
        
        <label class="aiopod-form-field">
            <input type="checkbox"name="update_existing_content" id="aiopod-field-update_existing_content" value="1" <?php checked( $update_existing_content, 1 ); ?> />
            <?php esc_html_e( 'Enable to update existing podcast and episode details from the imported feed. This may overwrite manual edits.',	'aio-podcast-studio'	); ?>
        </label>
    </div>

    <!-- Field: Auto-update Episodes? -->
    <div id="aiopod-form-control-auto_update_episodes" class="aiopod-form-control">
        <label for="aiopod-field-auto_update_episodes" class="aiopod-form-label">
            <?php esc_html_e( 'Auto-update Episodes?', 'aio-podcast-studio' ); ?>
        </label>
        
        <label class="aiopod-form-field">
            <input type="checkbox" name="auto_update_episodes" id="aiopod-field-auto_update_episodes" value="1" <?php checked( $auto_update_episodes, 1 ); ?> />
            <?php esc_html_e( 'Enable to automatically fetch new episodes from the imported feed.', 'aio-podcast-studio' ); ?>
        </label>						
    </div>

    <!-- Field: Auto-update Frequency -->
    <div id="aiopod-form-control-auto_update_frequency" class="aiopod-form-control"<?php if ( empty( $auto_update_episodes ) ) { echo ' style="display:none;"'; } ?>>
        <label for="aiopod-field-auto_update_frequency" class="aiopod-form-label">
            <?php esc_html_e( 'Auto-update Frequency', 'aio-podcast-studio' ); ?>
        </label>
        
        <div class="aiopod-form-field">
            <select name="auto_update_frequency" id="aiopod-field-auto_update_frequency" class="widefat">
                <?php
                $options = array(
                    '0'       => __( 'Only Once', 'aio-podcast-studio' ),
                    '3600'    => __( 'Every 1 Hour', 'aio-podcast-studio' ),
                    '86400'   => __( 'Every 1 Day', 'aio-podcast-studio' ),
                    '604800'  => __( 'Every 1 Week', 'aio-podcast-studio' ),
                    '2419200' => __( 'Every 1 Month', 'aio-podcast-studio' )
                );

                foreach ( $options as $key => $label ) {
                    printf( 
                        '<option value="%s"%s>%s</option>', 
                        esc_attr( $key ), 
                        selected( $key, $auto_update_frequency, false ), 
                        esc_html( $label )
                    );
                }
                ?>
            </select>
        </div>
        
        <p class="description">
            <?php esc_html_e( 'Choose how often the imported feed should be checked for new episodes.', 'aio-podcast-studio' ); ?>
        </p>
    </div>
    
    <!-- Button: Check & Import New Episodes -->
    <div id="aiopod-form-control-import_podcast" class="aiopod-form-control">
        <div class="aiopod-form-field">
            <button type="button" id="aiopod-button-import_podcast" class="button button-primary button-hero">
                <?php esc_html_e( 'Check for New Episodes', 'aio-podcast-studio' ); ?>
            </button>
        </div>
        <div class="aiopod-form-status" aria-live="polite" role="status"></div>
    </div>

    <!-- Field: Import Statistics -->
    <div id="aiopod-form-control-import_statistics" class="aiopod-form-control">
        <label for="aiopod-field-import_statistics" class="aiopod-form-label">
            <?php esc_html_e( 'Import Statistics', 'aio-podcast-studio' ); ?>
        </label>

        <div class="aiopod-form-field">
            <pre id="aiopod-field-import_statistics"><?php echo esc_textarea( $import_log ); ?></pre>
        </div>
    </div>
</div>
