<?php
namespace BwWinner;

class Admin_Pages {
	public $csv_import_hook;
	
	public function __construct() {
		add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
	}

	public function add_admin_menu() {
		add_menu_page(
			esc_html__( 'Winners Posts', 'bw-winner' ),
			esc_html__( 'Winners', 'bw-winner' ),
			'manage_options',
			'bw-winner',
			'',
			'dashicons-awards',
			25
		);

		$this->csv_import_hook = add_submenu_page(
			'bw-winner',
			esc_html__( 'Winners CSV Import', 'bw-winner' ),
			esc_html__( 'CSV Import', 'bw-winner' ),
			'manage_options',
			'bw-winner',
			array( $this, 'page_content' ),
			0
		);

		add_action( 'admin_enqueue_scripts', array( $this, 'menu_page_assets' ) );
	}

	public function page_content() {
		if ( ! current_user_can( 'manage_options' ) || ! current_user_can( 'publish_posts' ) ) {
			wp_die( esc_html__( 'You do not have sufficient capabilities to access this page.', 'bw-winner' ) );
		}
		?>
			<div class="wrap">
				<div id="bw-menu-page-app"></div>
			</div>
		<?php
	}

	public function menu_page_assets( $hook ) {
		if ( $hook === $this->csv_import_hook ) {
			wp_enqueue_media();
			$script_settings = include PLUGIN_PATH . 'build/admin-pages/csv-import/index.asset.php';
			wp_enqueue_script(
				'bw-winner-admin-page-csv-import',
				PLUGIN_URL . 'build/admin-pages/csv-import/index.js',
				array_merge( $script_settings['dependencies'], array( 'wp-api' ) ),
				$script_settings['version'],
				true
			);
			wp_enqueue_style(
				'bw-winner-admin-page-csv-import',
				PLUGIN_URL . 'build/admin-pages/csv-import/index.css',
				array(),
				$script_settings['version']
			);

			$options    = \BwWinner\Options\get_options();
			$medal_urls = array();
			if ( ! empty( $options['medals'] ) && is_array( $options['medals'] ) ) {
				foreach ( $options['medals'] as $tier => $attachment_id ) {
					$attachment_id = (int) $attachment_id;
					$medal_urls[ $tier ] = $attachment_id
						? wp_get_attachment_image_url( $attachment_id, 'medium' )
						: '';
				}
			}
			$options['medalUrls'] = $medal_urls;

			$medal_urls_by_year = array();
			if ( ! empty( $options['medalsByYear'] ) && is_array( $options['medalsByYear'] ) ) {
				foreach ( $options['medalsByYear'] as $year => $tiers ) {
					if ( ! is_array( $tiers ) ) continue;
					$year_urls = array();
					foreach ( $tiers as $tier => $attachment_id ) {
						$attachment_id = (int) $attachment_id;
						$year_urls[ $tier ] = $attachment_id
							? wp_get_attachment_image_url( $attachment_id, 'medium' )
							: '';
					}
					$medal_urls_by_year[ (string) $year ] = $year_urls;
				}
			}
			$options['medalUrlsByYear'] = $medal_urls_by_year;

			// Force empty year maps to encode as JSON object {} (not []) so the
			// React client always sees a plain object and lodash can't convert
			// later inserts at numeric-string keys into a sparse array.
			if ( empty( $options['medalsByYear'] ) ) {
				$options['medalsByYear'] = new \stdClass();
			}
			if ( empty( $options['medalUrlsByYear'] ) ) {
				$options['medalUrlsByYear'] = new \stdClass();
			}

			wp_localize_script( 'bw-winner-admin-page-csv-import', 'bwWinnersOptions', $options );
		}
	}
}
