<?php
/**
 * Plugin settings class.
 *
 * @package HBFWC
 */

defined( 'ABSPATH' ) || exit;

if ( class_exists( 'HBFWC_WC_Settings_Tab', false ) ) {
	return new HBFWC_WC_Settings_Tab();
}

/**
 * Class HBFWC_WC_Settings_Tab.
 */
class HBFWC_WC_Settings_Tab extends WC_Settings_Page {
	/**
	 * Setting group.
	 *
	 * @var string
	 */
	protected $setting_group = 'woocommerce_hbfwc_settings';

	/**
	 * Init and hook in the integration.
	 */
	public function __construct() {
		$this->id    = 'hbfwc';
		$this->label = __( 'Hotel Booking', 'hotel-booking-for-woocommerce' );

		parent::__construct();

		add_action( 'woocommerce_admin_field_hbfwc_radio', [ $this, 'generate_radio_html' ], 10 );
	}

	/**
	 * Generate radio HTML.
	 *
	 * @param array  $data Field data.
	 * @since  1.0.10
	 * @return string
	 */
	public function generate_radio_html( $data ) {
		$field_key = $data['field_name'] ?? false;
		if ( ! $field_key ) {
			return;
		}
		?>
		<tr class="<?php echo esc_attr( $data['row_class'] ); ?>">
			<th scope="row" class="titledesc">
				<label for="<?php echo esc_attr( $data['id'] ); ?>"><?php echo esc_html( $data['title'] ); ?></label>
			</th>
			<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $data['type'] ) ); ?>">
				<fieldset>
					<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
						<label><input type="radio" name="<?php echo esc_attr( $field_key ); ?>" value="<?php echo esc_attr( $option_key ); ?>" <?php checked( (string) $option_key, $data['value'] ); ?> >
						<?php echo wp_kses_post( $option_value ); ?>
						</label><br/>
					<?php endforeach; ?>

					<?php echo wp_kses_post( wpautop( wptexturize( $data['description'] ) ) ); // WPCS: XSS ok. ?>
				</fieldset>
			</td>
		</tr>
		<?php
	}

	/**
	 * Get option.
	 *
	 * @param string $key
	 * @param mixed  $default
	 */
	private function get_option( $key, $default = '' ) {
		$settings = get_option( $this->setting_group, [] );
		return $settings[$key] ?? $default;
	}

	/**
	 * Fields for default section.
	 * @return array
	 */
	protected function get_settings_for_default_section() {
		$fields = array(
			'exclude_shop'    => array(
				'title'       => __( 'Exclude from shop', 'hotel-booking-for-woocommerce' ),
				'type'        => 'checkbox',
				'tooltip'     => __( 'Exclude the accommodation products from regular product archive?', 'hotel-booking-for-woocommerce' ),
				'default'     => '',
			),
			'sameday_enabled' => array(
				'title'       => __( 'Enabled same day booking', 'hotel-booking-for-woocommerce' ),
				'type'        => 'checkbox',
				'tooltip'     => __( 'By default, bookings for the current day are disabled. Enable this option to allow customers to book for the same day.', 'hotel-booking-for-woocommerce' ),
				'default'     => '',
			),
			'show_rooms_field' => array(
				'title'   => __( 'Show "Rooms" field in search', 'hotel-booking-for-woocommerce' ),
				'type'    => 'checkbox',
				'tooltip' => __( 'Show a "Rooms" quantity field in the availability search forms, enabling customers to search for multiple rooms at once.', 'hotel-booking-for-woocommerce' ),
				'default' => '',
			),
			'availability_visibility' => array(
				'title'       => __( 'Unavailable rooms in availability search', 'hotel-booking-for-woocommerce' ),
				'type'        => 'select',
				'desc'        => __( 'Choose whether to hide unavailable rooms from the search results or display them while disabling the booking action.', 'hotel-booking-for-woocommerce' ),
				'options'     => array(
					'hide' => __( 'Hide unavailable rooms from the search results', 'hotel-booking-for-woocommerce' ),
					'show' => __( 'Display unavailable rooms but disable the booking action', 'hotel-booking-for-woocommerce' ),
				),
				'default'     => 'hide',
				'desc_tip'    => true,
			),
			'date_format'     => array(
				'title'       => __( 'Output date format', 'hotel-booking-for-woocommerce' ),
				'type'        => 'hbfwc_radio',
				'description' => __( 'Date format for calendar display: This will be used for the check-in and check-out dates in the cart and order.', 'hotel-booking-for-woocommerce' ),
				'desc_tip'    => false,
				'options'     => array(
					'D, j M' => '<span>' . wp_date( 'D, j M' ) . '</span> <code>D, j M</code>',
					'F j, Y' => '<span>' . wp_date( 'F j, Y' ) . '</span> <code>F j, Y</code>',
					'M j, Y' => '<span>' . wp_date( 'M j, Y' ) . '</span> <code>M j, Y</code>',
					'Y-m-d'  => '<span>' . wp_date( 'Y-m-d' ) . '</span> <code>Y-m-d</code>',
					'm/d/Y'  => '<span>' . wp_date( 'm/d/Y' ) . '</span> <code>m/d/Y</code>',
					'd/m/Y'  => '<span>' . wp_date( 'd/m/Y' ) . '</span> <code>d/m/Y</code>',
				),
				'default'     => 'D, j M',
			),
			'max_child_age'   => array(
				'title'             => __( 'Maximum age of children', 'hotel-booking-for-woocommerce' ),
				'type'              => 'number',
				'label'             => __( 'Maximum age of children', 'hotel-booking-for-woocommerce' ),
				'default'           => '16',
				'css'               => 'width: 70px;',
				'desc_tip'          => true,
				'desc'              => __( 'Specify the maximum age to which children are allowed.', 'hotel-booking-for-woocommerce' ),
				'custom_attributes' => array(
					'min'  => '0',
					'step' => '1',
				),
			),
			'addtocart_label' => array(
				'title'       => __( 'Add to cart button label', 'hotel-booking-for-woocommerce' ),
				'type'        => 'text',
				'label'       => __( 'Add to cart button label', 'hotel-booking-for-woocommerce' ),
				'default'     => __( 'Book Your Stay Now', 'hotel-booking-for-woocommerce' ),
				'desc_tip'    => true,
				'desc'        => __( 'For the accommodation single product page.', 'hotel-booking-for-woocommerce' ),
			),
		);

		$form_fields = apply_filters( 'hbfwc_admin_setting_fields', $fields );

		$wc_fields = [
			[
				'title' => __( 'Hotel Booking Settings', 'hotel-booking-for-woocommerce' ),
				'type'  => 'title',
				'id'    => 'hotel_booking_options',
			],
		];
		foreach( $form_fields as $key => $field_args ) {
			$field_args['id']         = $key;
			$field_args['field_name'] = $this->setting_group . "[{$key}]";
			$field_args['value']      = $this->get_option( $key, $field_args['default'] );
			
			$wc_fields[] = $field_args;
		}

		$wc_fields[] = [
			'type' => 'sectionend',
			'id'   => 'hotel_booking_options',
		];

		return $wc_fields;
	}
}

return new HBFWC_WC_Settings_Tab();
