<?php

namespace WPDesk\FakturyKSEF\Renderer;

use WPDeskKSEFVendor\WPDesk\View\Renderer\SimplePhpRenderer;

class KSeFRenderer extends SimplePhpRenderer {

	private const METABOX_ACTION_BUTTONS             = 'metabox_action_buttons';
	private const DOCUMENT_SENT_TO_KSEF              = 'document_sent_to_ksef';
	private const DOCUMENT_STATUS_TEMPLATE           = 'document_ksef_status';
	private const DOCUMENT_AWAITING_SENDING_TEMPLATE = 'document_awaiting_sending';
	private const CANNOT_SEND_DOCUMENT_TEMPLATE      = 'cannot_send_document';
	private const QR_CODE_TEMPLATE                   = 'ksef_qr_code';
	private const UNAUTHORIZED_TO_SEND_DOCUMENT      = 'unauthorized_send_document';
	private const DOWNLOAD_KSEF_XML                  = 'download_ksef_xml';
	private const DOCUMENT_ALREADY_EXISTS_IN_KSEF    = 'document_already_exists_in_ksef';
	private const CONNECTION_STATUS_TEMPLATE         = 'settings/connection_status';
	private const METABOX_PARAMS_FORM                = 'settings/metabox_form';


	/**
	 * Send to KSeF button
	 */
	public function render_metabox_action_buttons( int $post_id, ?string $download_url = null ): string {
		return $this->render(
			self::METABOX_ACTION_BUTTONS,
			[
				'post_id'      => $post_id,
				'download_url' => $download_url,
			]
		);
	}

	public function output_metabox_action_buttons( int $post_id, ?string $download_url = null ): void {
		$this->output_render(
			self::METABOX_ACTION_BUTTONS,
			[
				'post_id'      => $post_id,
				'download_url' => $download_url,
			]
		);
	}

	/**
	 * Document has already been sent to KSeF
	 */
	public function render_document_sent_to_ksef( string $ksef_reference_number ): string {
		return $this->render( self::DOCUMENT_SENT_TO_KSEF, [ 'ksef_reference_number' => $ksef_reference_number ] );
	}

	public function output_document_sent_to_ksef( string $ksef_reference_number ): void {
		$this->output_render( self::DOCUMENT_SENT_TO_KSEF, [ 'ksef_reference_number' => $ksef_reference_number ] );
	}

	/**
	 * Current status of sending document to KSeF.
	 */
	public function render_ksef_document_status( string $response, string $attempt ): string {
		return $this->render(
			self::DOCUMENT_STATUS_TEMPLATE,
			[
				'response' => $response,
				'attempt'  => $attempt,
			]
		);
	}

	public function output_ksef_document_status( string $response, string $attempt ): void {
		$this->output_render(
			self::DOCUMENT_STATUS_TEMPLATE,
			[
				'response' => $response,
				'attempt'  => $attempt,
			]
		);
	}

	/**
	 * Document is ready to be sent to KSeF.
	 */
	public function render_document_ready_to_send(): string {
		return $this->render( self::DOCUMENT_AWAITING_SENDING_TEMPLATE );
	}

	public function output_document_ready_to_send(): void {
		$this->output_render( self::DOCUMENT_AWAITING_SENDING_TEMPLATE );
	}

	/**
	 * Cannot send document
	 */
	public function render_cannot_send_document( array $reasons, ?string $overwritten_error_message = null ): string {
		$error_message = __( 'Cannot send this document', 'ksef-for-flexible-invoices' );

		if ( $overwritten_error_message !== null ) {
			$error_message = $overwritten_error_message;
		}

		return $this->render(
			self::CANNOT_SEND_DOCUMENT_TEMPLATE,
			[
				'reasons'       => $reasons,
				'error_message' => $error_message,
			]
		);
	}

	public function output_cannot_send_document( array $reasons = [], ?string $overwritten_error_message = null ): void {
		$error_message = __( 'Cannot send this document', 'ksef-for-flexible-invoices' );

		if ( $overwritten_error_message !== null ) {
			$error_message = $overwritten_error_message;
		}

		$this->output_render(
			self::CANNOT_SEND_DOCUMENT_TEMPLATE,
			[
				'reasons'       => $reasons,
				'error_message' => $error_message,
			]
		);
	}

	/**
	 * KSeF connection status
	 */
	public function render_connection_status( bool $connected, string $message = '' ): string {
		return $this->render(
			self::CONNECTION_STATUS_TEMPLATE,
			[
				'connected' => $connected,
				'message'   => $message,
			]
		);
	}

	public function output_connection_status( bool $connected, string $message ): void {
		$this->output_render(
			self::CONNECTION_STATUS_TEMPLATE,
			[
				'connected' => $connected,
				'message'   => $message,
			]
		);
	}

	/**
	 * QR Code with invoice URL
	 */
	public function render_document_qr_code( string $qr_code_src, string $qr_code_description ): string {
		return $this->render(
			self::QR_CODE_TEMPLATE,
			[
				'qr_code_src'      => $qr_code_src,
				'code_description' => $qr_code_description,
			]
		);
	}

	public function output_document_qr_code( string $qr_code_src, string $qr_code_description ): void {
		$this->output_render(
			self::QR_CODE_TEMPLATE,
			[
				'qr_code_src'      => $qr_code_src,
				'code_description' => $qr_code_description,
			]
		);
	}

	/**
	 * API Error - unauthorized
	 */
	public function render_unauthorized_connection_document_notice(): string {
		return $this->render(
			self::UNAUTHORIZED_TO_SEND_DOCUMENT
		);
	}

	public function output_unauthorized_connection_document_notice(): void {
		$this->output_render(
			self::UNAUTHORIZED_TO_SEND_DOCUMENT
		);
	}

	/**
	 * Button with ability to download generated XML file
	 */
	public function output_download_ksef_xml_button( string $download_url ): void {
		$this->output_render(
			self::DOWNLOAD_KSEF_XML,
			[ 'download_url' => $download_url ]
		);
	}

	/**
	 * Render info about document already existing in KSeF
	 */
	public function render_document_already_exists_in_ksef( string $ksef_reference_number ): string {
		return $this->render( self::DOCUMENT_ALREADY_EXISTS_IN_KSEF, [ 'ksef_reference_number' => $ksef_reference_number ] );
	}

	/**
	 * Render metabox form with additional request params
	 */
	public function output_metabox_form( string $exemption, string $zero_rate_type, string $np_rate_type ): void {
		$this->output_render(
			self::METABOX_PARAMS_FORM,
			[
				'exemption'      => $exemption,
				'zero_rate_type' => $zero_rate_type,
				'np_rate_type'   => $np_rate_type,
			]
		);
	}
}
