<?php
/**
 * Admin settings class for Text to PDF plugin.
 *
 * @package TextToPDF
 */

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

/**
 * TextToPDF_Settings Class
 */
class TextToPDF_Settings {

	/**
	 * Option key.
	 *
	 * @var string
	 */
	private $option_key = 'texttopdf_settings';

	/**
	 * Initialize settings hooks.
	 */
	public function init() {
		add_action( 'admin_init', array( $this, 'register_settings' ) );
		add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
	}

	/**
	 * Get default settings.
	 *
	 * @return array Default settings.
	 */
	public static function get_defaults() {
		return array(
			'enable_on_posts'        => '1',
			'enable_on_pages'        => '0',
			'button_label'           => 'Download PDF',
			'button_position'        => 'bottom',
			'include_title'          => '1',
			'include_featured_image' => '1',
			'include_author'         => '1',
			'include_date'           => '1',
			'header_text'            => '',
			'footer_text'            => '',
			'file_name_pattern'      => 'document-{date}',
			'page_size'              => 'a4',
			'font_size'              => 'medium',
			'margin_preset'          => 'normal',
			'watermark_text'         => '',
			'watermark_logo'         => '',
			'watermark_size'         => '500',
			'watermark_position'     => 'top-left',
			'watermark_opacity'      => '30',
		);
	}

	/**
	 * Retrieve a specific setting.
	 *
	 * @param string $key Setting key.
	 * @return mixed
	 */
	public static function get_setting( $key ) {
		$options  = get_option( 'texttopdf_settings', array() );
		$defaults = self::get_defaults();

		if ( isset( $options[ $key ] ) ) {
			return $options[ $key ];
		}

		return isset( $defaults[ $key ] ) ? $defaults[ $key ] : null;
	}

	/**
	 * Register plugin settings.
	 */
	public function register_settings() {
		register_setting(
			'texttopdf_settings_group',
			$this->option_key,
			array(
				'sanitize_callback' => array( $this, 'sanitize_settings' ),
			)
		);
	}

	/**
	 * Sanitize and validate settings input.
	 *
	 * @param array $input Input settings.
	 * @return array Sanitized settings.
	 */
	public function sanitize_settings( $input ) {
		$sanitized = array();
		$defaults  = self::get_defaults();

		// Booleans.
		$boolean_keys = array( 'enable_on_posts', 'enable_on_pages', 'include_title', 'include_featured_image', 'include_author', 'include_date' );
		foreach ( $boolean_keys as $key ) {
			$sanitized[ $key ] = ! empty( $input[ $key ] ) ? '1' : '0';
		}

		// Texts.
		$sanitized['button_label']      = sanitize_text_field( isset( $input['button_label'] ) ? $input['button_label'] : $defaults['button_label'] );
		$sanitized['header_text']       = sanitize_text_field( isset( $input['header_text'] ) ? $input['header_text'] : '' );
		$sanitized['footer_text']       = sanitize_text_field( isset( $input['footer_text'] ) ? $input['footer_text'] : '' );
		
		$file_name = sanitize_text_field( isset( $input['file_name_pattern'] ) ? $input['file_name_pattern'] : $defaults['file_name_pattern'] );
		$sanitized['file_name_pattern'] = empty( $file_name ) ? 'document-{date}' : $file_name;

		// Enums using allowlists.
		$allowed_positions = array( 'top', 'bottom', 'both', 'shortcode_only' );
		$sanitized['button_position'] = isset( $input['button_position'] ) && in_array( $input['button_position'], $allowed_positions, true ) ? $input['button_position'] : 'bottom';

		$allowed_page_sizes = array( 'a4', 'letter' );
		$sanitized['page_size'] = isset( $input['page_size'] ) && in_array( $input['page_size'], $allowed_page_sizes, true ) ? $input['page_size'] : 'a4';

		$allowed_font_sizes = array( 'small', 'medium', 'large' );
		$sanitized['font_size'] = isset( $input['font_size'] ) && in_array( $input['font_size'], $allowed_font_sizes, true ) ? $input['font_size'] : 'medium';

		$allowed_margins = array( 'narrow', 'normal', 'wide' );
		$sanitized['margin_preset'] = isset( $input['margin_preset'] ) && in_array( $input['margin_preset'], $allowed_margins, true ) ? $input['margin_preset'] : 'normal';

		$sanitized['watermark_text'] = sanitize_text_field( isset( $input['watermark_text'] ) ? $input['watermark_text'] : '' );
		$sanitized['watermark_logo'] = sanitize_url( isset( $input['watermark_logo'] ) ? $input['watermark_logo'] : '' );
		$sanitized['watermark_size'] = absint( isset( $input['watermark_size'] ) ? $input['watermark_size'] : 500 );
		if ( ! $sanitized['watermark_size'] ) {
			$sanitized['watermark_size'] = 500;
		}

		$allowed_watermark_positions = array( 'top-left', 'top-right', 'center', 'bottom-left', 'bottom-right' );
		$sanitized['watermark_position'] = isset( $input['watermark_position'] ) && in_array( $input['watermark_position'], $allowed_watermark_positions, true ) ? $input['watermark_position'] : 'top-left';

		$opacity = isset( $input['watermark_opacity'] ) ? absint( $input['watermark_opacity'] ) : 30;
		if ( $opacity < 1 ) $opacity = 1;
		if ( $opacity > 100 ) $opacity = 100;
		$sanitized['watermark_opacity'] = $opacity;

		return $sanitized;
	}

