<?php
/**
 * Admin settings page handler.
 *
 * @package MoxIcon
 */

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

class MoxIcon_Admin {

	public function __construct() {
		add_action( 'admin_menu', array( $this, 'add_menu_page' ) );
		add_action( 'admin_init', array( $this, 'register_settings' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
		add_filter( 'plugin_action_links_' . MOX_ICON_BASENAME, array( $this, 'add_action_links' ) );
	}

	public function add_menu_page() {
		add_menu_page(
			__( 'Mox Icons Settings', 'mox-icons-pack-for-elementor' ),
			__( 'Mox Icons', 'mox-icons-pack-for-elementor' ),
			'manage_options',
			'mox-icon-settings',
			array( $this, 'render_settings_page' ),
			MOX_ICON_URL . 'assets/images/mox-icons-menu-icon.svg',
			80 // Position
		);
	}

	public function register_settings() {
		register_setting(
			'mox_icon_settings_group',
			'mox_icon_settings',
			array(
				'sanitize_callback' => array( $this, 'sanitize_settings' ),
				'default'           => MoxIcon::get_default_settings(),
			)
		);
	}

	public function sanitize_settings( $input ) {
		$keys = array_keys( MoxIcon::get_library_definitions() );
		$sanitized = array();
		foreach ( $keys as $key ) {
			$sanitized[ $key ] = ! empty( $input[ $key ] );
		}
		return $sanitized;
	}

	public function enqueue_admin_assets( $hook ) {
		wp_enqueue_style(
			'mox-icon-admin-menu',
			MOX_ICON_URL . 'admin/css/mox-icon-admin-menu.css',
			array(),
			MOX_ICON_VERSION
		);

		if ( 'toplevel_page_mox-icon-settings' !== $hook ) {
			return;
		}
		wp_enqueue_style(
			'mox-icon-admin',
			MOX_ICON_URL . 'admin/css/mox-icon-admin.css',
			array(),
			MOX_ICON_VERSION
		);
		wp_enqueue_script(
			'mox-icon-admin',
			MOX_ICON_URL . 'admin/js/mox-icon-admin.js',
			array( 'jquery' ),
			MOX_ICON_VERSION,
			true
		);
		wp_localize_script(
			'mox-icon-admin',
			'moxIconAdmin',
			array(
				'activeLabel'   => __( 'Active', 'mox-icons-pack-for-elementor' ),
				'inactiveLabel' => __( 'Inactive', 'mox-icons-pack-for-elementor' ),
			)
		);
	}

	public function add_action_links( $links ) {
		$settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=mox-icon-settings' ) ) . '">'
			. esc_html__( 'Settings', 'mox-icons-pack-for-elementor' ) . '</a>';
		array_unshift( $links, $settings_link );
		return $links;
	}

	public function render_settings_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		require_once MOX_ICON_PATH . 'admin/partials/mox-icon-admin-display.php';
	}
}
