<?php
/**
 * Shortcode to display the latest weather data.
 *
 * @param array $atts Shortcode attributes.
 * @return string HTML output displaying the latest weather data.
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly 


/**
 * Function to get the custom weather icon based on the icon code.
 *
 * @param string $iconCode The code for the weather icon.
 * @return string URL for the custom weather icon.
 */




// Function to enqueue latest CSS for both frontend and admin
function wtdp_enqueue_latest_css() {
    
    // Get the file path of the CSS file
    $css_latest_path = plugin_dir_path(__FILE__) . 'includes/latest.css';

    // Use file modification time (filemtime) for cache busting, fall back to time() if the file doesn't exist
    $timestamp = file_exists($css_latest_path) ? filemtime($css_latest_path) : time();

    // Create the URL to the CSS file with a version query string
    $css_latest_url = plugin_dir_url(__FILE__) . 'includes/latest.css';

    // Enqueue the stylesheet with the timestamp for cache busting
    wp_enqueue_style('wtdp-latest-css', $css_latest_url, [], $timestamp);

    // Retrieve the JSON option and decode it
    $latest_css_options = json_decode(get_option('wtdp_latest_css', '{}'), true);





    $defaults = array(
        'wtdp-latest-container-bg-gradient-start'          => '#1e73be',
        'wtdp-latest-container-bg-gradient-end'            => '#0b328e',
        'wtdp-latest-items-bg-gradient-start'     => '#ffffff',
        'wtdp-latest-items-bg-gradient-end'       => '#f9f9f9',
        'wtdp-latest-items-text-color'            => '#000000',
        'wtdp-latest-timestamp-text-color'       => '#ffffff',
        'wtdp-latest-location-text-color'        => '#ffffff',
        'wtdp-latest-description-text-color'     => '#ffffff',
        'wtdp-latest-temp-text-color'            => '#ffffff',
        'wtdp-latest-timestamp-font-size'        => '14px',
        'wtdp-latest-location-font-size'         => '25px',
        'wtdp-latest-temp-font-size'             => '40px',
        'wtdp-latest-unit-font-size'             => '25px',
        'wtdp-latest-description-font-size'      => '15px',
        'wtdp-latest-items-font-size'             => '13px',
        'wtdp-latest-icon-size'                  => '100px',
        'wtdp-latest-container-width'            => '400px',
        'wtdp-latest-container-padding'          => '10px',
        'wtdp-latest-items-padding'               => '10px',

        // Show/Hide options
        'wtdp-latest-show-location'              => 0,
        'wtdp-latest-show-timestamp'             => 1,
        'wtdp-latest-show-temperature'           => 1,
        'wtdp-latest-show-icon'                  => 1,
        'wtdp-latest-show-description'           => 1,
        'wtdp-latest-show-humidity'              => 1,
        'wtdp-latest-show-wind-speed'            => 1,
        'wtdp-latest-show-pressure'              => 1,
        'wtdp-latest-show-sunrise'               => 1,
        'wtdp-latest-show-sunset'                => 1
    );



    // Merge defaults with options from the JSON
    $settings = array_merge($defaults, $latest_css_options);

    // Extract values from settings
    $wtdp_latest_container_bg_gradient_start = esc_attr($settings['wtdp-latest-container-bg-gradient-start']);
    $wtdp_latest_container_bg_gradient_end = esc_attr($settings['wtdp-latest-container-bg-gradient-end']);
    $wtdp_latest_item_bg_gradient_start = esc_attr($settings['wtdp-latest-items-bg-gradient-start']);
    $wtdp_latest_item_bg_gradient_end = esc_attr($settings['wtdp-latest-items-bg-gradient-end']);
    $wtdp_latest_item_text_color = esc_attr($settings['wtdp-latest-items-text-color']);
    $wtdp_latest_date_text_color = esc_attr($settings['wtdp-latest-timestamp-text-color']);
    $wtdp_latest_location_text_color = esc_attr($settings['wtdp-latest-location-text-color']);
    $wtdp_latest_description_text_color = esc_attr($settings['wtdp-latest-description-text-color']);
    $wtdp_latest_temp_text_color = esc_attr($settings['wtdp-latest-temp-text-color']);

    $wtdp_latest_date_font_size = esc_attr($settings['wtdp-latest-timestamp-font-size']);
    $wtdp_latest_location_font_size = esc_attr($settings['wtdp-latest-location-font-size']);
    $wtdp_latest_temp_font_size = esc_attr($settings['wtdp-latest-temp-font-size']);
    $wtdp_latest_unit_font_size = esc_attr($settings['wtdp-latest-unit-font-size']);
    $wtdp_latest_description_font_size = esc_attr($settings['wtdp-latest-description-font-size']);
    $wtdp_latest_item_font_size = esc_attr($settings['wtdp-latest-items-font-size']);

    $wtdp_latest_icon_size = esc_attr($settings['wtdp-latest-icon-size']);
    $wtdp_latest_container_width = esc_attr($settings['wtdp-latest-container-width']);
    $wtdp_latest_container_padding = esc_attr($settings['wtdp-latest-container-padding']);
    $wtdp_latest_item_padding = esc_attr($settings['wtdp-latest-items-padding']);

    // Show/Hide options
    global $wtdp_latest_show_location, $wtdp_latest_show_timestamp, $wtdp_latest_show_temperature, 
        $wtdp_latest_show_icon, $wtdp_latest_show_description, $wtdp_latest_show_humidity, 
        $wtdp_latest_show_wind_speed, $wtdp_latest_show_pressure, $wtdp_latest_show_sunrise_sunset;

    $wtdp_latest_show_location = isset($settings['wtdp-latest-show-location']) ? (int) $settings['wtdp-latest-show-location'] : 1;
    $wtdp_latest_show_timestamp = isset($settings['wtdp-latest-show-timestamp']) ? (int) $settings['wtdp-latest-show-timestamp'] : 1;
    $wtdp_latest_show_temperature = isset($settings['wtdp-latest-show-temperature']) ? (int) $settings['wtdp-latest-show-temperature'] : 1;
    $wtdp_latest_show_icon = isset($settings['wtdp-latest-show-icon']) ? (int) $settings['wtdp-latest-show-icon'] : 1;
    $wtdp_latest_show_description = isset($settings['wtdp-latest-show-description']) ? (int) $settings['wtdp-latest-show-description'] : 1;
    $wtdp_latest_show_humidity = isset($settings['wtdp-latest-show-humidity']) ? (int) $settings['wtdp-latest-show-humidity'] : 1;
    $wtdp_latest_show_wind_speed = isset($settings['wtdp-latest-show-wind-speed']) ? (int) $settings['wtdp-latest-show-wind-speed'] : 1;
    $wtdp_latest_show_pressure = isset($settings['wtdp-latest-show-pressure']) ? (int) $settings['wtdp-latest-show-pressure'] : 1;
    $wtdp_latest_show_sunrise_sunset = isset($settings['wtdp-latest-show-sunrise-sunset']) ? (int) $settings['wtdp-latest-show-sunrise-sunset'] : 1;

    // Output CSS variables
    $custom_css = "
        :root {
            --wtdp-latest-container-bg-gradient-start: {$wtdp_latest_container_bg_gradient_start};
            --wtdp-latest-container-bg-gradient-end: {$wtdp_latest_container_bg_gradient_end};
            --wtdp-latest-items-bg-gradient-start: {$wtdp_latest_item_bg_gradient_start};
            --wtdp-latest-items-bg-gradient-end: {$wtdp_latest_item_bg_gradient_end};
            --wtdp-latest-timestamp-text-color: {$wtdp_latest_date_text_color};
            --wtdp-latest-location-text-color: {$wtdp_latest_location_text_color};
            --wtdp-latest-description-text-color: {$wtdp_latest_description_text_color};
            --wtdp-latest-temp-text-color: {$wtdp_latest_temp_text_color};
            --wtdp-latest-items-text-color: {$wtdp_latest_item_text_color};
            --wtdp-latest-timestamp-font-size: {$wtdp_latest_date_font_size};
            --wtdp-latest-location-font-size: {$wtdp_latest_location_font_size};
            --wtdp-latest-temp-font-size: {$wtdp_latest_temp_font_size};
            --wtdp-latest-unit-font-size: {$wtdp_latest_unit_font_size};
            --wtdp-latest-description-font-size: {$wtdp_latest_description_font_size};
            --wtdp-latest-items-font-size: {$wtdp_latest_item_font_size};
            --wtdp-latest-icon-size: {$wtdp_latest_icon_size};
            --wtdp-latest-container-width: {$wtdp_latest_container_width};
            --wtdp-latest-container-padding: {$wtdp_latest_container_padding};
            --wtdp-latest-items-padding: {$wtdp_latest_item_padding};
        }

        .wtdp-latest-location { display: " . ($wtdp_latest_show_location ? 'block' : 'none') . "; }
        .wtdp-latest-timestamp { display: " . ($wtdp_latest_show_timestamp ? 'block' : 'none') . "; }
        .wtdp-latest-temperature { display: " . ($wtdp_latest_show_temperature ? 'block' : 'none') . "; }
        .wtdp-latest-icon { display: " . ($wtdp_latest_show_icon ? 'block' : 'none') . "; }
        .wtdp-latest-description { display: " . ($wtdp_latest_show_description ? 'block' : 'none') . "; }
        .wtdp-latest-humidity { display: " . ($wtdp_latest_show_humidity ? 'block' : 'none') . "; }
        .wtdp-latest-wind-speed { display: " . ($wtdp_latest_show_wind_speed ? 'block' : 'none') . "; }
        .wtdp-latest-pressure { display: " . ($wtdp_latest_show_pressure ? 'block' : 'none') . "; }
        .wtdp-latest-sunrise-sunset { display: " . ($wtdp_latest_show_sunrise_sunset ? 'block' : 'none') . "; }
    ";

    // Inject inline CSS with the correct handle
    wp_add_inline_style('wtdp-latest-css', esc_attr($custom_css));
}
add_action('wp_head', 'wtdp_enqueue_latest_css');
add_action('admin_enqueue_scripts', 'wtdp_enqueue_latest_css');








