<?php

namespace WPDesk\FakturyKSEF;

use WPDesk\FakturyKSEF\Repository\PluginSettings;
use WPDesk\FakturyKSEF\Services\PageChecker;
use WPDesk\FakturyKSEF\Xml\SendDocument;
use WPDeskKSEFVendor\WPDesk\PluginBuilder\Plugin\Hookable;
use WPDeskKSEFVendor\WPDesk_Plugin_Info;

class Assets implements Hookable {

	private const KSEF_COMMON_FUNCTIONS_HANDLE          = 'fi-ksef-sending-common-functions';
	private const METABOX_HANDLE                        = 'fi-ksef-metabox-handler';
	private const BULK_ACTION_HANDLE                    = 'fi-ksef-bulk-sending-handler';
	private const METABOX_FIELD_MANAGER_HANDLE          = 'fi-ksef-metabox-field-manager';
	private const GENERAL_SETTINGS_FIELD_MANAGER_HANDLE = 'fi-ksef-general-settings-field-manager';

	private WPDesk_Plugin_Info $plugin_info;

	private PageChecker $page_checker;

	private PluginSettings $settings;

	public function __construct( WPDesk_Plugin_Info $info, PageChecker $page_checker, PluginSettings $settings ) {
		$this->settings     = $settings;
		$this->page_checker = $page_checker;
		$this->plugin_info  = $info;
	}

	private function get_plugin_asset_url( string $file_url = '' ): string {
		return $this->plugin_info->get_plugin_url() . '/assets/' . $file_url;
	}

	private function get_assets_version(): string {
		return $this->plugin_info->get_version();
	}

	public function hooks() {
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ] );
	}

	public function enqueue_styles( $hook ) {
		if ( $this->page_checker->is_on_invoice_edit_page( $hook ) || $this->page_checker->is_on_invoice_list_page( $hook ) ) {
			wp_enqueue_style(
				'fi-ksef-status',
				$this->get_plugin_asset_url( 'css/ksef-status.css' ),
				[],
				$this->get_assets_version()
			);
			wp_enqueue_style(
				'fi-ksef-metabox',
				$this->get_plugin_asset_url( 'css/ksef-metabox.css' ),
				[],
				$this->get_assets_version()
			);
		}

		if ( $this->page_checker->is_on_ksef_settings_page() ) {
			wp_enqueue_style(
				'fi-ksef-settings',
				$this->get_plugin_asset_url( 'css/ksef-settings.css' ),
				[],
				$this->get_assets_version()
			);
		}
	}

	public function enqueue_scripts( $hook ) {

		$this->register_common_script();

		if ( $this->page_checker->is_on_invoice_edit_page( $hook ) ) {
			$this->load_metabox_handler();
			$this->load_metabox_fields_manager();
		}

		if ( $this->page_checker->is_on_invoice_list_page( $hook ) ) {
			$this->load_bulk_action_handler();
		}

		if ( $this->page_checker->is_on_ksef_settings_page() ) {
			$this->load_field_manager_script();
		}
	}

	private function load_metabox_fields_manager(): void {
		wp_enqueue_script(
			self::METABOX_FIELD_MANAGER_HANDLE,
			$this->get_plugin_asset_url( 'js/ksef-metabox-field-manager.js' ),
			[ 'jquery' ],
			$this->get_assets_version(),
			true
		);

		wp_localize_script(
			self::METABOX_FIELD_MANAGER_HANDLE,
			'fiksef_MetaboxFieldManagerData',
			[
				'zwRate' => $this->settings->get_exempt_rate(),
				'npRate' => $this->settings->get_np_rate(),
			]
		);
	}

	private function load_field_manager_script() {
		wp_enqueue_script(
			self::GENERAL_SETTINGS_FIELD_MANAGER_HANDLE,
			$this->get_plugin_asset_url( 'js/ksef-general-settings-field-manager.js' ),
			[ 'jquery' ],
			$this->get_assets_version(),
			true
		);
	}

	private function register_common_script(): void {
		wp_register_script(
			self::KSEF_COMMON_FUNCTIONS_HANDLE,
			$this->get_plugin_asset_url( 'js/ksef-ajax-handling.js' ),
			[ 'jquery' ],
			$this->get_assets_version(),
			true
		);
	}

	private function load_bulk_action_handler(): void {
		wp_enqueue_script(
			self::BULK_ACTION_HANDLE,
			$this->get_plugin_asset_url( 'js/ksef-bulk-handling.js' ),
			[ 'jquery', self::KSEF_COMMON_FUNCTIONS_HANDLE ],
			$this->get_assets_version(),
			true
		);

		wp_localize_script(
			self::BULK_ACTION_HANDLE,
			'fiksef_bulkSendingHandlerData',
			[
				'ajaxUrl'           => admin_url( 'admin-ajax.php' ),
				'ajaxSendingAction' => 'fiksef_send_document_to_ksef',
				'ajaxNonce'         => wp_create_nonce( SendDocument::NONCE_ACTION_SEND_DOCUMENT ),
				'pollNonce'         => wp_create_nonce( SendDocument::NONCE_ACTION_CHECK_STATUS ),
				'bulkAction'        => 'send_to_ksef',
			]
		);
	}

	private function load_metabox_handler(): void {
		global $post;
		if ( isset( $post->post_type ) && $post->post_type === 'inspire_invoice' ) {
			wp_enqueue_script(
				self::METABOX_HANDLE,
				$this->get_plugin_asset_url( 'js/ksef-metabox-handling.js' ),
				[ 'jquery', self::KSEF_COMMON_FUNCTIONS_HANDLE ],
				$this->get_assets_version(),
				true
			);

			wp_localize_script(
				self::METABOX_HANDLE,
				'fiksef_metaboxHandlerData',
				[
					'ajaxUrl'    => admin_url( 'admin-ajax.php' ),
					'ajaxAction' => 'fiksef_send_document_to_ksef',
					'documentId' => $post->ID ?? 0,
					'nonce'      => wp_create_nonce( SendDocument::NONCE_ACTION_SEND_DOCUMENT ),
					'pollNonce'  => wp_create_nonce( SendDocument::NONCE_ACTION_CHECK_STATUS ),
				]
			);
		}
	}
}
