<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://webequipe.com
 * @since      1.0.0
 *
 * @package    Webequipe_Spin_Win_Wheel
 * @subpackage Webequipe_Spin_Win_Wheel/admin
 */

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Webequipe_Spin_Win_Wheel
 * @subpackage Webequipe_Spin_Win_Wheel/admin
 * @author     Webequipe <support@webequipe.com>
 */

	require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-webequipe-spin-win-wheel-settings.php';
	require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-webequipe-spin-win-wheel-spin-list.php';
	require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-webequipe-spin-win-wheel-spin.php';
	require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-webequipe-spin-win-wheel-winner-list.php';
	require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-webequipe-spin-win-wheel-winner.php';

class Webequipe_Spin_Win_Wheel_Admin {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Webequipe_Spin_Win_Wheel_Loader    $loader    Maintains and registers all hooks for the plugin.
	 */
	protected $loader;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */

	public $spin;
    public $settings;
    public $winner;

	public function __construct( $plugin_name, $version ) {
		$this->spin = new Webequipe_Spin_Win_Wheel_Spin();
		$this->settings = new Webequipe_Spin_Win_Wheel_Settings();
		$this->winner = new Webequipe_Spin_Win_Wheel_Winner();
		$this->plugin_name = $plugin_name;
		$this->version     = $version;
		$this->loader      = new Webequipe_Spin_Win_Wheel_Loader();
		
		add_action( 'admin_menu', [ $this, 'admin_menu' ] );
		add_action( 'admin_init', [ $this->spin, 'form_handler' ] );
		add_filter( 'parent_file', [ $this, 'set_winner_menu_parent' ] );
		add_filter( 'submenu_file', [ $this, 'set_winner_submenu_highlight' ] );
	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in webequipe_sww_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The webequipe_sww_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_style(
			$this->plugin_name . '-admin',
			plugin_dir_url(__FILE__) . 'css/webequipe-spin-win-wheel-admin.css',
			array(),
			$this->version,
			'all'
		);

		wp_enqueue_style(
			$this->plugin_name . '-add-spin',
			plugin_dir_url(__FILE__) . 'css/parts/add-spin.css',
			array(),
			$this->version,
			'all'
		);

	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in webequipe_sww_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The webequipe_sww_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		// wp_enqueue_script(
		// 	'webequipe-spin-win-wheel-admin-custom-js',
		// 	plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/webequipe-spin-win-wheel-admin.js',
		// 	array( 'jquery' ),
		// 	$this->version,
		// 	false 
		// );


	}

	public function admin_menu() {


        $parent_slug = 'wsww-spin';
        $capability  = 'manage_options';

        $hook = add_menu_page( __( 'WSWW Wheel', 'webequipe-spin-win-wheel' ), __( 'WebEquipe Spin & Win Wheel', 'webequipe-spin-win-wheel' ), $capability, $parent_slug, [ $this->spin, 'plugin_page' ], 'dashicons-wsww-spin' );

        add_submenu_page( $parent_slug, __( 'WSWW Wheel', 'webequipe-spin-win-wheel' ), __( 'All Wheel', 'webequipe-spin-win-wheel' ), $capability, $parent_slug, [ $this->spin, 'plugin_page' ] );

        $hook2 = add_submenu_page( $parent_slug, __( 'Winner List', 'webequipe-spin-win-wheel' ), __( 'Winner List', 'webequipe-spin-win-wheel' ), $capability, 'wsww-spin-winner', [ $this->winner, 'winner_page' ] );

        $hook3 = add_submenu_page( $parent_slug, __( 'Settings', 'webequipe-spin-win-wheel' ), __( 'Settings', 'webequipe-spin-win-wheel' ), $capability, 'wsww-spin-settings', [ $this->settings, 'settings_page' ] );

        add_action( 'admin_head-' . $hook, [ $this, 'enqueue_assets' ] );
        add_action( 'admin_head-' . $hook2, [ $this, 'enqueue_assets' ] );
        // Intentionally do not enqueue heavy preview assets for the Settings screen.
		add_action( 'admin_enqueue_scripts', [ $this, 'plugin_admin_css' ] );
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_settings_assets' ] );
    }

