<?php
/**
 * Plugin main class.
 *
 * @package DhlPolska
 */

namespace DhlPolska;

use DhlPolska\CheckoutBlocks\DefaultOptions;
use DhlPolska\CheckoutBlocks\OrderMetaUpdater;
use DhlPolska\CheckoutBlocks\PostnummerField;
use DhlPolska\CheckoutBlocks\PickupPointValidatorsFactory;
use DhlPolska\Integration\FlexibleShipping\FlexibleShippingIntegration;
use DhlPolska\Point\CheckoutChoosePointField;
use DhlPolska\Shipment\DhlShipment;
use DhlPolska\WooCommerceSettings\WooCommerceDHLPolskaSettings;
use DhlPolska\WooCommerceSettings\WooCommerceDHLPolskaSettingsPage;
use DhlPolskaVendor\Octolize\Blocks\PickupPoint\IntegrationData;
use DhlPolskaVendor\Octolize\Blocks\PickupPoint\Registrator;
use DhlPolskaVendor\Octolize\PluginUpdateReminder\RemindersFactory;
use DhlPolskaVendor\Octolize\Tracker\TrackerInitializer;
use DhlPolskaVendor\WPDesk\FS\Shipment\ShipmentFunctionality;
use DhlPolskaVendor\WPDesk\Logger\SimpleLoggerFactory;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\HookableCollection;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\HookableParent;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\TemplateLoad;
use DhlPolskaVendor\WPDesk\View\Renderer\Renderer;
use DhlPolskaVendor\WPDesk\View\Renderer\SimplePhpRenderer;
use DhlPolskaVendor\WPDesk\View\Resolver\ChainResolver;
use DhlPolskaVendor\WPDesk\View\Resolver\DirResolver;
use DhlPolskaVendor\WPDesk\View\Resolver\WPThemeResolver;
use DhlPolskaVendor\Psr\Log\LoggerAwareInterface;
use DhlPolskaVendor\Psr\Log\LoggerAwareTrait;
use DhlPolskaVendor\Psr\Log\NullLogger;
use DhlPolskaVendor\WPDesk_Plugin_Info;

/**
 * Main plugin class. The most important flow decisions are made here.
 *
 * @codeCoverageIgnore
 */
class Plugin extends AbstractPlugin implements LoggerAwareInterface, HookableCollection {

	/**
	 * @var string
	 */
	private string $scripts_version = '10';

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

	use LoggerAwareTrait;
	use HookableParent;
	use TemplateLoad;

	/**
	 * Plugin constructor.
	 *
	 * @param WPDesk_Plugin_Info $plugin_info Plugin info.
	 */
	public function __construct( WPDesk_Plugin_Info $plugin_info ) {
		parent::__construct( $plugin_info );

		$this->plugin_url       = $this->plugin_info->get_plugin_url();
		$this->plugin_namespace = $this->plugin_info->get_text_domain();
	}

	/**
	 * Returns true when debug mode is on.
	 *
	 * @return bool
	 */
	private function is_debug_mode() {
		return ( new WooCommerceDHLPolskaSettings() )->is_logging_enabled();
	}


	/**
	 * Init base variables for plugin
	 */
	public function init_base_variables() {
		$this->plugin_url       = $this->plugin_info->get_plugin_url();
		$this->plugin_path      = $this->plugin_info->get_plugin_dir();
		$this->template_path    = $this->plugin_info->get_text_domain();
		$this->plugin_namespace = $this->plugin_info->get_text_domain();
	}

	/**
	 * Init plugin
	 */
	public function init() {
		if ( defined( 'WOOCOMMERCE_DHL_POLSKA_VERSION' ) ) {
			$this->scripts_version = WOOCOMMERCE_DHL_POLSKA_VERSION . '.' . $this->scripts_version;
		}

		$this->init_renderer();
		$this->init_tracker();
		$this->init_checkout_blocks();

		parent::init();
	}