function wtdp_display_latest_weather($atts) {




    // Fetch weather data
    $weather_data = wtdp_fetch_weather_by_lat_lon();

    // Fetch translations from settings
    $label_city = sanitize_text_field(get_option('wtdp_column_city', 'City'));
    $label_temperature = sanitize_text_field(get_option('wtdp_column_temperature', 'Temperature'));
    $label_humidity = sanitize_text_field(get_option('wtdp_column_humidity', 'Humidity'));
    $label_wind_speed = sanitize_text_field(get_option('wtdp_column_wind_speed', 'Wind Speed'));
    $label_pressure = sanitize_text_field(get_option('wtdp_column_pressure', 'Pressure'));
    $label_local_time = sanitize_text_field(get_option('wtdp_column_local_time', 'Local Time'));
    $label_sunrise = sanitize_text_field(get_option('wtdp_column_sunrise', 'Sunrise'));
    $label_sunset = sanitize_text_field(get_option('wtdp_column_sunset', 'Sunset'));
    $label_description = sanitize_text_field(get_option('wtdp_column_description', 'Description'));


    // Get unit system
    $unit_system = sanitize_text_field(get_option('wtdp_unit_system', 'metric'));

    // Determine temperature unit
    $temperature_unit = ($unit_system === 'metric') ? '°C' : '°F';

    // Start output buffering
    ob_start();

    if (isset($weather_data['error'])) {
        echo "<div class='error-message'>" . esc_html($weather_data['error']) . "</div>";
    } else {
        // Extract relevant data
        $weatherDescription = sanitize_text_field($weather_data['weather'][0]['description']);
        $iconCode = sanitize_text_field($weather_data['weather'][0]['icon']);
        $iconUrl = esc_url(wtdp_get_custom_weather_icon($iconCode)); // Custom icons

        $temperature = number_format(floatval($weather_data['main']['temp']), 1);
        $humidity = intval($weather_data['main']['humidity']);
        $pressure = intval($weather_data['main']['pressure']);
        $windSpeed = floatval($weather_data['wind']['speed']);
        $country = sanitize_text_field($weather_data['sys']['country']);
        $cityName = sanitize_text_field($weather_data['name']);
        $default_city = sanitize_text_field(get_option('wtdp_default_city', 'London'));
        $timestamp = intval($weather_data['dt']);
        $sunrise = intval($weather_data['sys']['sunrise']);
        $sunset = intval($weather_data['sys']['sunset']);

        $time_difference = intval(get_option('wtdp_time_difference', 0)); // Default to 0 if not set
        $local_timestamp = esc_html(gmdate('Y-m-d H:i', $timestamp + ($time_difference * 3600))); // Convert hours to seconds
        $local_sunrise = esc_html(gmdate('H:i', $sunrise + ($time_difference * 3600))); // Convert sunrise to local time
        $local_sunset = esc_html(gmdate('H:i', $sunset + ($time_difference * 3600))); // Convert sunset to local time
        
        // Check if the description is "Rošenje" and change it to "Rosenje"
        if ($weatherDescription === "Rošenje") {
            $weatherDescription = "Rosenje";
        }

    global $wtdp_latest_show_location, $wtdp_latest_show_timestamp, $wtdp_latest_show_temperature, 
        $wtdp_latest_show_icon, $wtdp_latest_show_description, $wtdp_latest_show_humidity, 
        $wtdp_latest_show_wind_speed, $wtdp_latest_show_pressure, $wtdp_latest_show_sunrise_sunset;

        // Display output
        echo "<div class='wtdp-latest-container'>";
        echo "<div class='wtdp-latest-info'>";
        // Conditionally display the location
            echo "<div class='wtdp-latest-location'>" . esc_html($cityName) . "</div>";
        // Conditionally display the timestamp
            echo "<div class='wtdp-latest-timestamp'>" . esc_html($local_timestamp) . "</div>";
        // Conditionally display the temperature
            echo "<div class='wtdp-latest-temperature'>" . esc_html($temperature) . "<sup><span class='wtdp-latest-temp-unit'>" . esc_html($temperature_unit) . "</span></sup></div>";
        // Conditionally Display the weather icon
            echo "<div class='wtdp-latest-icon'>";
            echo "<img src='" . esc_url($iconUrl) . "' alt='" . esc_attr($weatherDescription) . "' title='" . esc_attr($weatherDescription) . "' />";
            echo "</div>"; 
        // Conditionally Display the weather description
            echo "<div class='wtdp-latest-description'>" . esc_html($weatherDescription) . "</div>"; 
            echo "</div>"; 
            echo "<div class='wtdp-latest-items'>";
        // Conditionally display humidity
            echo "<div><span class='wtdp-latest-humidity'>" . esc_html($label_humidity) . ":</span> <span class='wtdp-latest-humidity-val'>" . esc_html($humidity) . "%</span></div>";
        // Conditionally display pressure
            echo "<div><span class='wtdp-latest-pressure'>" . esc_html($label_pressure) . ":</span> <span class='wtdp-latest-pressure-val'>" . esc_html($pressure) . " hPa</span></div>";
        // Conditionally display wind speed
            echo "<div><span class='wtdp-latest-wind-speed'>" . esc_html($label_wind_speed) . ":</span> <span class='wtdp-latest-wind-speed-val'>" . esc_html($windSpeed) . " " . ($unit_system === 'metric' ? 'm/s' : 'mph') . "</span></div>";
        // Conditionally display sunrise time
            echo "<div><span class='wtdp-latest-sunrise-sunset'>" . esc_html($label_sunrise) . ":</span> <span class='wtdp-latest-sunset-val'>" . esc_html($local_sunrise) . "</span></div>";
            echo "<div><span class='wtdp-latest-sunrise-sunset'>" . esc_html($label_sunset) . ":</span> <span class='wtdp-latest-sunrise-val'>" . esc_html($local_sunset) . "</span></div>";
        echo "</div>"; 
        echo "</div>"; 

    }

    return ob_get_clean(); // Return the buffered content
}


add_shortcode('wtdp_display_latest', 'wtdp_display_latest_weather');
