<?php
/**
 * Core plugin class.
 *
 * @package MoxIcon
 */

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

class MoxIcon {

	private static $instance = null;

	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		$this->load_dependencies();
		$this->init_hooks();
	}

	private function load_dependencies() {
		$files = array(
			'includes/libraries/abstract-mox-icon-library.php',
			'includes/libraries/class-mox-heroicons.php',
			'includes/libraries/class-mox-mdi.php',
			'includes/libraries/class-mox-simplelineicons.php',
			'includes/libraries/class-mox-svg-icon-library.php',
			'includes/class-mox-icon-admin.php',
		);

		foreach ( $files as $file ) {
			$path = MOX_ICON_PATH . $file;
			if ( ! file_exists( $path ) ) {
				if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
					// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
					error_log( 'Mox Icons: Missing required file — ' . $path );
				}
				throw new \RuntimeException(
					'Mox Icons: Missing required plugin file. Please reinstall the plugin.'
				);
			}
			require_once $path;
		}
	}

	private function init_hooks() {
		add_filter( 'elementor/icons_manager/additional_tabs', array( $this, 'register_icon_libraries' ) );
		add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'enqueue_editor_icon_picker_styles' ) );
		new MoxIcon_Admin();
	}

	/**
	 * Enqueue editor-only adjustments for the Elementor icon library list.
	 */
	public function enqueue_editor_icon_picker_styles() {
		wp_enqueue_style(
			'mox-icon-picker',
			MOX_ICON_URL . 'assets/css/mox-icon-picker.css',
			array(),
			MOX_ICON_VERSION
		);
	}

	/**
	 * Register icon libraries with Elementor.
	 */
	public function register_icon_libraries( $tabs ) {
		$settings = get_option( 'mox_icon_settings', array() );

		$libraries = array(
			'heroicons_outline' => new MoxIcon_Heroicons( 'outline' ),
			'heroicons_solid'   => new MoxIcon_Heroicons( 'solid' ),
			'mdi'               => new MoxIcon_MDI(),
			'simplelineicons'   => new MoxIcon_SimpleLineIcons(),
		);

		foreach ( self::get_library_definitions() as $key => $definition ) {
			if ( empty( $definition['svg_library'] ) ) {
				continue;
			}
			$libraries[ $key ] = new MoxIcon_SVG_Icon_Library( $definition );
		}

		foreach ( $libraries as $key => $library ) {
			// Default to enabled if setting not set
			$enabled = isset( $settings[ $key ] ) ? (bool) $settings[ $key ] : true;
			if ( $enabled ) {
				$tabs[ $library->get_name() ] = $library->get_config();
			}
		}

		return $tabs;
	}

	/**
	 * Get all library definitions used by Elementor registration and admin UI.
	 *
	 * @return array
	 */
	public static function get_library_definitions() {
		return array(
			'heroicons_outline' => array(
				'label'          => __( 'Heroicons Outline', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Clean stroke-based icons by Tailwind Labs.', 'mox-icons-pack-for-elementor' ),
				'count'          => 324,
				'license'        => 'MIT',
				'credit'         => 'https://heroicons.com',
				'version'        => '2.2.0',
				'elementor_name' => 'moxicon-heroicons-outline',
			),
			'heroicons_solid'   => array(
				'label'          => __( 'Heroicons Solid', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Filled variant of Heroicons by Tailwind Labs.', 'mox-icons-pack-for-elementor' ),
				'count'          => 324,
				'license'        => 'MIT',
				'credit'         => 'https://heroicons.com',
				'version'        => '2.2.0',
				'elementor_name' => 'moxicon-heroicons-solid',
			),
			'mdi'               => array(
				'label'          => __( 'Material Design Icons', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Community-maintained icon library by Pictogrammers.', 'mox-icons-pack-for-elementor' ),
				'count'          => 7448,
				'license'        => 'Apache 2.0',
				'credit'         => 'https://pictogrammers.com/library/mdi/',
				'version'        => '7.4.47',
				'elementor_name' => 'moxicon-mdi',
			),
			'simplelineicons'   => array(
				'label'          => __( 'Simple Line Icons', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Simple and minimal line icons.', 'mox-icons-pack-for-elementor' ),
				'count'          => 189,
				'license'        => 'MIT',
				'credit'         => 'https://simplelineicons.github.io/',
				'version'        => '2.5.5',
				'elementor_name' => 'moxicon-simplelineicons',
			),
			'lucide'            => array(
				'label'          => __( 'Lucide', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Beautiful and consistent open-source stroke icons.', 'mox-icons-pack-for-elementor' ),
				'count'          => 1962,
				'license'        => 'ISC',
				'credit'         => 'https://lucide.dev/icons/',
				'version'        => '1.17.0',
				'elementor_name' => 'moxicon-lucide',
				'svg_library'    => true,
				'prefix'         => 'mox-lucide',
				'css'            => 'assets/libs/lucide/lucide.css',
				'json'           => 'assets/libs/lucide/icons.json',
				'label_icon'     => 'mox-lucide mox-lucide-home',
			),
			'tabler'            => array(
				'label'          => __( 'Tabler Icons', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Pixel-perfect outline icons for interfaces and dashboards.', 'mox-icons-pack-for-elementor' ),
				'count'          => 5093,
				'license'        => 'MIT',
				'credit'         => 'https://tabler.io/icons',
				'version'        => '3.44.0',
				'elementor_name' => 'moxicon-tabler',
				'svg_library'    => true,
				'prefix'         => 'mox-tabler',
				'css'            => 'assets/libs/tabler/tabler-icons.css',
				'json'           => 'assets/libs/tabler/icons.json',
				'label_icon'     => 'mox-tabler mox-tabler-home',
			),
			'phosphor'          => array(
				'label'          => __( 'Phosphor Icons', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Flexible regular-weight icons with a friendly visual style.', 'mox-icons-pack-for-elementor' ),
				'count'          => 1512,
				'license'        => 'MIT',
				'credit'         => 'https://phosphoricons.com/',
				'version'        => '2.1.1',
				'elementor_name' => 'moxicon-phosphor',
				'svg_library'    => true,
				'prefix'         => 'mox-phosphor',
				'css'            => 'assets/libs/phosphor/phosphor-icons.css',
				'json'           => 'assets/libs/phosphor/icons.json',
				'label_icon'     => 'mox-phosphor mox-phosphor-house',
			),
			'bootstrap_icons'   => array(
				'label'          => __( 'Bootstrap Icons', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Official open-source icon set from the Bootstrap team.', 'mox-icons-pack-for-elementor' ),
				'count'          => 2078,
				'license'        => 'MIT',
				'credit'         => 'https://icons.getbootstrap.com/',
				'version'        => '1.13.1',
				'elementor_name' => 'moxicon-bootstrap-icons',
				'svg_library'    => true,
				'prefix'         => 'mox-bootstrap',
				'css'            => 'assets/libs/bootstrap-icons/bootstrap-icons.css',
				'json'           => 'assets/libs/bootstrap-icons/icons.json',
				'label_icon'     => 'mox-bootstrap mox-bootstrap-house',
			),
			'iconoir'           => array(
				'label'          => __( 'Iconoir', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Clean, modern open-source icons for product interfaces.', 'mox-icons-pack-for-elementor' ),
				'count'          => 1383,
				'license'        => 'MIT',
				'credit'         => 'https://iconoir.com/',
				'version'        => '7.11.0',
				'elementor_name' => 'moxicon-iconoir',
				'svg_library'    => true,
				'prefix'         => 'mox-iconoir',
				'css'            => 'assets/libs/iconoir/iconoir.css',
				'json'           => 'assets/libs/iconoir/icons.json',
				'label_icon'     => 'mox-iconoir mox-iconoir-home',
			),
			'feather'           => array(
				'label'          => __( 'Feather Icons', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Simple, elegant open-source stroke icons.', 'mox-icons-pack-for-elementor' ),
				'count'          => 287,
				'license'        => 'MIT',
				'credit'         => 'https://feathericons.com/',
				'version'        => '4.29.2',
				'elementor_name' => 'moxicon-feather',
				'svg_library'    => true,
				'prefix'         => 'mox-feather',
				'css'            => 'assets/libs/feather/feather-icons.css',
				'json'           => 'assets/libs/feather/icons.json',
				'label_icon'     => 'mox-feather mox-feather-home',
			),
			'fluent_color'      => array(
				'label'          => __( 'Fluent UI System Color Icons', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Colorful Fluent UI system icons by Microsoft.', 'mox-icons-pack-for-elementor' ),
				'count'          => 890,
				'license'        => 'MIT',
				'credit'         => 'https://github.com/microsoft/fluentui-system-icons',
				'version'        => '1.2.21',
				'elementor_name' => 'moxicon-fluent-color',
				'svg_library'    => true,
				'prefix'         => 'mox-fluent-color',
				'css'            => 'assets/libs/fluent-color/fluent-color.css',
				'json'           => 'assets/libs/fluent-color/icons.json',
				'label_icon'     => 'mox-fluent-color mox-fluent-color-home-24',
			),
			'pixelarticons'     => array(
				'label'          => __( 'Pixelarticons', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Pixel-perfect retro interface icons by Gerrit Halfmann.', 'mox-icons-pack-for-elementor' ),
				'count'          => 1172,
				'license'        => 'MIT',
				'credit'         => 'https://github.com/halfmage/pixelarticons',
				'version'        => '1.2.7',
				'elementor_name' => 'moxicon-pixelarticons',
				'svg_library'    => true,
				'prefix'         => 'mox-pixelarticons',
				'css'            => 'assets/libs/pixelarticons/pixelarticons.css',
				'json'           => 'assets/libs/pixelarticons/icons.json',
				'label_icon'     => 'mox-pixelarticons mox-pixelarticons-home',
			),
			'line_md'           => array(
				'label'          => __( 'Material Line Icons', 'mox-icons-pack-for-elementor' ),
				'description'    => __( 'Animated-style Material line icons by Vjacheslav Trushkin.', 'mox-icons-pack-for-elementor' ),
				'count'          => 1279,
				'license'        => 'MIT',
				'credit'         => 'https://github.com/cyberalien/line-md',
				'version'        => '1.2.16',
				'elementor_name' => 'moxicon-line-md',
				'svg_library'    => true,
				'prefix'         => 'mox-line-md',
				'css'            => 'assets/libs/line-md/line-md.css',
				'json'           => 'assets/libs/line-md/icons.json',
				'label_icon'     => 'mox-line-md mox-line-md-home',
			),
		);
	}

	/**
	 * Get default enabled settings for all libraries.
	 *
	 * @return array
	 */
	public static function get_default_settings() {
		return array_fill_keys( array_keys( self::get_library_definitions() ), true );
	}
}
