<?php
/**
 * Class ShipmentsSubmenu
 *
 * @package DhlPolska\Admin\Menu
 */

namespace DhlPolska\Admin\Menu;

use DhlPolska\Admin\DhlPolskaMenu;
use DhlPolska\Admin\Shipments\ShipmentsListTable;
use DhlPolska\API\WebAPI;
use DhlPolska\WooCommerceSettings\WooCommerceDHLPolskaSettings;
use DhlPolskaVendor\Psr\Log\LoggerInterface;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\Hookable;

/**
 * Register DHL Polska shipments submenu.
 */
class ShipmentsSubmenu implements Hookable {

	private WooCommerceDHLPolskaSettings $settings;

	private LoggerInterface $logger;

	private WebAPI $webapi;

	public function __construct( WooCommerceDHLPolskaSettings $settings, LoggerInterface $logger, WebAPI $webapi ) {
		$this->settings = $settings;
		$this->logger   = $logger;
		$this->webapi   = $webapi;
	}

	/**
	 * Hooks.
	 */
	public function hooks() {
		add_action( 'admin_menu', [ $this, 'register_submenu' ] );
	}

	/**
	 * @return void
	 */
	public function register_submenu() {
		add_submenu_page(
			DhlPolskaMenu::MENU_SLUG,
			__( 'Przesyłki', 'dhl-polska' ),
			__( 'Przesyłki', 'dhl-polska' ),
			'manage_woocommerce',
			DhlPolskaMenu::MENU_SLUG,
			[ $this, 'render_page' ],
			1
		);
	}

	/**
	 * @return void
	 */
	public function render_page() {
		$table = new ShipmentsListTable( $this->settings, $this->logger, $this->webapi );
		$table->handle_bulk_actions();
		$table->prepare_items();
		$notice    = $table->get_notice();
		$menu_slug = DhlPolskaMenu::MENU_SLUG;

		include __DIR__ . '/../views/html-shipments-page.php';
	}
}