	/**
	 * @return void
	 */
	private function init_checkout_blocks(): void {
		$integration_name = 'dhl-polska';

		( new DefaultOptions( $integration_name ) )->hooks();
		( new OrderMetaUpdater( $integration_name ) )->hooks();
		( new PostnummerField() )->hooks();

		$integration_data = ( new IntegrationData() )
			->set_integration_name( $integration_name )
			->set_meta_data_name( CheckoutChoosePointField::FIELD_PARCELSHOP_ID )
			->set_nonce_name( $integration_name )
			->set_flexible_shipping_integration( FlexibleShippingIntegration::INTEGRATION_ID );

		( new Registrator(
			$integration_data,
			$this->plugin_info->get_plugin_dir(),
			$this->plugin_info->get_plugin_file_name(),
			( new PickupPointValidatorsFactory() )->get_validators()
		) )->hooks();
	}

	/**
	 * @return void
	 */
	private function init_tracker() {
		$this->add_hookable(
			TrackerInitializer::create_from_plugin_info_for_shipping_method(
				$this->plugin_info,
				WooCommerceDHLPolskaSettingsPage::SECTION_ID
			)
		);
	}

	/**
	 * Init hooks.
	 */
	public function hooks() {
		parent::hooks();

		add_action( 'woocommerce_init', [ $this, 'init_hookable_objects' ] );

		$this->add_hookable(
			new ShipmentFunctionality(
				is_null( $this->logger ) ? new NullLogger() : $this->logger,
				trailingslashit( $this->get_plugin_assets_url() ) . '../vendor_prefixed/wpdesk/wp-wpdesk-fs-shipment/assets/',
				$this->scripts_version
			)
		);

		$this->hooks_on_hookable_objects();
	}

	/**
	 * @return void
	 */
	public function init_hookable_objects() {
		if ( $this->is_debug_mode() ) {
			$this->logger = ( new SimpleLoggerFactory( 'dhl-polska' ) )->getLogger();
		} else {
			$this->logger = new NullLogger();
		}

		if ( ! class_exists( 'WPDesk_Flexible_Shipping_Shipment_dhl_polska' ) ) {
			class_alias( DhlShipment::class, 'WPDesk_Flexible_Shipping_Shipment_dhl_polska' );
		}

		( new HookableObjects(
			$this->get_plugin_assets_url(),
			$this->scripts_version,
			WC()->cart,
			$this->get_plugin_url(),
			WC()->session,
			$this->plugin_info,
			$this->logger,
			$this->renderer
		) )->hooks();
	}

	/**
	 * Quick links on plugins page.
	 *
	 * @param string[] $links .
	 *
	 * @return string[]
	 */
	public function links_filter( $links ) {
		$docs_link    = 'https://octol.io/dhl-polska-docs';
		$support_link = 'https://octol.io/dhl-polska-support';

		$external_attributes = ' target="_blank" ';

		$settings_link = admin_url( 'admin.php?page=wc-settings&tab=shipping&section=' . WooCommerceDHLPolskaSettingsPage::SECTION_ID );

		$plugin_links = [
			'<a href="' . $settings_link . '">' . __( 'Ustawienia', 'dhl-polska' ) . '</a>',
			'<a href="' . $docs_link . '"' . $external_attributes . '>' . __( 'Dokumentacja', 'dhl-polska' ) . '</a>',
			'<a href="' . $support_link . '"' . $external_attributes . '>' . __( 'Wsparcie', 'dhl-polska' ) . '</a>',
		];

		return array_merge( $plugin_links, $links );
	}


	/**
	 * Init renderer.
	 *
	 * @return void
	 */
	private function init_renderer() {
		$resolver = new ChainResolver();
		$resolver->appendResolver( new WPThemeResolver( $this->get_template_path() ) );
		$resolver->appendResolver( new DirResolver( trailingslashit( $this->plugin_path ) . 'templates' ) );
		$resolver->appendResolver( new DirResolver( trailingslashit( $this->plugin_path ) . 'vendor_prefixed/wpdesk/wp-woocommerce-shipping/templates' ) );
		$this->renderer = new SimplePhpRenderer( $resolver );
	}
}