	/**
	 * Add options page to the menu.
	 */
	public function add_settings_page() {
		add_options_page(
			__( 'Text to PDF Settings', 'texttopdfnet-text-web-to-pdf' ),
			__( 'Text to PDF', 'texttopdfnet-text-web-to-pdf' ),
			'manage_options',
			'texttopdf-settings',
			array( $this, 'render_settings_page' )
		);
	}

	/**
	 * Render the administrative interface.
	 */
	public function render_settings_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$options = wp_parse_args( get_option( $this->option_key, array() ), self::get_defaults() );
		?>
		<div class="wrap">
			<h1><?php esc_html_e( 'Text to PDF Settings', 'texttopdfnet-text-web-to-pdf' ); ?></h1>
			<form action="options.php" method="post">
				<?php
				settings_fields( 'texttopdf_settings_group' );
				?>
				<table class="form-table">
					
					<tr>
						<th scope="row"><?php esc_html_e( 'Enable on Posts', 'texttopdfnet-text-web-to-pdf' ); ?></th>
						<td>
							<label>
								<input type="checkbox" name="<?php echo esc_attr( $this->option_key ); ?>[enable_on_posts]" value="1" <?php checked( '1', $options['enable_on_posts'] ); ?>>
								<?php esc_html_e( 'Show download button on standard posts.', 'texttopdfnet-text-web-to-pdf' ); ?>
							</label>
						</td>
					</tr>
					
					<tr>
						<th scope="row"><?php esc_html_e( 'Enable on Pages', 'texttopdfnet-text-web-to-pdf' ); ?></th>
						<td>
							<label>
								<input type="checkbox" name="<?php echo esc_attr( $this->option_key ); ?>[enable_on_pages]" value="1" <?php checked( '1', $options['enable_on_pages'] ); ?>>
								<?php esc_html_e( 'Show download button on standard pages.', 'texttopdfnet-text-web-to-pdf' ); ?>
							</label>
						</td>
					</tr>

					<tr>
						<th scope="row"><label for="texttopdf_button_label"><?php esc_html_e( 'Button Label', 'texttopdfnet-text-web-to-pdf' ); ?></label></th>
						<td>
							<input type="text" id="texttopdf_button_label" name="<?php echo esc_attr( $this->option_key ); ?>[button_label]" value="<?php echo esc_attr( $options['button_label'] ); ?>" class="regular-text">
						</td>
					</tr>

