<?php
/**
 * Admin UI for label field visibility (single control surface).
 *
 * @package PhoenixWP\PriceLabels
 */

declare(strict_types=1);

namespace PhoenixWP\PriceLabels\Admin;

use PhoenixWP\PriceLabels\Labels\Label_Field_Settings;

defined( 'ABSPATH' ) || exit;

/**
 * Renders one label-field checkbox group on the generator screen.
 */
final class Label_Field_Settings_Admin {

	private static ?self $instance = null;

	public static function instance(): self {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	private function __construct() {}

	private function __clone() {}

	public function init(): void {
		// Field preferences persist when a PDF is generated.
	}

	/**
	 * Renders the unified label field selector inside the PDF form.
	 */
	public function render_fields_row(): void {
		$enabled = Label_Field_Settings::get_defaults();
		$labels  = Label_Field_Settings::get_field_labels();

		echo '<tr class="phoenix-price-labels-for-woocommerce-fields-row"><th scope="row">' . esc_html__( 'Label fields', 'phoenix-price-labels-for-woocommerce' ) . '</th><td>';
		echo '<fieldset class="phoenix-price-labels-for-woocommerce-field-settings">';
		echo '<p class="description" style="margin-top:0;">' . esc_html__( 'Choose which fields appear on labels and the checkout overview. Selection is remembered for your next run.', 'phoenix-price-labels-for-woocommerce' ) . '</p>';

		foreach ( Label_Field_Settings::REQUIRED_FIELDS as $field ) {
			$this->render_locked_field( $field, $labels );
		}

		foreach ( Label_Field_Settings::get_toggleable_fields() as $field ) {
			$this->render_toggle_field( $field, $enabled, $labels );
		}

		Unit_Price_Settings_Admin::instance()->render_nested_panel();

		/**
		 * Fires after nested label field settings (unit price, scan code, etc.).
		 */
		do_action( 'phoenix_wp_price_labels_after_label_field_settings' );

		echo '</fieldset></td></tr>';
	}

	/**
	 * @param array<string, string> $labels Field labels.
	 */
	private function render_locked_field( string $field, array $labels ): void {
		$label = $labels[ $field ] ?? $field;

		echo '<label class="phoenix-price-labels-for-woocommerce-field-settings__locked" style="display:block;margin-bottom:0.35em;color:#646970;">';
		echo '<input type="checkbox" checked disabled /> ';
		echo esc_html( $label );
		echo ' <span class="description">(' . esc_html__( 'always', 'phoenix-price-labels-for-woocommerce' ) . ')</span>';
		echo '</label>';
	}

	/**
	 * @param array<string, bool>   $enabled Enabled fields.
	 * @param array<string, string> $labels  Field labels.
	 */
	private function render_toggle_field( string $field, array $enabled, array $labels ): void {
		$label   = $labels[ $field ] ?? $field;
		$checked = ! empty( $enabled[ $field ] );

		echo '<label style="display:block;margin-bottom:0.35em;">';
		printf(
			'<input type="checkbox" name="label_fields[%1$s]" id="phoenix_wp_price_labels_field_%1$s" value="1" %2$s /> %3$s',
			esc_attr( $field ),
			checked( $checked, true, false ),
			esc_html( $label )
		);

		if ( in_array( $field, Label_Field_Settings::OVERVIEW_ONLY_FIELDS, true ) ) {
			echo ' <span class="description">(' . esc_html__( 'overview only', 'phoenix-price-labels-for-woocommerce' ) . ')</span>';
		}

		if ( 'ean' === $field && in_array( 'barcode', Label_Field_Settings::get_toggleable_fields(), true ) ) {
			echo '<span class="description phoenix-price-labels-for-woocommerce-field-settings__ean-hint" style="display:block;margin:0.15em 0 0 1.45em;">';
			echo esc_html__( 'Not available together with the scan code graphic.', 'phoenix-price-labels-for-woocommerce' );
			echo '</span>';
		}

		if ( 'barcode' === $field ) {
			echo '<span class="description phoenix-price-labels-for-woocommerce-field-settings__barcode-hint" hidden style="display:block;margin:0.15em 0 0 1.45em;">';
			echo esc_html__( 'EAN / GTIN text is turned off while scan codes are enabled.', 'phoenix-price-labels-for-woocommerce' );
			echo '</span>';
			echo '<span class="description phoenix-price-labels-for-woocommerce-field-settings__barcode-title-hint" hidden style="display:block;margin:0.1em 0 0 1.45em;">';
			echo esc_html__( 'On label sheets up to 35 mm high, the product title is limited to one line while scan codes are enabled.', 'phoenix-price-labels-for-woocommerce' );
			echo '</span>';
		}

		echo '</label>';
	}
}
