<?php

if (!defined('ABSPATH')) {
    exit;
}

// Enqueue scripts and styles
add_action('wp_enqueue_scripts', 'bibldare_bible_schedule_enqueue_scripts');

function bibldare_bible_schedule_enqueue_scripts()
{
    wp_enqueue_style(
        'bible-schedule-style',
        plugin_dir_url(BIBLDARE_PLUGIN_PATH) . 'assets/css/bible-schedule.css',
        [],
        '1.0.0'
    );

    wp_localize_script('bible-schedule-script', 'bibldareBibleScheduleAjax', [
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('bibldare_bible_schedule_nonce')
    ]);
}

// Enqueue admin scripts
add_action('admin_enqueue_scripts', 'bibldare_enqueue_admin_scripts');


function bibldare_enqueue_admin_scripts($hook)
{
    if ($hook !== 'tools_page_bible-daily-reading-plan') {
        return;
    }

    wp_enqueue_script(
        'bibldare-admin-settings',
        plugin_dir_url(BIBLDARE_PLUGIN_PATH) . 'assets/js/admin-settings.js',
        [],
        '1.0.0',
        true
    );

    // Enqueue WordPress color picker scripts and styles
    wp_enqueue_style('wp-color-picker');
    wp_enqueue_script('wp-color-picker');
}

// Enqueue shortcode scripts
add_action('wp_enqueue_scripts', function () {
    wp_enqueue_script('bibldare-bible-reading-plan', plugin_dir_url(__FILE__) . '../assets/js/bible-reading-plan.js', ['jquery'], '1.2.0', true);

    $custom_background_color = get_option('bibldare_bible_custom_background_color', '');
    $text_color = get_option('bibldare_bible_text_color', '');
    $timezone = get_option('bibldare_bible_timezone', 'Asia/Tbilisi');

    wp_localize_script('bibldare-bible-reading-plan', 'bibldareData', [
        'ajaxUrl' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('bibldare_bible_schedule_nonce'),
        'serverToday' => bibldare_get_today_index($timezone),
        'custom_background_color' => $custom_background_color,
        'text_color' => $text_color,
        'lang' => get_option('bibldare_bible_language', 'eng'),
        'version' => get_option('bibldare_bible_version', DEFAULT_VERSION),
        'background' => get_option('bibldare_bible_background', 'white'),
        'minDay' => 1,
        'maxDay' => 365
    ]);
});

add_filter('wpseo_opengraph_image_id', '__return_false', 999);

add_filter('wpseo_opengraph_image', 'bibldare_dynamic_daily_og_image', 999);

function bibldare_dynamic_daily_og_image($current_img)
{
    if (is_front_page() || is_home()) {

        $timezone = get_option('bibldare_bible_timezone', 'Asia/Tbilisi');
        $date = new DateTime("now", new DateTimeZone($timezone));
        $today_string = $date->format('d');

        $image_name = "og-{$today_string}.jpg";

        $plugin_root_path = dirname(plugin_dir_path(__FILE__));
        $image_path = $plugin_root_path . '/assets/images/og/' . $image_name;

        $plugin_root_url = plugin_dir_url($plugin_root_path . '/index.php');
        $image_url = $plugin_root_url . 'assets/images/og/' . $image_name;

        if (file_exists($image_path)) {
            return $image_url . '?v=' . time();
        }
    }

    return $current_img;
}