<?php
/**
 * Invoices settings.
 *
 * @package     EDD\Pro\Admin\Settings
 * @copyright   Copyright (c) 2025, Sandhills Development, LLC
 * @license     https://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       3.5.0
 */

namespace EDD\Pro\Admin\Settings;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use EDD\EventManagement\SubscriberInterface;
use EDD\Admin\Settings\Traits\Helpers;

/**
 * Class Invoices
 *
 * @since 3.5.0
 */
class Invoices implements SubscriberInterface {

	use Helpers;

	/**
	 * Returns an array of events that this subscriber wants to listen to.
	 *
	 * @return array
	 */
	public static function get_subscribed_events() {
		return array(
			'edd_settings_sections_gateways' => 'register_section',
			'edd_settings_gateways'          => 'register_settings',
			'edd_edd-invoices-styles'        => 'render_styles',
			'admin_enqueue_scripts'          => 'enqueue_scripts',
		);
	}

	/**
	 * Registers the Invoices settings section.
	 *
	 * @since 3.5.0
	 * @param array $sections Existing settings sections.
	 * @return array
	 */
	public function register_section( $sections ) {
		$sections['edd-invoices'] = __( 'Invoices', 'easy-digital-downloads' );

		return $sections;
	}

	/**
	 * Registers the Invoices settings.
	 *
	 * @since 3.5.0
	 * @param array $settings Existing settings.
	 * @return array
	 */
	public function register_settings( $settings ) {
		$invoice_settings = array(
			'edd-invoices-disabled'             => array(
				'id'      => 'edd-invoices-disabled',
				'name'    => __( 'Enable Invoices', 'easy-digital-downloads' ),
				'check'   => __( 'Enable invoice functionality.', 'easy-digital-downloads' ),
				'type'    => 'checkbox_toggle',
				'std'     => false,
				'desc'    => __( 'When disabled, all invoice-related functionality will be hidden from both admin and customer-facing areas. Purchase receipts will continue to work normally.', 'easy-digital-downloads' ),
				'options' => array(
					'inverse' => true,
				),
				'data'    => array(
					'edd-requirement'         => 'edd-invoices-disabled',
					'edd-requirement-inverse' => 'true',
				),
			),
			'edd-invoices-disable-editing'      => array(
				'id'      => 'edd-invoices-disable-editing',
				'name'    => __( 'Enable Address Editing', 'easy-digital-downloads' ),
				'check'   => __( 'Allow customers to edit invoice address.', 'easy-digital-downloads' ),
				'type'    => 'checkbox_toggle',
				'std'     => false,
				'options' => array(
					'inverse' => true,
				),
				'desc'    => sprintf(
					/* translators: %s: manage_shop_settings capability */
					__( 'When disabled, customers cannot change their billing information (name, address) displayed on the invoice. Users with the %s capability can always edit this information.', 'easy-digital-downloads' ),
					'<code>manage_shop_settings</code>'
				),
				'class'   => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'inverse' ) ),
			),
			'edd-invoices-disable-free'         => array(
				'id'    => 'edd-invoices-disable-free',
				'name'  => __( 'Disable Invoices for Free Purchases', 'easy-digital-downloads' ),
				'check' => __( 'Optionally disable invoice generation for free purchases.', 'easy-digital-downloads' ),
				'type'  => 'checkbox_toggle',
				'class' => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'inverse' ) ),
			),
			'edd-invoices-ubl'                  => array(
				'id'    => 'edd-invoices-ubl',
				'name'  => __( 'Enable Electronic Invoices', 'easy-digital-downloads' ),
				'check' => __( 'Optionally enable electronic (UBL) invoices for customers.', 'easy-digital-downloads' ),
				'type'  => 'checkbox_toggle',
				'desc'  => __( 'Please ensure that your business information under Downloads > Settings is complete.<br>Store owners can download electronic invoices for individual orders or from Downloads > Reports > Export.', 'easy-digital-downloads' ),
				'class' => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'inverse' ) ),
			),
			'edd-invoices-page'                 => array(
				'id'      => 'edd-invoices-page',
				'name'    => __( 'Invoice Page', 'easy-digital-downloads' ),
				'desc'    => __( 'Which Page contains the [edd_invoices] shortcode? This page is used to display the form customers can use to update how their billing information appears on the invoice.', 'easy-digital-downloads' ),
				'type'    => 'select',
				'options' => edd_get_pages(),
				'class'   => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'inverse' ) ),
			),
			'edd-invoices-logo'                 => array(
				'id'    => 'edd-invoices-logo',
				'name'  => __( 'Logo URL', 'easy-digital-downloads' ),
				'type'  => 'upload',
				'size'  => 'regular',
				'class' => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'inverse' ) ),
			),
			'edd-invoices-styles'               => array(
				'id'    => 'edd-invoices-styles',
				'name'  => __( 'Invoice Style', 'easy-digital-downloads' ),
				'type'  => 'hook',
				'class' => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'inverse' ) ),
			),
			'edd-invoices-company-name'         => array(
				'id'    => 'edd-invoices-company-name',
				'name'  => __( 'Company Name', 'easy-digital-downloads' ),
				'desc'  => __( 'Company Name shown on Invoices', 'easy-digital-downloads' ),
				'type'  => 'text',
				'size'  => 'regular',
				'class' => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'inverse' ) ),
			),
			'edd-invoices-use-business-address' => array(
				'id'    => 'edd-invoices-use-business-address',
				'name'  => __( 'Use Business Address', 'easy-digital-downloads' ),
				'check' => __( 'Use the business address for the company address on invoices.', 'easy-digital-downloads' ),
				'type'  => 'checkbox_toggle',
				'class' => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'edd-invoices-use-business-address', 'inverse' ) ),
			),
			'edd-invoices-company-address'      => array(
				'id'       => 'edd-invoices-company-address',
				'name'     => __( 'Company Address', 'easy-digital-downloads' ),
				'desc'     => __( 'This address block will display on your invoices.', 'easy-digital-downloads' ),
				'type'     => 'textarea',
				'std'      => function_exists( 'edd_invoices_get_company_address' ) ? edd_invoices_get_company_address() : '',
				'class'    => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'edd-invoices-company-address', 'inverse' ) ),
				'readonly' => (bool) edd_get_option( 'edd-invoices-use-business-address' ),
			),
			'edd-invoices-number'               => array(
				'id'    => 'edd-invoices-number',
				'name'  => __( 'Registration Number', 'easy-digital-downloads' ),
				'desc'  => __( 'Company Registration Number', 'easy-digital-downloads' ),
				'type'  => 'text',
				'size'  => 'regular',
				'class' => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'inverse' ) ),
			),
			'edd-invoices-vat'                  => array(
				'id'    => 'edd-invoices-tax',
				'name'  => __( 'Tax/VAT Number', 'easy-digital-downloads' ),
				'desc'  => __( 'Company Tax/VAT Number', 'easy-digital-downloads' ),
				'type'  => 'text',
				'size'  => 'regular',
				'class' => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'inverse' ) ),
			),
			'edd-invoices-additional-text'      => array(
				'id'    => 'edd-invoices-additional-text',
				'name'  => __( 'Additional Text', 'easy-digital-downloads' ),
				'desc'  => __( 'Any text entered here will appear at the bottom of each invoice.', 'easy-digital-downloads' ),
				'type'  => 'rich_editor',
				'size'  => '10',
				'class' => $this->get_requires_css_class( 'edd-invoices-disabled', array( 'inverse' ) ),
			),
		);

		return array_merge( $settings, array( 'edd-invoices' => $invoice_settings ) );
	}

	/**
	 * Renders the invoice styles.
	 *
	 * @since 3.5.1
	 * @param array $args Arguments passed by the setting.
	 * @return void
	 */
	public function render_styles( $args ) {
		?>
		<div class="edd-invoices-styles" style="display:flex;gap:12px;">
			<div class="edd-invoices-styles__style">
				<?php
				edd_select_callback(
					array(
						'id'          => 'edd-invoices-style',
						'name'        => __( 'Invoice Style', 'easy-digital-downloads' ),
						'type'        => 'select',
						'multiple'    => false,
						'options'     => array(
							''        => __( 'Default', 'easy-digital-downloads' ),
							'minimal' => __( 'Minimal', 'easy-digital-downloads' ),
							'lines'   => __( 'Lines', 'easy-digital-downloads' ),
						),
						'desc'        => '',
						'field_class' => 'edd-invoices-style',
					)
				);
				?>
			</div>
			<div class="edd-invoices-styles__colors" style="display:none;">
				<?php
				edd_color_callback(
					array(
						'id'          => 'edd-invoices-color',
						'name'        => __( 'Accent Color', 'easy-digital-downloads' ),
						'type'        => 'color',
						'desc'        => __( 'The accent color is not available for the minimal invoice style.', 'easy-digital-downloads' ),
						'field_class' => '',
					)
				);
				?>
			</div>
		</div>
		<?php
	}

	/**
	 * Enqueues the invoice settings script.
	 *
	 * @since 3.5.1
	 * @return void
	 */
	public function enqueue_scripts() {
		if ( ! edd_is_admin_page( 'settings', 'gateways' ) ) {
			return;
		}
		if ( 'edd-invoices' !== filter_input( INPUT_GET, 'section', FILTER_SANITIZE_SPECIAL_CHARS ) ) {
			return;
		}

		$script = "jQuery(document).ready(function($) {
			var select = $( 'select.edd-invoices-style' ),
				accentWrapper = $( '.edd-invoices-styles__colors' ),
				button = accentWrapper.find( 'button.wp-color-result' ),
				businessAddress = $( '.edd-invoices-use-business-address input' );

			edd_invoices_color_picker( select.val() );
			$( select ).on( 'change', function() {
				edd_invoices_color_picker( $( this ).val() );
			} );

			function edd_invoices_color_picker( value ) {
				if ( 'minimal' === value ) {
					accentWrapper.fadeOut();
					button.attr( 'disabled', true );
				} else {
					accentWrapper.fadeIn();
					button.attr( 'disabled', false );
				}
			}

			businessAddress.on( 'change', function() {
				var companyAddress = $( '.edd-invoices-company-address textarea' );
				if ( $( this ).is( ':checked' ) ) {
					companyAddress.attr( 'readonly', true ).val( '' );
				} else {
					companyAddress.attr( 'readonly', false );
				}
			} );
		} );";
		wp_add_inline_script( 'edd-admin-settings', $script );
	}
}
