<?php
/**
 * Icon loader + helpers for MultiWeather.
 */

defined('ABSPATH') || exit;

/**
 * Prefixed Erik Flowers / OpenWeather icon mapper.
 *
 * @param string|int $code OpenWeather icon code (e.g. "01d" or 800) or event key.
 * @return string Erik Flowers CSS class.
 */
if ( ! function_exists( 'mwpl_erikflowers_icon_map' ) ) {
	function mwpl_erikflowers_icon_map( $code ) {
		$map = array(
			// OWM Icon Codes
			'01d' => 'wi-day-sunny',
			'01n' => 'wi-night-clear',
			'02d' => 'wi-day-cloudy',
			'02n' => 'wi-night-alt-cloudy',
			'03d' => 'wi-cloud',
			'03n' => 'wi-cloud',
			'04d' => 'wi-cloudy',
			'04n' => 'wi-cloudy',
			'09d' => 'wi-showers',
			'09n' => 'wi-showers',
			'10d' => 'wi-day-rain',
			'10n' => 'wi-night-alt-rain',
			'11d' => 'wi-thunderstorm',
			'11n' => 'wi-night-alt-thunderstorm',
			'13d' => 'wi-snow',
			'13n' => 'wi-snow',
			'50d' => 'wi-fog',
			'50n' => 'wi-fog',

			// OWM Numeric Weather Codes
			200 => 'wi-owm-200',
			201 => 'wi-owm-201',
			202 => 'wi-owm-202',
			210 => 'wi-owm-210',
			211 => 'wi-owm-211',
			212 => 'wi-owm-212',
			221 => 'wi-owm-221',
			230 => 'wi-owm-230',
			231 => 'wi-owm-231',
			232 => 'wi-owm-232',
			300 => 'wi-owm-300',
			301 => 'wi-owm-301',
			302 => 'wi-owm-302',
			310 => 'wi-owm-310',
			311 => 'wi-owm-311',
			312 => 'wi-owm-312',
			313 => 'wi-owm-313',
			314 => 'wi-owm-314',
			321 => 'wi-owm-321',
			500 => 'wi-owm-500',
			501 => 'wi-owm-501',
			502 => 'wi-owm-502',
			503 => 'wi-owm-503',
			504 => 'wi-owm-504',
			511 => 'wi-owm-511',
			520 => 'wi-owm-520',
			521 => 'wi-owm-521',
			522 => 'wi-owm-522',
			531 => 'wi-owm-531',
			600 => 'wi-owm-600',
			601 => 'wi-owm-601',
			602 => 'wi-owm-602',
			611 => 'wi-owm-611',
			612 => 'wi-owm-612',
			615 => 'wi-owm-615',
			616 => 'wi-owm-616',
			620 => 'wi-owm-620',
			621 => 'wi-owm-621',
			622 => 'wi-owm-622',
			701 => 'wi-owm-701',
			711 => 'wi-owm-711',
			721 => 'wi-owm-721',
			731 => 'wi-owm-731',
			741 => 'wi-owm-741',
			751 => 'wi-owm-751',
			761 => 'wi-owm-761',
			762 => 'wi-owm-762',
			771 => 'wi-owm-771',
			781 => 'wi-owm-781',
			800 => 'wi-owm-800',
			801 => 'wi-owm-801',
			802 => 'wi-owm-802',
			803 => 'wi-owm-803',
			804 => 'wi-owm-804',
			900 => 'wi-tornado',
			901 => 'wi-storm-showers',
			902 => 'wi-hurricane',
			903 => 'wi-snowflake-cold',
			904 => 'wi-hot',
			905 => 'wi-windy',
			906 => 'wi-hail',

			// Sun & Moon Events (in case you use these keys elsewhere)
			'sunrise'         => 'wi-sunrise',
			'sunset'          => 'wi-sunset',
			'moonrise'        => 'wi-moonrise',
			'moonset'         => 'wi-moonset',
			'new_moon'        => 'wi-moon-new',
			'full_moon'       => 'wi-moon-full',
			'first_quarter'   => 'wi-moon-first-quarter',
			'third_quarter'   => 'wi-moon-third-quarter',
			'waning_crescent' => 'wi-moon-waning-crescent',
			'waxing_crescent' => 'wi-moon-waxing-crescent',
			'waning_gibbous'  => 'wi-moon-waning-gibbous',
			'waxing_gibbous'  => 'wi-moon-waxing-gibbous',
		);

		return isset( $map[ $code ] ) ? $map[ $code ] : 'wi-na';
	}
}

/**
 * Backwards-compat public mapper used in templates:
 *   mw_erikflowers_icon_map( $icon )
 *
 * PHPCS will complain about the name, so we silence it here.
 */
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
if ( ! function_exists( 'mw_erikflowers_icon_map' ) ) {
	function mw_erikflowers_icon_map( $code ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
		return mwpl_erikflowers_icon_map( $code );
	}
}

/**
 * Prefixed icon URL / HTML resolver.
 *
 * NOTE: For the "erikflowers" pack this returns the full <i> tag HTML,
 * because that's how your original mw_get_icon_url behaved.
 *
 * @param string|int $icon_code
 * @param string     $pack      Icon pack slug, e.g. "vclouds-v2" or "erikflowers".
 * @return string URL or HTML, depending on pack.
 */
if ( ! function_exists( 'mwpl_get_icon_url' ) ) {
	function mwpl_get_icon_url( $icon_code, $pack = 'vclouds-v2' ) {
		// Erik Flowers is class-based, so we return HTML.
		if ( 'erikflowers' === $pack ) {
			$class = mwpl_erikflowers_icon_map( $icon_code );

			return '<i class="wi ' . esc_attr( $class ) . '"></i>';
		}

		// Image-based pack: build URL relative to plugin root.
		$relative_path   = 'assets/icons/' . $pack . '/' . $icon_code . '.png';
		$plugin_root_url = plugin_dir_url( __DIR__ );

		return $plugin_root_url . $relative_path;
	}
}

/**
 * Backwards-compat public mapper used in templates:
 *   mw_erikflowers_icon_map( $icon )
 *
 * PHPCS will complain about the name, so we silence it here.
 */
if ( ! function_exists( 'mw_erikflowers_icon_map' ) ) {
	function mw_erikflowers_icon_map( $code ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
		return mwpl_erikflowers_icon_map( $code );
	}
}

/**
 * Backwards-compat wrapper for templates still calling:
 *   mw_get_icon_url( $icon_code, $pack )
 */
if ( ! function_exists( 'mw_get_icon_url' ) ) {
	function mw_get_icon_url( $icon_code, $pack = 'vclouds-v2' ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
		return mwpl_get_icon_url( $icon_code, $pack );
	}
}