					<tr>
						<th scope="row"><?php esc_html_e( 'Button Position', 'texttopdfnet-text-web-to-pdf' ); ?></th>
						<td>
							<select name="<?php echo esc_attr( $this->option_key ); ?>[button_position]">
								<option value="top" <?php selected( 'top', $options['button_position'] ); ?>><?php esc_html_e( 'Top of Content', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="bottom" <?php selected( 'bottom', $options['button_position'] ); ?>><?php esc_html_e( 'Bottom of Content', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="both" <?php selected( 'both', $options['button_position'] ); ?>><?php esc_html_e( 'Both Top and Bottom', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="shortcode_only" <?php selected( 'shortcode_only', $options['button_position'] ); ?>><?php esc_html_e( 'Shortcode Only [text_to_pdf_button]', 'texttopdfnet-text-web-to-pdf' ); ?></option>
							</select>
						</td>
					</tr>

					<tr>
						<th scope="row"><?php esc_html_e( 'Content to Include', 'texttopdfnet-text-web-to-pdf' ); ?></th>
						<td>
							<label><input type="checkbox" name="<?php echo esc_attr( $this->option_key ); ?>[include_title]" value="1" <?php checked( '1', $options['include_title'] ); ?>> <?php esc_html_e( 'Post Title', 'texttopdfnet-text-web-to-pdf' ); ?></label><br>
							<label><input type="checkbox" name="<?php echo esc_attr( $this->option_key ); ?>[include_featured_image]" value="1" <?php checked( '1', $options['include_featured_image'] ); ?>> <?php esc_html_e( 'Featured Image', 'texttopdfnet-text-web-to-pdf' ); ?></label><br>
							<label><input type="checkbox" name="<?php echo esc_attr( $this->option_key ); ?>[include_author]" value="1" <?php checked( '1', $options['include_author'] ); ?>> <?php esc_html_e( 'Author Name', 'texttopdfnet-text-web-to-pdf' ); ?></label><br>
							<label><input type="checkbox" name="<?php echo esc_attr( $this->option_key ); ?>[include_date]" value="1" <?php checked( '1', $options['include_date'] ); ?>> <?php esc_html_e( 'Publish Date', 'texttopdfnet-text-web-to-pdf' ); ?></label>
						</td>
					</tr>

					<tr>
						<th scope="row"><label for="texttopdf_file_name_pattern"><?php esc_html_e( 'File Name Pattern', 'texttopdfnet-text-web-to-pdf' ); ?></label></th>
						<td>
							<input type="text" id="texttopdf_file_name_pattern" name="<?php echo esc_attr( $this->option_key ); ?>[file_name_pattern]" value="<?php echo esc_attr( $options['file_name_pattern'] ); ?>" class="regular-text">
							<p class="description"><?php esc_html_e( 'Use {title} for post title and {date} for current date.', 'texttopdfnet-text-web-to-pdf' ); ?></p>
						</td>
					</tr>
					
					<tr>
						<th scope="row"><label for="texttopdf_header_text"><?php esc_html_e( 'Global Header Text', 'texttopdfnet-text-web-to-pdf' ); ?></label></th>
						<td>
							<input type="text" id="texttopdf_header_text" name="<?php echo esc_attr( $this->option_key ); ?>[header_text]" value="<?php echo esc_attr( $options['header_text'] ); ?>" class="regular-text">
						</td>
					</tr>
					
					<tr>
						<th scope="row"><label for="texttopdf_footer_text"><?php esc_html_e( 'Global Footer Text', 'texttopdfnet-text-web-to-pdf' ); ?></label></th>
						<td>
							<input type="text" id="texttopdf_footer_text" name="<?php echo esc_attr( $this->option_key ); ?>[footer_text]" value="<?php echo esc_attr( $options['footer_text'] ); ?>" class="regular-text">
						</td>
					</tr>

					<tr>
						<th scope="row"><?php esc_html_e( 'Document Page Size', 'texttopdfnet-text-web-to-pdf' ); ?></th>
						<td>
							<select name="<?php echo esc_attr( $this->option_key ); ?>[page_size]">
								<option value="a4" <?php selected( 'a4', $options['page_size'] ); ?>><?php esc_html_e( 'A4', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="letter" <?php selected( 'letter', $options['page_size'] ); ?>><?php esc_html_e( 'Letter', 'texttopdfnet-text-web-to-pdf' ); ?></option>
							</select>
						</td>
					</tr>
					
					<tr>
						<th scope="row"><?php esc_html_e( 'Font Size', 'texttopdfnet-text-web-to-pdf' ); ?></th>
						<td>
							<select name="<?php echo esc_attr( $this->option_key ); ?>[font_size]">
								<option value="small" <?php selected( 'small', $options['font_size'] ); ?>><?php esc_html_e( 'Small', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="medium" <?php selected( 'medium', $options['font_size'] ); ?>><?php esc_html_e( 'Medium', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="large" <?php selected( 'large', $options['font_size'] ); ?>><?php esc_html_e( 'Large', 'texttopdfnet-text-web-to-pdf' ); ?></option>
							</select>
						</td>
					</tr>
					
					<tr>
						<th scope="row"><?php esc_html_e( 'Margin Preset', 'texttopdfnet-text-web-to-pdf' ); ?></th>
						<td>
							<select name="<?php echo esc_attr( $this->option_key ); ?>[margin_preset]">
								<option value="narrow" <?php selected( 'narrow', $options['margin_preset'] ); ?>><?php esc_html_e( 'Narrow', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="normal" <?php selected( 'normal', $options['margin_preset'] ); ?>><?php esc_html_e( 'Normal', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="wide" <?php selected( 'wide', $options['margin_preset'] ); ?>><?php esc_html_e( 'Wide', 'texttopdfnet-text-web-to-pdf' ); ?></option>
							</select>
						</td>
					</tr>

					<tr>
						<th scope="row"><label for="texttopdf_watermark_text"><?php esc_html_e( 'Watermark Site Name', 'texttopdfnet-text-web-to-pdf' ); ?></label></th>
						<td>
							<input type="text" id="texttopdf_watermark_text" name="<?php echo esc_attr( $this->option_key ); ?>[watermark_text]" value="<?php echo esc_attr( $options['watermark_text'] ); ?>" class="regular-text">
						</td>
					</tr>

					<tr>
						<th scope="row"><label for="texttopdf_watermark_logo"><?php esc_html_e( 'Watermark Logo URL', 'texttopdfnet-text-web-to-pdf' ); ?></label></th>
						<td>
							<input type="text" id="texttopdf_watermark_logo" name="<?php echo esc_attr( $this->option_key ); ?>[watermark_logo]" value="<?php echo esc_attr( $options['watermark_logo'] ); ?>" class="regular-text">
							<button type="button" class="button" id="texttopdf_upload_logo_button"><?php esc_html_e( 'Upload / Select Image', 'texttopdfnet-text-web-to-pdf' ); ?></button>
						</td>
					</tr>

					<tr>
						<th scope="row"><label for="texttopdf_watermark_size"><?php esc_html_e( 'Watermark Logo Size (px)', 'texttopdfnet-text-web-to-pdf' ); ?></label></th>
						<td>
							<input type="number" id="texttopdf_watermark_size" name="<?php echo esc_attr( $this->option_key ); ?>[watermark_size]" value="<?php echo esc_attr( $options['watermark_size'] ); ?>" class="small-text">
						</td>
					</tr>

					<tr>
						<th scope="row"><label for="texttopdf_watermark_opacity"><?php esc_html_e( 'Watermark Opacity (%)', 'texttopdfnet-text-web-to-pdf' ); ?></label></th>
						<td>
							<input type="number" id="texttopdf_watermark_opacity" name="<?php echo esc_attr( $this->option_key ); ?>[watermark_opacity]" value="<?php echo esc_attr( $options['watermark_opacity'] ); ?>" class="small-text" min="1" max="100" step="1"> %
						</td>
					</tr>

					<tr>
						<th scope="row"><?php esc_html_e( 'Watermark Position', 'texttopdfnet-text-web-to-pdf' ); ?></th>
						<td>
							<select name="<?php echo esc_attr( $this->option_key ); ?>[watermark_position]">
								<option value="top-left" <?php selected( 'top-left', $options['watermark_position'] ); ?>><?php esc_html_e( 'Top Left Corner', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="top-right" <?php selected( 'top-right', $options['watermark_position'] ); ?>><?php esc_html_e( 'Top Right Corner', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="center" <?php selected( 'center', $options['watermark_position'] ); ?>><?php esc_html_e( 'Center', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="bottom-left" <?php selected( 'bottom-left', $options['watermark_position'] ); ?>><?php esc_html_e( 'Bottom Left Corner', 'texttopdfnet-text-web-to-pdf' ); ?></option>
								<option value="bottom-right" <?php selected( 'bottom-right', $options['watermark_position'] ); ?>><?php esc_html_e( 'Bottom Right Corner', 'texttopdfnet-text-web-to-pdf' ); ?></option>
							</select>
						</td>
					</tr>

				</table>
				<?php submit_button(); ?>
			</form>
		</div>
		<?php
	}
}