	/**
	 * Enqueue settings-page specific assets.
	 *
	 * @return void
	 */
	public function enqueue_settings_assets() {
		$page_raw = filter_input( INPUT_GET, 'page', FILTER_DEFAULT );
		$page     = is_string( $page_raw ) ? sanitize_key( wp_unslash( $page_raw ) ) : '';

		if ( 'wsww-spin-settings' !== $page ) {
			return;
		}

		$settings_css_path = plugin_dir_path( __FILE__ ) . 'css/webequipe-spin-win-wheel-settings.css';
		$settings_js_path  = plugin_dir_path( __FILE__ ) . 'js/webequipe-spin-win-wheel-settings.js';
		$settings_css_url  = plugin_dir_url( __FILE__ ) . 'css/webequipe-spin-win-wheel-settings.css';
		$settings_js_url   = plugin_dir_url( __FILE__ ) . 'js/webequipe-spin-win-wheel-settings.js';

		wp_enqueue_style(
			'webequipe-spin-win-wheel-settings',
			$settings_css_url,
			array( $this->plugin_name . '-admin' ),
			file_exists( $settings_css_path ) ? (string) filemtime( $settings_css_path ) : $this->version
		);

		wp_enqueue_script(
			'webequipe-spin-win-wheel-settings',
			$settings_js_url,
			array( 'jquery' ),
			file_exists( $settings_js_path ) ? (string) filemtime( $settings_js_path ) : $this->version,
			true
		);

		wp_localize_script(
			'webequipe-spin-win-wheel-settings',
			'wswwSettings',
			array(
				'ajaxurl'                      => admin_url( 'admin-ajax.php' ),
				'nonce'                        => wp_create_nonce( 'wsww_mailchimp_settings' ),
				'msgEnterApiKey'               => __( 'Please enter your Mailchimp API key first.', 'webequipe-spin-win-wheel' ),
				'msgConnecting'                => __( 'Connecting to Mailchimp…', 'webequipe-spin-win-wheel' ),
				'msgUnableToConnect'           => __( 'Unable to connect to Mailchimp. Please check your API key.', 'webequipe-spin-win-wheel' ),
				'msgConnected'                 => __( 'Mailchimp connected.', 'webequipe-spin-win-wheel' ),
				'msgSelectAudience'            => __( 'Select an audience', 'webequipe-spin-win-wheel' ),
				'msgConnectionFailed'          => __( 'Mailchimp connection failed. Please try again.', 'webequipe-spin-win-wheel' ),
				'msgDisconnecting'             => __( 'Disconnecting from Mailchimp…', 'webequipe-spin-win-wheel' ),
				'msgUnableToDisconnect'        => __( 'Unable to disconnect from Mailchimp. Please try again.', 'webequipe-spin-win-wheel' ),
				'msgDisconnected'              => __( 'Disconnected from Mailchimp.', 'webequipe-spin-win-wheel' ),
				'msgDisconnectFailed'          => __( 'Mailchimp disconnect failed. Please try again.', 'webequipe-spin-win-wheel' ),
			)
		);
	}

