<?php
/**
 * Shortcode registration and dispatch.
 *
 * @package Donatico\Frontend
 */

declare(strict_types=1);

namespace Donatico\Frontend;

/**
 * Registers all five Donatico shortcodes and delegates rendering to
 * FormRenderer (for the form) and WidgetRenderer (for everything else).
 */
class Shortcodes {

	/** @var FormRenderer */
	private FormRenderer $formRenderer;

	/** @var WidgetRenderer */
	private WidgetRenderer $widgetRenderer;

	/** @var Assets */
	private Assets $assets;

	/**
	 * @param FormRenderer   $formRenderer
	 * @param WidgetRenderer $widgetRenderer
	 * @param Assets         $assets
	 */
	public function __construct(
		FormRenderer $formRenderer,
		WidgetRenderer $widgetRenderer,
		Assets $assets
	) {
		$this->formRenderer   = $formRenderer;
		$this->widgetRenderer = $widgetRenderer;
		$this->assets         = $assets;
	}

	/**
	 * Register all shortcodes with WordPress.
	 * Hooked to init.
	 *
	 * @return void
	 */
	public function register(): void {
		add_shortcode( 'donatico_form', array( $this, 'renderForm' ) );
		add_shortcode( 'donatico_total_raised', array( $this, 'renderTotalRaised' ) );
		add_shortcode( 'donatico_donation_count', array( $this, 'renderDonationCount' ) );
		add_shortcode( 'donatico_recent_donors', array( $this, 'renderRecentDonors' ) );
		add_shortcode( 'donatico_progress_bar', array( $this, 'renderProgressBar' ) );
	}

	// -------------------------------------------------------------------------
	// [donatico_form]
	// -------------------------------------------------------------------------

	/**
	 * @param array<string, mixed>|string $atts
	 * @return string
	 */
	public function renderForm( $atts ): string {
		$atts = shortcode_atts(
			array(
				'layout'          => get_option( 'donatico_form_layout', 'stacked' ),
				'amounts'         => '',
				'title'           => '',
				'description'     => '',
				'button_text'     => __( 'Donate Now', 'donatico' ),
				'show_message'    => 'yes',
				'allow_anonymous' => 'yes',
				'goal'            => '',
			),
			$atts,
			'donatico_form'
		);

		return $this->formRenderer->render( $atts );
	}

	// -------------------------------------------------------------------------
	// [donatico_total_raised]
	// -------------------------------------------------------------------------

	/**
	 * @param array<string, mixed>|string $atts
	 * @return string
	 */
	public function renderTotalRaised( $atts ): string {
		$atts = shortcode_atts(
			array(
				'format' => 'currency',
				'since'  => '',
			),
			$atts,
			'donatico_total_raised'
		);

		$this->assets->enqueueNow();
		return $this->widgetRenderer->totalRaised( $atts );
	}

	// -------------------------------------------------------------------------
	// [donatico_donation_count]
	// -------------------------------------------------------------------------

	/**
	 * @param array<string, mixed>|string $atts
	 * @return string
	 */
	public function renderDonationCount( $atts ): string {
		$atts = shortcode_atts(
			array(
				'format'          => 'number',
				'text_singular'   => __( 'donation', 'donatico' ),
				'text_plural'     => __( 'donations', 'donatico' ),
			),
			$atts,
			'donatico_donation_count'
		);

		return $this->widgetRenderer->donationCount( $atts );
	}

	// -------------------------------------------------------------------------
	// [donatico_recent_donors]
	// -------------------------------------------------------------------------

	/**
	 * @param array<string, mixed>|string $atts
	 * @return string
	 */
	public function renderRecentDonors( $atts ): string {
		$atts = shortcode_atts(
			array(
				'limit'          => 5,
				'show_amount'    => 'yes',
				'show_message'   => 'yes',
				'show_date'      => 'yes',
				'hide_anonymous' => 'yes',
			),
			$atts,
			'donatico_recent_donors'
		);

		$this->assets->enqueueNow();
		return $this->widgetRenderer->recentDonors( $atts );
	}

	// -------------------------------------------------------------------------
	// [donatico_progress_bar]
	// -------------------------------------------------------------------------

	/**
	 * @param array<string, mixed>|string $atts
	 * @return string
	 */
	public function renderProgressBar( $atts ): string {
		$atts = shortcode_atts(
			array(
				'goal'            => '',
				'show_amount'     => 'yes',
				'show_percentage' => 'yes',
			),
			$atts,
			'donatico_progress_bar'
		);

		$this->assets->enqueueNow();
		return $this->widgetRenderer->progressBar( $atts );
	}
}
