<?php

namespace ACP\Export;

use AC;
use AC\Asset\Location;
use AC\Registrable;
use ACP\Export\Asset\Script;

class TableScreen implements Registrable {

	/**
	 * @var Location
	 */
	protected $location;

	/**
	 * @var int
	 */
	protected $num_items_per_iteration;

	public function __construct( Location $location ) {
		$this->location = $location;
	}

	public function register() {
		add_action( 'ac/table/list_screen', [ $this, 'load_list_screen' ] );
	}

	/**
	 * Load a list screen and potentially attach the proper exporting information to it
	 *
	 * @param AC\ListScreen $list_screen List screen for current table screen
	 *
	 * @since 1.0
	 */
	public function load_list_screen( AC\ListScreen $list_screen ) {
		if ( ! $list_screen instanceof ListScreen ) {
			return;
		}

		$list_screen->export()->attach();

		$this->num_items_per_iteration = $list_screen->export()->get_num_items_per_iteration();

		add_action( 'ac/table_scripts', [ $this, 'scripts' ] );
	}

	public function scripts() {
		$style = new AC\Asset\Style(
			'acp-export-listscreen',
			$this->location->with_suffix( 'assets/export/css/listscreen.css' )
		);
		$style->enqueue();

		$script = new Script\Table(
			'acp-export-listscreen',
			$this->location->with_suffix( 'assets/export/js/listscreen.js' ),
			$this->num_items_per_iteration
		);
		$script->enqueue();
	}

}