<?php
/**
 * WPForms Square Hosted Checkout field.
 *
 * @package SHC4WC
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Payment field that sends buyers to Square Hosted Checkout (no card fields on-site).
 */
class SHC4WC_WPForms_Field extends WPForms_Field {

	/**
	 * Primary class constructor.
	 */
	public function init() {
		$this->name     = esc_html__( 'Square Hosted Checkout', 'payments-connect-square' );
		$this->keywords = esc_html__( 'square, pay, payment, hosted, checkout, credit card', 'payments-connect-square' );
		$this->type     = 'shc4wc-square';
		$this->icon     = 'fa-credit-card';
		$this->order    = 91;
		$this->group    = 'payment';

		$this->default_settings = array(
			'description' => esc_html__(
				'After you submit this form, you will be redirected to Square to complete payment securely.',
				'payments-connect-square'
			),
		);

		$this->hooks();
	}

	/**
	 * Field hooks.
	 */
	private function hooks() {
		add_filter( 'wpforms_field_new_required', array( $this, 'default_required' ), 10, 2 );
		add_filter( 'wpforms_builder_field_button_attributes', array( $this, 'field_button_atts' ), 10, 3 );
		add_filter( 'wpforms_field_new_display_duplicate_button', array( $this, 'hide_duplicate_button' ), 10, 2 );
		add_filter( 'wpforms_field_preview_display_duplicate_button', array( $this, 'hide_duplicate_button' ), 10, 2 );
	}

	/**
	 * Default to required.
	 *
	 * @param bool  $required Required status.
	 * @param array $field    Field settings.
	 * @return bool
	 */
	public function default_required( $required, array $field ) {
		return $this->type === $field['type'] ? true : (bool) $required;
	}

	/**
	 * Limit to one hosted checkout field per form.
	 *
	 * @param array $atts      Button attributes.
	 * @param array $field     Field settings.
	 * @param array $form_data Form data.
	 * @return array
	 */
	public function field_button_atts( $atts, array $field, array $form_data ) {
		$atts = (array) $atts;

		if ( $field['type'] !== $this->type ) {
			return $atts;
		}

		if ( SHC4WC_WPForms::form_has_hosted_field( $form_data ) ) {
			$atts['atts']['disabled'] = 'true';
			$atts['class'][]          = 'wpforms-add-fields-button-disabled';
		}

		if ( ! SHC4WC_Plugin::is_connected() ) {
			$atts['class'][] = 'warning-modal';
			$atts['class'][] = 'shc4wc-square-connection-required';
		}

		return $atts;
	}

	/**
	 * Hide duplicate button for this field type.
	 *
	 * @param bool  $display Display switch.
	 * @param array $field   Field settings.
	 * @return bool
	 */
	public function hide_duplicate_button( $display, array $field ) {
		return $field['type'] === $this->type ? false : (bool) $display;
	}

	/**
	 * Field options in the builder.
	 *
	 * @param array $field Field settings.
	 */
	public function field_options( $field ) {
		$this->field_option( 'basic-options', $field, array( 'markup' => 'open' ) );
		$this->field_option( 'label', $field );
		$this->field_option( 'description', $field );
		$this->field_option( 'required', $field );
		$this->field_option( 'basic-options', $field, array( 'markup' => 'close' ) );
	}

	/**
	 * Field preview in the builder canvas.
	 *
	 * @param array $field Field settings.
	 */
	public function field_preview( $field ) {
		$this->field_preview_option( 'label', $field );
		$this->field_preview_option( 'description', $field );
	}

	/**
	 * Frontend field display.
	 *
	 * @param array $field      Field settings.
	 * @param array $field_atts Field attributes.
	 * @param array $form_data  Form data.
	 */
	public function field_display( $field, $field_atts, $form_data ) {
		if ( ! SHC4WC_Plugin::is_connected() ) {
			echo '<p class="shc4wc-wpforms-notice">' . esc_html__( 'Square payments are not configured yet.', 'payments-connect-square' ) . '</p>';
		}
	}

	/**
	 * @param int   $field_id     Field ID.
	 * @param mixed $field_submit Submitted value.
	 * @param array $form_data    Form data.
	 */
	public function validate( $field_id, $field_submit, $form_data ) {
		unset( $field_id, $field_submit, $form_data );
	}

	/**
	 * @param int   $field_id     Field ID.
	 * @param mixed $field_submit Submitted value.
	 * @param array $form_data    Form data.
	 */
	public function format( $field_id, $field_submit, $form_data ) {
		unset( $field_id, $field_submit, $form_data );
	}
}
