<?php
/**
 * Shortcodes class.
 *
 * @package TextToPDF
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * TextToPDF_Shortcodes Class
 */
class TextToPDF_Shortcodes {

	/**
	 * Initialize hooks.
	 */
	public function init() {
		add_shortcode( 'text_to_pdf_form', array( $this, 'render_form_shortcode' ) );
		add_shortcode( 'text_to_pdf_button', array( $this, 'render_button_shortcode' ) );
	}

	/**
	 * Render the [text_to_pdf_form] shortcode.
	 *
	 * @return string HTML output.
	 */
	public function render_form_shortcode() {
		ob_start();
		?>
		<div class="texttopdf-form-wrapper">
			<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" class="texttopdf-form">
				<input type="hidden" name="action" value="texttopdf_generate_from_text">
				<?php wp_nonce_field( 'texttopdf_generate_from_text_nonce', 'texttopdf_nonce' ); ?>
				
				<div class="texttopdf-field">
					<label for="texttopdf_title"><?php esc_html_e( 'Document Title (Optional)', 'texttopdfnet-text-web-to-pdf' ); ?></label>
					<input type="text" name="texttopdf_title" id="texttopdf_title" class="texttopdf-input">
				</div>

				<div class="texttopdf-field">
					<label for="texttopdf_content"><?php esc_html_e( 'Text Content (Required)', 'texttopdfnet-text-web-to-pdf' ); ?></label>
					<textarea name="texttopdf_content" id="texttopdf_content" class="texttopdf-textarea" rows="10" required></textarea>
				</div>

				<div class="texttopdf-submit">
					<button type="submit" class="texttopdf-button"><?php esc_html_e( 'Generate PDF', 'texttopdfnet-text-web-to-pdf' ); ?></button>
				</div>
			</form>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * Render the [text_to_pdf_button] shortcode.
	 *
	 * @return string HTML output.
	 */
	public function render_button_shortcode() {
		if ( ! is_singular() ) {
			return '';
		}

		$post_id      = get_the_ID();
		$button_label = TextToPDF_Settings::get_setting( 'button_label' );
		if ( empty( $button_label ) ) {
			$button_label = __( 'Download PDF', 'texttopdfnet-text-web-to-pdf' );
		}

		$url = wp_nonce_url( 
			add_query_arg( 
				array( 
					'action'  => 'texttopdf_generate_from_post', 
					'post_id' => $post_id 
				), 
				admin_url( 'admin-post.php' ) 
			), 
			'texttopdf_generate_from_post_nonce', 
			'texttopdf_nonce' 
		);

		ob_start();
		?>
		<div class="texttopdf-button-wrapper">
			<a href="<?php echo esc_url( $url ); ?>" class="texttopdf-button" target="_blank">
				<?php echo esc_html( $button_label ); ?>
			</a>
		</div>
		<?php
		return ob_get_clean();
	}
}
