<?php
/**
 * Topping Items admin functionality for FoodOrd.
 *
 * @package FoodOrd
 */

/**
 * Class Foodord_Topping_Items
 */
class Foodord_Topping_Items {

	/**
	 * Constructor.
	 */
	public function __construct() {
		// Constructor.
	}

	/**
	 * Render topping items section.
	 *
	 * @param array|null $existing_set Existing topping set data for edit mode.
	 */
	public function render_topping_items_section( $existing_set = null ) {
		$group_types = array(
			'dropdown' => array(
				'label'       => esc_html__( 'Dropdown', 'foodord' ),
				'description' => esc_html__( 'Customers pick from a compact dropdown menu.', 'foodord' ),
			),
			'radio'    => array(
				'label'       => esc_html__( 'Radio Buttons', 'foodord' ),
				'description' => esc_html__( 'Customers choose a single topping with every option visible.', 'foodord' ),
			),
			'checkbox' => array(
				'label'       => esc_html__( 'Checkboxes', 'foodord' ),
				'description' => esc_html__( 'Customers can pick multiple toppings at once.', 'foodord' ),
			),
		);

		$default_group_type       = 'dropdown';
		$requested_group_type_raw = filter_input( INPUT_GET, 'group_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
		$requested_group_type     = is_string( $requested_group_type_raw ) ? sanitize_key( $requested_group_type_raw ) : '';
		// Use existing data if available, otherwise use requested or default.
		if ( $existing_set && isset( $existing_set['group_type'] ) ) {
			$active_group_type = $existing_set['group_type'];
		} else {
			$active_group_type = array_key_exists( $requested_group_type, $group_types ) ? $requested_group_type : $default_group_type;
		}
		$currency_code      = sanitize_text_field( (string) get_option( 'woocommerce_currency', '' ) );
		$product_categories = get_terms(
			array(
				'taxonomy'   => 'product_cat',
				'hide_empty' => false,
				'orderby'    => 'name',
				'order'      => 'ASC',
			)
		);

		if ( is_wp_error( $product_categories ) ) {
			$product_categories = array();
		}
		?>
		<div class="topping-items-content" data-active-group-type="<?php echo esc_attr( $active_group_type ); ?>">
			<input type="hidden" name="selected_group_type" id="selected_group_type" class="wrc-hidden-input" value="<?php echo esc_attr( $active_group_type ); ?>" />
			<div class="topping-items-panels">
				<?php
				foreach ( $group_types as $group_type => $group_meta ) :
					$is_active             = ( $active_group_type === $group_type );
					$supports_price_toggle = in_array( $group_type, array( 'dropdown', 'radio', 'checkbox' ), true );
					$panel_classes         = 'topping-items-panel' . ( $is_active ? '' : ' step-content--hidden' );
					if ( $supports_price_toggle ) {
						$panel_classes .= ' supports-price-toggle';
					}
					$disabled_attr  = $is_active ? '' : ' disabled="disabled"';
					$panel_label_id = 'topping-items-form-title-' . $group_type;
					?>
					<div class="<?php echo esc_attr( $panel_classes ); ?>" data-group-type="<?php echo esc_attr( $group_type ); ?>" id="topping-items-panel-<?php echo esc_attr( $group_type ); ?>" aria-hidden="<?php echo esc_attr( $is_active ? 'false' : 'true' ); ?>">
						<div class="wrc-topping-items-container">
							<div class="wrc-topping-items-header">
								<h3 class="wrc-section-title" id="<?php echo esc_attr( $panel_label_id ); ?>">
									<?php
									// translators: %s represents the group type label (e.g., Dropdown, Radio Buttons).
									printf( esc_html__( '%s Group Settings', 'foodord' ), esc_html( $group_meta['label'] ) );
									?>
								</h3>
								<p class="description"><?php echo esc_html( $group_meta['description'] ); ?></p>
							</div>
							<div class="wrc-topping-items-content">

					<?php if ( $supports_price_toggle ) : ?>
						<?php
						$hide_price_checked = $existing_set && isset( $existing_set['hide_price'] ) && ! empty( $existing_set['hide_price'] );
						?>
						<div class="topping-panel-controls">
							<label class="topping-hide-price-control" for="topping_hide_price_<?php echo esc_attr( $group_type ); ?>">
								<input type="checkbox" id="topping_hide_price_<?php echo esc_attr( $group_type ); ?>" class="toggle-hide-price wrc-checkbox" data-group-type="<?php echo esc_attr( $group_type ); ?>"<?php echo esc_attr( $disabled_attr ); ?><?php checked( $is_active && $hide_price_checked ); ?> />
								<span>
									<?php
									// translators: %s represents the group type label (e.g., Dropdown, Radio Buttons).
									printf( esc_html__( 'Hide price fields for %s', 'foodord' ), esc_html( $group_meta['label'] ) );
									?>
								</span>
							</label>
							<p class="description">
								<?php
								// translators: %s represents the group type label (e.g., Dropdown, Radio Buttons).
								printf( esc_html__( 'Frontend customers will not see price values for this %s group, but you can still manage them here.', 'foodord' ), esc_html( $group_meta['label'] ) );
								?>
							</p>
							<input type="hidden" name="topping_hide_price[<?php echo esc_attr( $group_type ); ?>]" id="topping_hide_price_state_<?php echo esc_attr( $group_type ); ?>" class="wrc-hidden-input" value="0" />
						</div>
					<?php endif; ?>

						<div class="topping-items-form" role="group" aria-labelledby="<?php echo esc_attr( $panel_label_id ); ?>">
							<div class="wrc-field">
								<label class="wrc-label" for="topping_group_name_<?php echo esc_attr( $group_type ); ?>">
									<?php echo esc_html__( 'Topping Group Name', 'foodord' ); ?>
									<span class="wrc-required" aria-hidden="true">*</span>
									<span class="screen-reader-text"><?php echo esc_html__( 'required', 'foodord' ); ?></span>
								</label>
								<?php
								$group_name_value = '';
								if ( $existing_set && $is_active && isset( $existing_set['name'] ) ) {
									$group_name_value = $existing_set['name'];
								}
								?>
								<input type="text" id="topping_group_name_<?php echo esc_attr( $group_type ); ?>" name="topping_group_name" class="regular-text wrc-text-input" required aria-required="true" value="<?php echo esc_attr( $group_name_value ); ?>"<?php echo esc_attr( $disabled_attr ); ?> />
								<p class="description"><?php echo esc_html__( 'For internal use only', 'foodord' ); ?></p>
							</div>
							<div class="wrc-field">
								<label class="wrc-label" for="topping_group_label_<?php echo esc_attr( $group_type ); ?>">
									<?php echo esc_html__( 'Topping Group Label', 'foodord' ); ?>
									<span class="wrc-required" aria-hidden="true">*</span>
									<span class="screen-reader-text"><?php echo esc_html__( 'required', 'foodord' ); ?></span>
								</label>
								<?php
								$group_label_value = '';
								if ( $existing_set && $is_active && isset( $existing_set['label'] ) ) {
									$group_label_value = $existing_set['label'];
								}
								?>
								<input type="text" id="topping_group_label_<?php echo esc_attr( $group_type ); ?>" name="topping_group_label" class="regular-text wrc-text-input" required aria-required="true" value="<?php echo esc_attr( $group_label_value ); ?>"<?php echo esc_attr( $disabled_attr ); ?> />
								<p class="description"><?php echo esc_html__( 'Visible on frontend to visitors', 'foodord' ); ?></p>
							</div>
							<?php if ( 'checkbox' === $group_type ) : ?>
								<?php $checkbox_limits_legend_id = 'topping_checkbox_limits_' . $group_type; ?>
								<div class="topping-checkbox-limits" role="group" aria-labelledby="<?php echo esc_attr( $checkbox_limits_legend_id ); ?>">
									<span id="<?php echo esc_attr( $checkbox_limits_legend_id ); ?>" class="screen-reader-text"><?php echo esc_html__( 'Checkbox selection limits', 'foodord' ); ?></span>
									<?php $checkbox_min_help_id = 'topping_checkbox_min_help_' . $group_type; ?>
									<div class="wrc-field wrc-field--checkbox-limit">
										<label class="wrc-label" for="topping_checkbox_min_<?php echo esc_attr( $group_type ); ?>"><?php echo esc_html__( 'Min.', 'foodord' ); ?></label>
										<div class="wrc-number-input-control">
											<?php
											$checkbox_min_value = '';
											if ( $existing_set && $is_active && isset( $existing_set['checkbox_min'] ) && null !== $existing_set['checkbox_min'] ) {
												$checkbox_min_value = absint( $existing_set['checkbox_min'] );
											}
											?>
											<input type="number" id="topping_checkbox_min_<?php echo esc_attr( $group_type ); ?>" name="topping_checkbox_minimum" class="small-text wrc-number-input" min="0" step="1" placeholder="0" value="<?php echo esc_attr( $checkbox_min_value ); ?>" aria-describedby="<?php echo esc_attr( $checkbox_min_help_id ); ?>"<?php echo esc_attr( $disabled_attr ); ?> />
											<div class="topping-checkbox-limit-controls">
												<button type="button" class="topping-checkbox-limit-control topping-checkbox-limit-control--increase" data-direction="increase" data-input-id="topping_checkbox_min_<?php echo esc_attr( $group_type ); ?>" aria-label="<?php echo esc_attr__( 'Increase minimum value', 'foodord' ); ?>"<?php echo esc_attr( $disabled_attr ); ?>>
													<span class="dashicons dashicons-arrow-up-alt2" aria-hidden="true"></span>
													<span class="screen-reader-text"><?php echo esc_html__( 'Increase minimum value', 'foodord' ); ?></span>
												</button>
												<button type="button" class="topping-checkbox-limit-control topping-checkbox-limit-control--decrease" data-direction="decrease" data-input-id="topping_checkbox_min_<?php echo esc_attr( $group_type ); ?>" aria-label="<?php echo esc_attr__( 'Decrease minimum value', 'foodord' ); ?>"<?php echo esc_attr( $disabled_attr ); ?>>
													<span class="dashicons dashicons-arrow-down-alt2" aria-hidden="true"></span>
													<span class="screen-reader-text"><?php echo esc_html__( 'Decrease minimum value', 'foodord' ); ?></span>
												</button>
											</div>
										</div>
										<p class="description" id="<?php echo esc_attr( $checkbox_min_help_id ); ?>"><?php echo esc_html__( 'Minimum toppings the customer must select.', 'foodord' ); ?></p>
									</div>
									<?php $checkbox_max_help_id = 'topping_checkbox_max_help_' . $group_type; ?>
									<div class="wrc-field wrc-field--checkbox-limit">
										<label class="wrc-label" for="topping_checkbox_max_<?php echo esc_attr( $group_type ); ?>"><?php echo esc_html__( 'Max.', 'foodord' ); ?></label>
										<div class="wrc-number-input-control">
											<?php
											$checkbox_max_value = '';
											if ( $existing_set && $is_active && isset( $existing_set['checkbox_max'] ) && null !== $existing_set['checkbox_max'] ) {
												$checkbox_max_value = absint( $existing_set['checkbox_max'] );
											}
											?>
											<input type="number" id="topping_checkbox_max_<?php echo esc_attr( $group_type ); ?>" name="topping_checkbox_maximum" class="small-text wrc-number-input" min="0" step="1" placeholder="0" value="<?php echo esc_attr( $checkbox_max_value ); ?>" aria-describedby="<?php echo esc_attr( $checkbox_max_help_id ); ?>"<?php echo esc_attr( $disabled_attr ); ?> />
											<div class="topping-checkbox-limit-controls">
												<button type="button" class="topping-checkbox-limit-control topping-checkbox-limit-control--increase" data-direction="increase" data-input-id="topping_checkbox_max_<?php echo esc_attr( $group_type ); ?>" aria-label="<?php echo esc_attr__( 'Increase maximum value', 'foodord' ); ?>"<?php echo esc_attr( $disabled_attr ); ?>>
													<span class="dashicons dashicons-arrow-up-alt2" aria-hidden="true"></span>
													<span class="screen-reader-text"><?php echo esc_html__( 'Increase maximum value', 'foodord' ); ?></span>
												</button>
												<button type="button" class="topping-checkbox-limit-control topping-checkbox-limit-control--decrease" data-direction="decrease" data-input-id="topping_checkbox_max_<?php echo esc_attr( $group_type ); ?>" aria-label="<?php echo esc_attr__( 'Decrease maximum value', 'foodord' ); ?>"<?php echo esc_attr( $disabled_attr ); ?>>
													<span class="dashicons dashicons-arrow-down-alt2" aria-hidden="true"></span>
													<span class="screen-reader-text"><?php echo esc_html__( 'Decrease maximum value', 'foodord' ); ?></span>
												</button>
											</div>
										</div>
										<p class="description" id="<?php echo esc_attr( $checkbox_max_help_id ); ?>"><?php echo esc_html__( 'Maximum toppings the customer can select.', 'foodord' ); ?></p>
									</div>
								</div>
							<?php endif; ?>
						</div>

						<div class="wrc-toppings-list-container">
							<div class="wrc-toppings-list-header">
								<h4 class="wrc-section-title">
									<?php
									// translators: %s represents the group type label (e.g., Dropdown, Radio Buttons).
									printf( esc_html__( '%s Toppings', 'foodord' ), esc_html( $group_meta['label'] ) );
									?>
								</h4>
							</div>
							<div class="wrc-toppings-list-content">
						<?php
						$allows_default = in_array( $group_type, array( 'dropdown', 'radio', 'checkbox' ), true );
						?>
						<?php if ( $allows_default ) : ?>
							<div class="topping-default-heading">
								<span class="topping-default-heading__label"><?php echo esc_html__( 'Default', 'foodord' ); ?></span>
							</div>
						<?php endif; ?>

						<div class="topping-items-sortable" id="topping-items-sortable-<?php echo esc_attr( $group_type ); ?>" data-group-type="<?php echo esc_attr( $group_type ); ?>">
							<?php
							$existing_items = array();
							if ( $existing_set && $is_active && isset( $existing_set['items'] ) && is_array( $existing_set['items'] ) ) {
								$existing_items = $existing_set['items'];
							}

							// If no existing items, create at least one empty row.
							if ( empty( $existing_items ) ) {
								$existing_items = array(
									array(
										'label'   => '',
										'price'   => 0.0,
										'default' => 0,
									),
								);
							}

							foreach ( $existing_items as $item_index => $item ) :
								$item_label   = isset( $item['label'] ) ? $item['label'] : '';
								$item_price   = isset( $item['price'] ) ? (float) $item['price'] : 0.0;
								$item_default = isset( $item['default'] ) ? (int) $item['default'] : 0;
								?>
							<div class="topping-item-row" data-group-type="<?php echo esc_attr( $group_type ); ?>" data-topping-index="<?php echo esc_attr( $item_index ); ?>">
								<div class="topping-item-fields">
									<div class="wrc-field wrc-field--label">
										<label class="wrc-label" for="topping_item_label_<?php echo esc_attr( $group_type ); ?>_<?php echo esc_attr( $item_index ); ?>">
											<?php echo esc_html__( 'Label', 'foodord' ); ?>
											<span class="wrc-required" aria-hidden="true">*</span>
											<span class="screen-reader-text"><?php echo esc_html__( 'required', 'foodord' ); ?></span>
										</label>
										<input type="text" id="topping_item_label_<?php echo esc_attr( $group_type ); ?>_<?php echo esc_attr( $item_index ); ?>" name="topping_item_label[]" class="regular-text wrc-text-input" required aria-required="true" placeholder="<?php echo esc_attr__( 'Label', 'foodord' ); ?>" value="<?php echo esc_attr( $item_label ); ?>"<?php echo esc_attr( $disabled_attr ); ?> />
									</div>
									<div class="wrc-field wrc-field--price">
										<label class="wrc-label" for="topping_item_price_<?php echo esc_attr( $group_type ); ?>_<?php echo esc_attr( $item_index ); ?>">
											<?php echo esc_html__( 'Price', 'foodord' ); ?>
										</label>
									<div class="wrc-currency-input"<?php echo $currency_code ? ' data-currency="' . esc_attr( $currency_code ) . '"' : ''; ?>>
										<input type="number" id="topping_item_price_<?php echo esc_attr( $group_type ); ?>_<?php echo esc_attr( $item_index ); ?>" name="topping_item_price[]" class="regular-text wrc-number-input" step="0.01" min="0" placeholder="0" value="<?php echo esc_attr( $item_price > 0 ? number_format( $item_price, 2, '.', '' ) : '' ); ?>"<?php echo esc_attr( $disabled_attr ); ?> />
									</div>
									</div>
								</div>
							<div class="topping-item-actions">
								<div class="drag-handle" title="<?php echo esc_attr__( 'Drag to reorder', 'foodord' ); ?>" aria-label="<?php echo esc_attr__( 'Drag to reorder', 'foodord' ); ?>">
									<span class="drag-dots" aria-hidden="true"></span>
								</div>
								<button type="button" class="remove-topping-item" data-group-type="<?php echo esc_attr( $group_type ); ?>" title="<?php echo esc_attr__( 'Remove topping item', 'foodord' ); ?>" aria-label="<?php echo esc_attr__( 'Remove topping item', 'foodord' ); ?>"<?php echo esc_attr( $disabled_attr ); ?>>
									<span class="dashicons dashicons-no-alt" aria-hidden="true"></span>
								</button>
								<?php if ( $allows_default ) : ?>
									<div class="topping-default-control">
										<label class="topping-default-label" for="topping_item_default_<?php echo esc_attr( $group_type ); ?>_<?php echo esc_attr( $item_index ); ?>">
											<input type="checkbox" id="topping_item_default_<?php echo esc_attr( $group_type ); ?>_<?php echo esc_attr( $item_index ); ?>" class="topping-default-checkbox" name="topping_item_default[<?php echo esc_attr( $group_type ); ?>][]" value="<?php echo esc_attr( $item_index ); ?>"<?php echo esc_attr( $disabled_attr ); ?> aria-label="<?php echo esc_attr__( 'Mark topping as default', 'foodord' ); ?>"<?php checked( $item_default ); ?> />
											<span class="screen-reader-text"><?php echo esc_html__( 'Mark topping as default', 'foodord' ); ?></span>
										</label>
									</div>
								<?php endif; ?>
							</div>
							</div>
							<?php endforeach; ?>
						</div>

						<div class="add-topping-action">
							<button type="button" class="button button-secondary add-new-topping" data-group-type="<?php echo esc_attr( $group_type ); ?>"<?php echo esc_attr( $disabled_attr ); ?>>
								<span class="dashicons dashicons-plus"></span>
								<?php echo esc_html__( 'Add New Topping', 'foodord' ); ?>
							</button>
						</div>

						<div class="topping-category-assignment wrc-field wrc-field--categories">
							<?php if ( empty( $product_categories ) ) : ?>
								<span class="wrc-label"><?php echo esc_html__( 'WooCommerce Categories (Global)', 'foodord' ); ?></span>
								<p class="description"><?php echo esc_html__( 'No WooCommerce product categories were found. Create a category to enable this connection.', 'foodord' ); ?></p>
							<?php else : ?>
								<?php $category_label_id = 'topping_category_label_' . $group_type; ?>
								<div class="wrc-category-selector" role="group" aria-labelledby="<?php echo esc_attr( $category_label_id ); ?>">
									<span class="wrc-label wrc-category-selector__title" id="<?php echo esc_attr( $category_label_id ); ?>"><?php echo esc_html__( 'WooCommerce Categories (Global)', 'foodord' ); ?></span>
									<p class="description wrc-category-selector__subtitle"><?php echo esc_html__( 'Category mappings configured here apply globally. To adjust the topping group order, visit the "Product Data" tab on the product edit screen.', 'foodord' ); ?></p>
									<div class="wrc-category-selector__options">
										<?php
										$selected_categories = array();
										if ( $existing_set && $is_active && isset( $existing_set['categories'] ) && is_array( $existing_set['categories'] ) ) {
											$selected_categories = $existing_set['categories'];
										}
										?>
										<?php foreach ( $product_categories as $category ) : ?>
											<?php $category_input_id = 'topping_category_' . $group_type . '_' . $category->term_id; ?>
											<label class="wrc-category-option" for="<?php echo esc_attr( $category_input_id ); ?>">
												<input type="checkbox" id="<?php echo esc_attr( $category_input_id ); ?>" class="wrc-checkbox" name="topping_categories[<?php echo esc_attr( $group_type ); ?>][]" value="<?php echo esc_attr( $category->term_id ); ?>"<?php echo esc_attr( $disabled_attr ); ?><?php checked( in_array( $category->term_id, $selected_categories, true ) ); ?> />
												<span class="wrc-category-option__name"><?php echo esc_html( $category->name ); ?></span>
											</label>
										<?php endforeach; ?>
									</div>
								</div>
								<p class="description"><?php echo esc_html__( 'Select one or more categories where this topping group should be available.', 'foodord' ); ?></p>
							<?php endif; ?>
						</div>
							</div>
						</div>
							</div>
						</div>
					</div>
				<?php endforeach; ?>
			</div>

			<div class="topping-items-actions">
				<button type="button" class="button button-primary" id="topping-items-back">
					<span class="dashicons dashicons-arrow-left-alt"></span>
					<?php echo esc_html__( 'Back', 'foodord' ); ?>
				</button>
			</div>
		</div>
		<?php
	}

	/**
	 * Render fixed save button.
	 */
	public function render_save_button() {
		?>
		<!-- Fixed Save Changes Button -->
		<div class="wrc-save-fixed">
			<button type="submit" class="button button-primary" name="save_topping_group">
				<?php echo esc_html__( 'Save Changes', 'foodord' ); ?>
			</button>
		</div>
		<?php
	}
}
