<?php

function wtdp_get_custom_weather_icon($iconCode) {
    $icon_map = array(
        '01d' => 'clear-day',        // Clear sky (day)
        '01n' => 'clear-night',      // Clear sky (night)
        '02d' => 'cloudy-2-day',     // Few clouds (day)
        '02n' => 'cloudy-2-night',    // Few clouds (night)
        '03d' => 'cloudy-3-day',      // Scattered clouds (day)
        '03n' => 'cloudy-3-night',     // Scattered clouds (night)
        '04d' => 'cloudy',           // Broken clouds (day)
        '04n' => 'cloudy',           // Broken clouds (night)
        '09d' => 'rainy-3-day',      // Shower rain (day)
        '09n' => 'rainy-3-night',     // Shower rain (night)
        '10d' => 'rainy-2-day',      // Rain (day)
        '10n' => 'rainy-2-night',     // Rain (night)
        '11d' => 'severe-thunderstorm', // Thunderstorm (day)
        '11n' => 'severe-thunderstorm', // Thunderstorm (night)
        '13d' => 'snowy-3-day',      // Snow (day)
        '13n' => 'snowy-3-night',     // Snow (night)
        '50d' => 'fog-day',          // Mist (day)
        '50n' => 'fog-night',        // Mist (night)
    );

    // Default to 'UNKNOWN' if the icon code is not mapped
    $base_name = isset($icon_map[$iconCode]) ? $icon_map[$iconCode] : 'UNKNOWN';

    // Build icon file path
    $icon_path = plugins_url("includes/weather-icons/animated/{$base_name}.svg", __FILE__);

    return $icon_path;
}


function wtdp_fetch_weather_by_lat_lon() {
    // Get JSON options
    $general_settings = json_decode(get_option('wtdp_general', '{}'), true);

    // Retrieve settings
    $api_key = isset($general_settings['wtdp_api_key']) ? $general_settings['wtdp_api_key'] : '';
    $latitude = isset($general_settings['wtdp_latitude']) ? $general_settings['wtdp_latitude'] : '';
    $longitude = isset($general_settings['wtdp_longitude']) ? $general_settings['wtdp_longitude'] : '';
    $units = isset($general_settings['wtdp_unit_system']) ? $general_settings['wtdp_unit_system'] : 'metric';
    $language = isset($general_settings['wtdp_language']) ? $general_settings['wtdp_language'] : 'en';


    // Validate API key, latitude, and longitude
    if (empty($api_key)) {
        return array('error' => __('API key is not set. Please configure it in the plugin settings.', 'extended-weather'));
    }

    if (empty($latitude) || empty($longitude)) {
        return array('error' => __('Latitude or Longitude is not set. Please configure it in the plugin settings.', 'extended-weather'));
    }

    // Validate latitude and longitude to be numeric
    if (!is_numeric($latitude) || !is_numeric($longitude)) {
        return array('error' => __('Invalid Latitude or Longitude values.', 'extended-weather'));
    }

    // Construct the API request URL
    $url = "https://api.openweathermap.org/data/2.5/weather?lat=" . urlencode($latitude) . "&lon=" . urlencode($longitude) . "&units=" . urlencode($units) . "&appid=" . urlencode($api_key) . "&lang=" . urlencode($language);

    // Make the request using wp_remote_get()
    $response = wp_remote_get($url);

    // Check for errors in the response
    if (is_wp_error($response)) {
        // translators: %s is the error message returned from the API request
        return array('error' => sprintf(__('Error making API request: %s', 'extended-weather'), $response->get_error_message()));
    }

    // Retrieve the response body and decode it
    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);

    if (isset($data['cod']) && $data['cod'] == 200) {
        return $data;
    } else {
        return array('error' => __('Error fetching weather data.', 'extended-weather'));
    }
}




?>