<?php
/**
 * Newsletter widget powered by Fluent Forms and FluentCRM.
 *
 * @package GustavoPortfolioCore
 */

defined( 'ABSPATH' ) || exit;

/**
 * Keeps the newsletter CTA available even if the presentation theme changes.
 */
final class GSO_Newsletter_Widget extends WP_Widget {
	/**
	 * Register the widget.
	 */
	public function __construct() {
		parent::__construct(
			'gso_newsletter_widget',
			__( 'Gustavo — Newsletter (FluentCRM)', 'gustavoo-portfolio-core' ),
			array(
				'classname'                   => 'widget_gso_newsletter',
				'description'                 => __( 'Exibe o formulário global de newsletter configurado no Portfólio.', 'gustavoo-portfolio-core' ),
				'customize_selective_refresh' => true,
			)
		);
	}

	/**
	 * Render the widget.
	 *
	 * @param array<string, string> $args     Sidebar wrappers.
	 * @param array<string, mixed>  $instance Saved settings.
	 * @return void
	 */
	public function widget( $args, $instance ) {
		$settings = GSO_Portfolio_Settings::get_settings();
		$title    = ! empty( $instance['title'] ) ? (string) $instance['title'] : (string) $settings['newsletter_heading'];
		$text     = ! empty( $instance['text'] ) ? (string) $instance['text'] : (string) $settings['newsletter_text'];
		$form_id  = ! empty( $instance['form_id'] ) ? absint( $instance['form_id'] ) : absint( $settings['newsletter_form_id'] );

		echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Registered sidebar wrapper.

		if ( $title ) {
			echo $args['before_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Registered sidebar wrapper.
			echo esc_html( $title );
			echo $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Registered sidebar wrapper.
		}

		if ( $text ) {
			printf( '<p class="widget_gso_newsletter__text">%s</p>', esc_html( $text ) );
		}

		$form = gso_render_fluent_form( $form_id );
		if ( $form ) {
			echo $form; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is generated by Fluent Forms.
		} elseif ( current_user_can( 'manage_options' ) ) {
			printf(
				'<p><a href="%1$s">%2$s</a></p>',
				esc_url( admin_url( 'edit.php?post_type=' . GSO_Projects::POST_TYPE . '&page=' . GSO_Portfolio_Settings::PAGE_SLUG ) ),
				esc_html__( 'Configure o formulário de newsletter.', 'gustavoo-portfolio-core' )
			);
		}

		echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Registered sidebar wrapper.
	}

	/**
	 * Render widget settings.
	 *
	 * @param array<string, mixed> $instance Saved settings.
	 * @return void
	 */
	public function form( $instance ) {
		$title   = isset( $instance['title'] ) ? (string) $instance['title'] : '';
		$text    = isset( $instance['text'] ) ? (string) $instance['text'] : '';
		$form_id = isset( $instance['form_id'] ) ? absint( $instance['form_id'] ) : 0;
		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Título:', 'gustavoo-portfolio-core' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>"><?php esc_html_e( 'Texto:', 'gustavoo-portfolio-core' ); ?></label>
			<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'text' ) ); ?>" rows="4"><?php echo esc_textarea( $text ); ?></textarea>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'form_id' ) ); ?>"><?php esc_html_e( 'ID do Fluent Forms (opcional):', 'gustavoo-portfolio-core' ); ?></label>
			<input class="tiny-text" id="<?php echo esc_attr( $this->get_field_id( 'form_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'form_id' ) ); ?>" type="number" min="0" step="1" value="<?php echo esc_attr( (string) $form_id ); ?>">
		</p>
		<?php
	}

	/**
	 * Sanitize widget settings.
	 *
	 * @param array<string, mixed> $new_instance New values.
	 * @param array<string, mixed> $old_instance Previous values.
	 * @return array<string, mixed>
	 */
	public function update( $new_instance, $old_instance ) {
		unset( $old_instance );

		return array(
			'title'   => isset( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '',
			'text'    => isset( $new_instance['text'] ) ? sanitize_textarea_field( $new_instance['text'] ) : '',
			'form_id' => isset( $new_instance['form_id'] ) ? absint( $new_instance['form_id'] ) : 0,
		);
	}
}