    public function enqueue_assets() {

    // WordPress media uploader (safe to leave)
    wp_enqueue_media();

    // Bootstrap CSS
		wp_enqueue_style(
			'webequipe-spin-win-wheel-bootstrap',
			plugin_dir_url( dirname( __FILE__ ) ) . 'assets/css/bootstrap.min.css',
			array(),
			$this->version,
			'all'
		);

		// Bootstrap JS
		wp_enqueue_script(
			'webequipe-spin-win-wheel-bootstrap',
			plugin_dir_url( dirname( __FILE__ ) ) . 'assets/js/bootstrap.bundle.min.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		// Admin CSS
		wp_enqueue_style(
			'webequipe-spin-win-wheel-admin',
			plugin_dir_url( __FILE__ ) . 'css/webequipe-spin-win-wheel-admin.css',
			array( 'webequipe-spin-win-wheel-bootstrap' ), // depends on your bootstrap
			$this->version
		);

		// D3.js for preview
		wp_enqueue_script(
			'webequipe-spin-win-wheel-d3',
			plugin_dir_url( dirname( __FILE__ ) ) . 'assets/js/d3.v3.min.js',
			array(),
			$this->version,
			false
		);

		// Admin JS
		wp_enqueue_script(
			'webequipe-spin-win-wheel-admin',
			plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/webequipe-spin-win-wheel-admin.js',
			array( 'jquery', 'webequipe-spin-win-wheel-bootstrap', 'webequipe-spin-win-wheel-d3' ), // depends on your bootstrap
			$this->version,
			false
		);

		// Localize script for AJAX
		wp_localize_script( 'webequipe-spin-win-wheel-admin', 'wsww_ajax_object', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'preview_nonce' => wp_create_nonce( 'wsww-preview-nonce' ),
			'assets_url' => plugin_dir_url( dirname( __FILE__ ) ) . 'assets',
			'delete_icon_url' => plugin_dir_url( dirname( __FILE__ ) ) . 'assets/img/delete.svg'
		) );
		
		// Hook to add inline scripts in admin footer
		add_action( 'admin_footer', array( $this, 'add_admin_inline_scripts' ), 20 );
	}
	
	/**
	 * Static variable to store admin inline scripts
	 *
	 * @var array
	 */
	private static $admin_inline_scripts = [];
	
	/**
	 * Add inline scripts during admin_footer hook
	 *
	 * @return void
	 */
	public function add_admin_inline_scripts() {
		if ( ! empty( self::$admin_inline_scripts ) && wp_script_is( 'webequipe-spin-win-wheel-admin', 'enqueued' ) ) {
			foreach ( self::$admin_inline_scripts as $script ) {
				wp_add_inline_script( 'webequipe-spin-win-wheel-admin', $script, 'after' );
			}
		}
	}
	
	/**
	 * Store admin inline script content to be added via wp_add_inline_script()
	 *
	 * @param string $script_content The JavaScript code to add
	 * @return void
	 */
	public static function add_admin_inline_script_content( $script_content ) {
		if ( ! empty( $script_content ) ) {
			self::$admin_inline_scripts[] = $script_content;
		}
	}

	        //style
    

	public function plugin_admin_css() {
		$icon_url = plugin_dir_url( dirname( __FILE__ ) ) . 'assets/img/cbl_logo.png';

		$inline_css = '.dashicons-wsww-spin:before {
				background-image: url("' . esc_url( $icon_url ) . '");
		}';
		
		wp_add_inline_style( $this->plugin_name . '-admin', $inline_css );
	}

	/**
	 * Ensure Winner pages stay under the plugin parent menu.
	 *
	 * @param string $parent_file Current parent file.
	 * @return string
	 */
	public function set_winner_menu_parent( $parent_file ) {
		$page_raw = filter_input( INPUT_GET, 'page', FILTER_DEFAULT );
		$page     = is_string( $page_raw ) ? sanitize_key( wp_unslash( $page_raw ) ) : '';
		if ( 'wsww-spin-winner' === $page ) {
			return 'wsww-spin';
		}

		return $parent_file;
	}

	/**
	 * Keep Winner List submenu highlighted for winner list/detail screens.
	 *
	 * @param string $submenu_file Current submenu file.
	 * @return string
	 */
	public function set_winner_submenu_highlight( $submenu_file ) {
		$page_raw = filter_input( INPUT_GET, 'page', FILTER_DEFAULT );
		$page     = is_string( $page_raw ) ? sanitize_key( wp_unslash( $page_raw ) ) : '';
		if ( 'wsww-spin-winner' === $page ) {
			return 'wsww-spin-winner';
		}

		return $submenu_file;
	}



}
