<?php
namespace TotalTheme;

defined( 'ABSPATH' ) || exit;

/**
 * Theme Icon methods.
 *
 * @package Total WordPress theme
 * @subpackage Classes
 * @version 5.5
 */
class Theme_Icons {

	/**
	 * Font icon stylesheet name.
	 */
	const STYLE_HANDLE = 'ticons';

	/**
	 * Transient name for getting icons list.
	 */
	const TRANSIENT = 'wpex_theme_icons_list';

	/**
	 * Holds array of icons.
	 */
	protected static $icons_list = null;

	/**
	 * Checks if theme icons are enabled.
	 */
	public static function is_enabled() {
		return apply_filters( 'wpex_ticons_enable', true );
	}

	/**
	 * Returns Theme Icons CSS file.
	 */
	public static function get_css_url() {
		return wpex_asset_url( 'lib/ticons/css/ticons.min.css' );
	}

	/**
	 * Returns a json list of all Theme Icons.
	 */
	public static function get_json_url() {
		return wpex_asset_url( 'lib/ticons/json/ticons.json' );
	}

	/**
	 * Registers the theme icon font stylesheet.
	 */
	public static function register_font_style() {
		wp_register_style(
			self::STYLE_HANDLE,
			self::get_css_url(),
			array(),
			WPEX_THEME_VERSION
		);
	}

	/**
	 * Enqueues theme icon font stylesheet.
	 */
	public static function enqueue_font_style() {
		wp_enqueue_style( self::STYLE_HANDLE );
	}

	/**
	 * Returns list of all available theme icons.
	 */
	public static function get_icons_list() {
		if ( ! is_null( self::$icons_list ) ) {
			return self::$icons_list;
		}

		$icons      = [ 'none' ];
		$icon_files = get_transient( self::TRANSIENT );
	//	$icon_files = [];

		if ( ! $icon_files ) {
			$icon_files = self::list_files();
		}

		set_transient( self::TRANSIENT, $icon_files, MONTH_IN_SECONDS );

		if ( $icon_files ) {
			$icons = array_merge( $icons, $icon_files );
		}

		// Set keys equal to vars.
		$icons = array_combine( $icons, $icons );

		// Old Filter @todo deprecate
		$icons = apply_filters( 'wpex_get_awesome_icons', $icons );

		/**
		 * Filters the list of theme icons.
		 *
		 * @param array $icons
		 */
		self::$icons_list = (array) apply_filters( 'wpex_ticons_list', $icons );

		return self::$icons_list;
	}

	/**
	 * Returns the preferred format for theme icons.
	 */
	public static function get_format() {
		$format = 'font';

		if ( get_theme_mod( 'svg_icons_enable', false ) ) {
			$format = 'svg';
		}

		/**
		 * Filters the theme icon format (font|svg).
		 *
		 * @param string $format
		 */
		$format = (string) apply_filters( 'wpex_theme_icon_format', $format );

		return $format;
	}

	/**
	 * Returns icon.
	 */
	public static function get_icon( $icon = '', $extra_class = '' ) {
		if ( ! is_string( $icon ) || null === $icon || '' === trim( $icon ) || 'ticon ticon-none' === $icon || 'none' === $icon ) {
			return;
		}

		$html       = '';
		$icon_attrs = array();
		$format     = self::get_format();
		$is_ticon   = true;

		if ( strpos( $icon, '/' ) && ! str_starts_with( $icon, 'ticon' ) ) {
			$is_ticon = false;
			$format = 'svg';
		} else {
			$icon = str_replace( 'ticons/', '', $icon );
		}

		// Add svg class.
		if ( 'svg' === $format ) {
			$icon_attrs['class'] = array( 'wpex-svg-icon' );
		}

		// Add ticon prefix and ticon class.
		else {
			if ( is_string( $icon ) && str_starts_with( $icon, 'ticon' ) ) {
				$class = explode( ' ', $icon );
			} else {
				$class = array( 'ticon' );
				$class[] = 'ticon-' . sanitize_html_class( $icon );
			}
			$icon_attrs['class'] = $class;
		}

		$icon_attrs['aria-hidden'] = 'true';

		if ( $extra_class ) {
			if ( is_array( $extra_class ) ) {
				$extra_classes = $extra_class['class'] ?? null;
				unset( $extra_class['class'] );
				$icon_attrs = wp_parse_args( $extra_class, $icon_attrs );
				array_unshift( $icon_attrs['class'], $extra_classes );
			} else {
				array_unshift( $icon_attrs['class'], $extra_class );
			}
		}

		switch ( $format ) {
			case 'svg':
				$svg_path = $icon;
				if ( $is_ticon ) {
					$icon = str_replace( 'ticon-', '', $icon );
					$icon = str_replace( 'ticon', '', $icon );
					$svg_path = 'ticons/' . trim( $icon );
				}
				$get_svg = wpex_get_svg( $svg_path );
				if ( $get_svg ) {
					$html = '<span ' . trim( wpex_parse_attrs( $icon_attrs ) ) . '>' . $get_svg . '</span>';
				}
				break;
			case 'font':
			default:
				self::enqueue_font_style();
				$html = '<span ' . trim( wpex_parse_attrs( $icon_attrs ) ) . '></span>';
				break;
		}

		/**
		 * Filters the theme icon html.
		 *
		 * @param string $html
		 * @param $icon
		 * @param $extra_class
		 */
		$html = (string) apply_filters( 'wpex_theme_icon_html', $html, $icon, $extra_class );

		return $html;
	}

	/**
	 * Renders an icon.
	 */
	public static function render_icon( $icon = '', $extra_class = '' ) {
		echo self::get_icon( $icon, $extra_class );
	}

	/**
	 * Returns a list of all icon file names inside theme directory.
	 */
	protected static function list_files() {
		$folder = trailingslashit( get_template_directory() ) . 'assets/svgs/ticons/';
		$files  = array();
		$dir    = @opendir( $folder );
		if ( $dir ) {
			while ( ( $file = readdir( $dir ) ) !== false ) {
				// Skip current and parent folder links.
				if ( in_array( $file, array( '.', '..' ), true ) ) {
					continue;
				}
				// Skip hidden files.
				if ( '.' === $file[0] ) {
					continue;
				}
				$files[] = pathinfo( $file, PATHINFO_FILENAME); // return file name withotu extension
			}
			closedir( $dir );
		}
		return $files;
	}

	/**
	 * Generates a json array of the theme icons
	 */
	protected static function generate_json_list() {
		$iconList = [];
		$icon_files = self::list_files();
		foreach ( $icon_files as $icon_file ) {
			$iconList[] = $icon_file;
		}
		sort( $iconList );
		$icons = new \stdClass();
		$icons->icons = $iconList;
		return json_encode( $icons );
	}

	/**
	 * Deletes the list transient.
	 */
	public static function delete_transient() {
		delete_transient( self::TRANSIENT );
	}

}