<?php
/**
 * Class HookableObjects
 *
 * @package DhlPolska
 */

namespace DhlPolska;

use DhlPolska\Integration\FlexibleShipping\FlexibleShippingIntegration;
use DhlPolska\CheckoutBlocks\ShippingMethodLogoAssets;
use DhlPolska\Point\CheckoutChoosePointField;
use DhlPolska\Point\CheckoutPointValidator;
use DhlPolska\Point\CheckoutPostedDataPreparing;
use DhlPolska\Point\CheckoutSavePoint;
use DhlPolska\Point\CheckoutShipmentMetaUpdater;
use DhlPolska\Point\EmailOrderDetailsPointInfo;
use DhlPolska\Point\PointAssetsCheckout;
use DhlPolska\Point\OrderDetailsPointInfo;
use DhlPolska\Shipment\AddShipmentMetabox;
use DhlPolska\Shipment\AutoCourierCreator;
use DhlPolska\Shipment\AutoShipmentCreator;
use DhlPolska\Shipment\DhlShipment;
use DhlPolska\Shipment\Metabox\Field\Packages;
use DhlPolska\Shipment\ReturnLabelDispatcher;
use DhlPolska\Shipment\ShipmentAssets;
use DhlPolska\Shipment\ShipmentTypes;
use DhlPolska\Admin\DhlPolskaMenu;
use DhlPolska\Admin\Menu\MenuOrderer;
use DhlPolska\Admin\Menu\PnpSubmenu;
use DhlPolska\Admin\Menu\SettingsSubmenu;
use DhlPolska\Admin\Menu\ShipmentsSubmenu;
use DhlPolska\Download\BinaryDownloadResponse;
use DhlPolskaVendor\Octolize\DhlPolska\API\WebAPI;
use DhlPolskaVendor\Octolize\DhlPolska\API\WebApi\Struct\AuthData as WebApiAuthData;
use DhlPolska\ShipmentTemplates\ShipmentTemplateMetabox;
use DhlPolska\ShipmentTemplates\ShipmentTemplateListColumns;
use DhlPolska\ShipmentTemplates\ShipmentTemplatePostType;
use DhlPolska\WooCommerceSettings\Field\ConnectionStatusField;
use DhlPolska\WooCommerceSettings\SettingsAssets;
use DhlPolska\WooCommerceSettings\Templates\TemplatesRepository;
use DhlPolska\WooCommerceSettings\WooCommerceDHLPolskaSettings;
use DhlPolska\WooCommerceSettings\WooCommerceDHLPolskaSettingsFields;
use DhlPolska\WooCommerceSettings\WooCommerceDHLPolskaSettingsPage;
use DhlPolska\ShippingMethod\InstanceSettingsAssets;
use DhlPolska\ShippingMethod\ShippingMethodLogoLabel;
use DhlPolskaVendor\Psr\Log\LoggerInterface;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\HookableCollection;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\HookableParent;
use DhlPolskaVendor\WPDesk\View\Renderer\Renderer;
use DhlPolskaVendor\WPDesk_Plugin_Info;

/**
 * Can add hookable objects.
 */
class HookableObjects implements HookableCollection {

	use HookableParent;

	/**
	 * @var string
	 */
	private $assets_url;

	/**
	 * @var string
	 */
	private $scripts_version;

	/**
	 * @var \WC_Cart|null
	 */
	private $cart;

	/**
	 * @var string
	 */
	private $plugin_url;

	/**
	 * @var \WC_Session|null
	 */
	private $session;

	/**
	 * @var WPDesk_Plugin_Info
	 */
	private $plugin_info;

	/**
	 * @var LoggerInterface
	 */
	private $logger;

	/**
	 * @var Renderer
	 */
	private Renderer $renderer;

	/**
	 * HookableObjects constructor.
	 *
	 * @param string              $assets_url      .
	 * @param string              $scripts_version .
	 * @param \WC_Cart|null       $cart            .
	 * @param string              $plugin_url      .
	 * @param \WC_Session|null    $session         .
	 * @param \WPDesk_Plugin_Info $plugin_info     .
	 * @param LoggerInterface     $logger          .
	 * @param Renderer            $renderer        .
	 */
	public function __construct( $assets_url, $scripts_version, $cart, $plugin_url, $session, $plugin_info, LoggerInterface $logger, Renderer $renderer ) {
		$this->assets_url      = $assets_url;
		$this->scripts_version = $scripts_version;
		$this->cart            = $cart;
		$this->plugin_url      = $plugin_url;
		$this->session         = $session;
		$this->plugin_info     = $plugin_info;
		$this->logger          = $logger;
		$this->renderer        = $renderer;
	}

