<?php
/**
 * Block Styles
 *
 * Generates dynamic CSS for blocks based on admin settings.
 *
 * @package DonorCore
 */

namespace DonorCore\Frontend;

defined( 'ABSPATH' ) || exit;

/**
 * Class Block_Styles
 */
class Block_Styles {

	/**
	 * Initialize hooks.
	 */
	public function init() {
		add_action( 'wp_enqueue_scripts', array( $this, 'output_frontend_styles' ) );
		add_action( 'enqueue_block_editor_assets', array( $this, 'output_editor_styles' ) );
	}

	public function output_frontend_styles() {
		$donation_form = $this->get_donation_form_settings();
		$progress_bar  = $this->get_progress_bar_settings();

		$css = $this->generate_donation_form_css( $donation_form );
		$css .= $this->generate_progress_bar_css( $progress_bar );

		// Ensure handle exists or register dummy
		if ( ! wp_script_is( 'donor-core-block-styles', 'enqueued' ) ) {
			wp_register_style( 'donor-core-block-styles', false, array(), DONOR_CORE_VERSION );
			wp_enqueue_style( 'donor-core-block-styles' );
		}
		wp_add_inline_style( 'donor-core-block-styles', wp_strip_all_tags( $css ) );
	}

	/**
	 * Output editor styles.
	 */
	public function output_editor_styles() {
		$donation_form = $this->get_donation_form_settings();
		$progress_bar  = $this->get_progress_bar_settings();

		// Inline styles for editor
		$css = $this->generate_donation_form_css( $donation_form, true );
		$css .= $this->generate_progress_bar_css( $progress_bar, true );

		wp_add_inline_style( 'wp-edit-blocks', wp_strip_all_tags( $css ) );
	}

	/**
	 * Get donation form settings.
	 *
	 * @return array
	 */
	private function get_donation_form_settings() {
		$all_settings = $this->get_all_settings();
		return array(
			'primary_color'   => $all_settings['donation_form_primary_color'],
			'font_family'     => $all_settings['donation_form_font_family'],
			'heading_size'    => $all_settings['donation_form_heading_size'],
			'body_size'       => $all_settings['donation_form_body_size'],
			'border_radius'   => $all_settings['donation_form_border_radius'],
			'bg_color'        => $all_settings['donation_form_bg_color'],
			'text_color'      => $all_settings['donation_form_text_color'],
		);
	}

	/**
	 * Get progress bar settings.
	 *
	 * @return array
	 */
	private function get_progress_bar_settings() {
		$all_settings = $this->get_all_settings();
		return array(
			'primary_color'   => $all_settings['progress_bar_primary_color'],
			'font_family'     => $all_settings['progress_bar_font_family'],
			'heading_size'    => $all_settings['progress_bar_heading_size'],
			'body_size'       => $all_settings['progress_bar_body_size'],
			'border_radius'   => $all_settings['progress_bar_border_radius'],
			'bg_color'        => $all_settings['progress_bar_bg_color'],
			'text_color'      => $all_settings['progress_bar_text_color'],
			'fill_color'      => $all_settings['progress_bar_fill_color'],
			'height'          => $all_settings['progress_bar_height'],
			'padding'         => $all_settings['progress_bar_container_padding'],
			'margin'          => $all_settings['progress_bar_container_margin'],
			'max_width'       => $all_settings['progress_bar_max_width'],
			'shadow'          => $all_settings['progress_bar_shadow'],
		);
	}

	/**
	 * Get all settings (helper to reuse style-generator logic).
	 *
	 * @return array
	 */
	private function get_all_settings() {
		// Get settings from style generator which has all the defaults
		$style_generator = new \DonorCore\Frontend\Style_Generator();
		return $style_generator->get_settings();
	}

	/**
	 * Get font family CSS value.
	 *
	 * @param string $family Font family setting.
	 * @return string
	 */
	private function get_font_family_css( $family ) {
		if ( $family === 'system' || empty( $family ) ) {
			return '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif';
		}
		$clean = stripslashes( wp_strip_all_tags( $family ) );
		$clean = str_replace( array( '&#039;', '&quot;', '&amp;', '\\' ), array( "'", '"', '&', '' ), $clean );
		return trim( $clean );
	}

	/**
	 * Generate donation form CSS.
	 *
	 * @param array $settings Settings array.
	 * @param bool  $for_editor Whether this is for editor.
	 * @return string
	 */
	private function generate_donation_form_css( $settings, $for_editor = false ) {
		$font_family = $this->get_font_family_css( $settings['font_family'] );

		$css = '
			.dc-donation-form-wrapper {
				--dc-df-primary: ' . esc_attr( $settings['primary_color'] ) . ';
				--dc-df-font-family: ' . $font_family . ';
				--dc-df-heading-size: ' . absint( $settings['heading_size'] ) . 'px;
				--dc-df-body-size: ' . absint( $settings['body_size'] ) . 'px;
				--dc-df-radius: ' . absint( $settings['border_radius'] ) . 'px;
				--dc-df-bg: ' . esc_attr( $settings['bg_color'] ) . ';
				--dc-df-text: ' . esc_attr( $settings['text_color'] ) . ';
			}
		';

		return $css;
	}

	/**
	 * Generate progress bar CSS.
	 *
	 * @param array $settings Settings array.
	 * @param bool  $for_editor Whether this is for editor.
	 * @return string
	 */
	private function generate_progress_bar_css( $settings, $for_editor = false ) {
		$font_family = $this->get_font_family_css( $settings['font_family'] );

		// Get shadow CSS value
		$shadow_value = '0 4px 12px rgba(0, 0, 0, 0.08)';
		switch ( $settings['shadow'] ) {
			case 'none': $shadow_value = 'none'; break;
			case 'light': $shadow_value = '0 2px 6px rgba(0, 0, 0, 0.05)'; break;
			case 'strong': $shadow_value = '0 6px 20px rgba(0, 0, 0, 0.15)'; break;
		}

		$css = '
			.dc-progress-bar-wrapper, .progress-bar-editor-preview {
				--dc-pb-primary: ' . esc_attr( $settings['primary_color'] ) . ';
				--dc-pb-font-family: ' . $font_family . ';
				--dc-pb-heading-size: ' . absint( $settings['heading_size'] ) . 'px;
				--dc-pb-body-size: ' . absint( $settings['body_size'] ) . 'px;
				--dc-pb-radius: ' . absint( $settings['border_radius'] ) . 'px;
				--dc-pb-bg: ' . esc_attr( $settings['bg_color'] ) . ';
				--dc-pb-text: ' . esc_attr( $settings['text_color'] ) . ';
				--dc-pb-fill: ' . esc_attr( $settings['fill_color'] ) . ';
				--dc-pb-height: ' . absint( $settings['height'] ) . 'px;
				--dc-pb-padding: ' . absint( $settings['padding'] ) . 'px;
				--dc-pb-margin: ' . absint( $settings['margin'] ) . 'px;
				--dc-pb-max-width: ' . absint( $settings['max_width'] ) . 'px;
				--dc-pb-shadow: ' . esc_attr( $shadow_value ) . ';
			}
		';

		return $css;
	}
}