	/**
	 * Hooks.
	 */
	public function hooks() {
		$dhl_settings   = new WooCommerceDHLPolskaSettings();
		$shipment_types = new ShipmentTypes();

		DhlShipment::set_shipping_settings( $dhl_settings );
		DhlShipment::set_logger( $this->logger );

		$webapi = new WebAPI(
			new WebApiAuthData( $dhl_settings->get_webapi_login(), $dhl_settings->get_webapi_password() ),
			$dhl_settings->get_webapi_sap_number(),
			$dhl_settings->is_test_mode(),
			$this->logger
		);

		$this->add_hookable( new ConnectionStatusField() );
		$this->add_hookable( new CheckConnectionStatus( $dhl_settings, $this->logger ) );
		$binary_download_response = new BinaryDownloadResponse();

		$shipments_submenu = new ShipmentsSubmenu( $dhl_settings, $this->logger, $webapi, $this->assets_url, $this->scripts_version );
		$this->add_hookable( new DhlPolskaMenu( $shipments_submenu ) );
		$this->add_hookable( $shipments_submenu );
		$this->add_hookable( new PnpSubmenu( $webapi, $binary_download_response ) );
		$this->add_hookable( new SettingsSubmenu() );
		$this->add_hookable( new MenuOrderer() );
		$templates_repository = new TemplatesRepository( $dhl_settings, $shipment_types );
		$this->add_hookable( new WooCommerceDHLPolskaSettingsPage( new WooCommerceDHLPolskaSettingsFields( $templates_repository, $shipment_types ) ) );
		$this->add_hookable( new ShipmentTemplatePostType() );
		$this->add_hookable( new ShipmentTemplateMetabox( $templates_repository, $shipment_types ) );
		$this->add_hookable( new ShipmentTemplateListColumns( $templates_repository, $shipment_types ) );
		$this->add_hookable( new SettingsAssets( $this->assets_url, $this->scripts_version ) );

		$this->add_hookable( new ShippingMethod() );
		$this->add_hookable( new ShippingMethodLogoLabel( $this->plugin_url ) );
		$this->add_hookable( new InstanceSettingsAssets( $this->assets_url, $this->scripts_version ) );
		$this->add_hookable( new FlexibleShippingIntegration() );
		$this->add_hookable( new AutoShipmentCreator( $dhl_settings, $this->logger ) );
		$this->add_hookable( new AutoCourierCreator( $dhl_settings, $this->logger ) );
		$this->add_hookable( new AddShipmentMetabox() );
		$this->add_hookable( new ReturnLabelDispatcher( $binary_download_response ) );
		$this->add_hookable( new ShipmentAssets( $this->assets_url, $this->scripts_version, $dhl_settings->is_test_mode() ) );
		$this->add_hookable( new CheckoutChoosePointField( $this->renderer ) );
		$this->add_hookable( new CheckoutPostedDataPreparing() );
		$this->add_hookable( new CheckoutPointValidator() );
		$this->add_hookable( new CheckoutSavePoint() );
		$this->add_hookable( new CheckoutShipmentMetaUpdater() );
		$this->add_hookable( new ShippingMethodLogoAssets( $this->plugin_url, $this->scripts_version ) );
		$this->add_hookable( new OrderDetailsPointInfo( $this->renderer ) );
		$this->add_hookable( new EmailOrderDetailsPointInfo( $this->renderer ) );
		$this->add_hookable(
			new PointAssetsCheckout(
				$this->assets_url,
				$this->scripts_version,
				$dhl_settings->is_test_mode() ? 'https://psm-sandbox.dhl24.com.pl' : 'https://parcelshop.dhl.pl'
			)
		);
		$this->add_hookable(
			new Packages(
				$dhl_settings->get_default_weight(),
				get_option( 'woocommerce_weight_unit', 'kg' ),
				get_option( 'woocommerce_dimension_unit', 'cm' ),
				$shipment_types
			)
		);

		$this->hooks_on_hookable_objects();
	}
}
